单链表

本文为个人学习笔记。简单的单链表实现。

 

  实现过程中到的问题:

  ①:定义指针要赋值,或者new一个新的空间。

  ②:delete 不能随便用。。。

先来两张常见基本指针操作截图,再上代码

 

头文件:

 1 #include <iostream>
 2 using namespace std;
 3 
 4 struct Node
 5 {
 6     int data;
 7     Node* next;
 8 };
 9 
10 class List
11 {
12     Node* head;
13 public:
14     List(){head = NULL; length = 0;}
15     ~List(){};
16 
17     void Init();
18     void Insertlist(int _data,unsigned int _location);
19     void Deletelist(unsigned int _location);
20 
21     void Printlist();
22     void Emptylist();
23 
24     int Findkth(unsigned int k);//找到第K个数据
25     int Find(int m_num,unsigned int k);//找到第k个m_num的位置
26 
27 private:
28     Node* m_node;//最后一个元素
29     unsigned int length;
30 };

cpp文件:

  1 #include "list.h"
  2 
  3 int main()
  4 {
  5     List linkedlist;
  6 
  7     linkedlist.Init();
  8     int i = 1;
  9     while (i)
 10     {
 11         cout<<"1:插入;2:删除;3:输出;4:清空;5:查数;6:查位置;0:退出"<<endl;
 12         cin>>i;
 13         switch (i)
 14         {
 15         int data;unsigned int location;
 16         case 0: return 0;
 17         case 1:cout<<"请输入要插入的数据和位置"<<endl;
 18                     cin>>data>>location;
 19                     linkedlist.Insertlist(data,location);
 20                     continue;
 21         case 2:cout<<"请输入要删除的位置:"<<endl;
 22                     cin>>location;
 23                     linkedlist.Deletelist(location);
 24                     continue    ;
 25         case 3:linkedlist.Printlist();
 26                     continue;
 27         case 4:linkedlist.Emptylist();
 28                     continue;
 29         case 5:cout<<"请输入要查找数据的位置:"<<endl;
 30                     cin>>location;
 31                     cout<<linkedlist.Findkth(location)<<endl;;
 32                     continue;
 33         case 6:cout<<"输入num和k"<<endl;
 34                     cin>>data>>location;
 35                     cout<<linkedlist.Find(data,location)<<endl;
 36                     continue    ;
 37             break;
 38         }
 39     }
 40 
 41 }
 42 
 43 //*********************
 44 //初始化链表
 45 //*********************
 46 void List::Init()
 47 {
 48     m_node = new Node;//此处要new一个新的空间
 49     cout<<"输入初始化元素,并以0结束 "<<endl;
 50     int init_data = 0;
 51     while(cin>>init_data)
 52     {
 53         if (init_data == 0)
 54         {
 55             break;
 56         }
 57         
 58         if(length == 0)
 59         {
 60             m_node->data = init_data;
 61             m_node->next = NULL;
 62             head = m_node;
 63         }
 64         else
 65         {
 66             Node* m_temp = new Node;
 67             m_temp->data = init_data;
 68             m_node->next = m_temp;
 69             m_node = m_temp;
 70             m_node->next = NULL;
 71         }
 72         length++;
 73     }
 74 //    delete m_node;//这将会将最后一个节点也删除点
 75 }
 76 
 77 //*********************
 78 //_data为要插入的数
 79 //_location为插入位置
 80 //*********************
 81 void List::Insertlist(int _data,unsigned int _location)
 82 {
 83     if(_location > length+1)
 84     {
 85         cout<<"位置太大,请输入新的数据和位置"<<endl;
 86         cin>>_data>>_location;
 87         Insertlist(_data,_location);
 88     }
 89     else if (_location == 1)
 90     {
 91         Node* m_temp = new Node;
 92         m_temp->data = _data;
 93         m_temp->next = head;
 94         head = m_temp;
 95 
 96         length++;
 97     } 
 98     else
 99     {
100         unsigned int n = 1;
101         Node* m_temp = new Node;
102         m_node = head;
103 
104         while (n < _location - 1)
105         {
106             m_node = m_node->next;
107             n++;
108         }
109         
110         m_temp->data = _data;
111         m_temp->next = m_node->next;
112         m_node->next = m_temp;
113 
114         length++;
115     }
116 }
117 
118 //*********************
119 //_location为删除位置
120 //*********************
121 void List::Deletelist(unsigned int _location)
122 {
123     if(_location > length+1)
124     {
125         cout<<"位置太大,请输入新的位置"<<endl;
126         cin>>_location;
127         Deletelist(_location);
128     }
129     else if (_location == 1)
130     {
131         m_node = head;
132         head = head->next;
133         delete m_node;
134         length--;
135     } 
136     else
137     {
138         unsigned int n = 1;
139         Node* m_temp = new Node;
140         m_node = head;
141 
142         while (n < _location - 1)
143         {
144             m_node = m_node->next;
145             n++;
146         }
147 
148         m_temp = m_node->next;
149         m_node->next = m_temp->next;
150         delete m_temp;
151         length--;
152     }
153 }
154 //*********************
155 //输出链表
156 //*********************
157 void List::Printlist()
158 {
159     Node* m_temp = head;
160     while(m_temp)
161     {
162         cout<<m_temp->data<<"\t";
163         m_temp = m_temp->next;
164     }
165     cout<<endl;
166 }
167 //*********************
168 //清空链表
169 //*********************
170 void List::Emptylist()
171 {
172     Node* m_temp = head;
173     while(m_temp)
174     {
175         head = m_temp->next;
176         delete m_temp;
177         m_temp = head;
178     }
179     length = 0;
180     cout<<endl;
181 }
182 
183 //*********************
184 //查找第k个数
185 //*********************
186 int List::Findkth(unsigned int k)
187 {
188     if (k>length)
189     {
190         cout<<"没找到这个位置,请重新输入:\n";
191         cin>>k;
192         Findkth(k);
193     }
194     else
195     {
196         m_node = head;
197         for (unsigned int i = 1;i<k;i++)
198         {
199             m_node = m_node->next;
200         }
201         return m_node->data;
202     }
203     cout<<"false!\n";
204     return 0;
205 }
206 
207 //*********************
208 ////找到第k个m_num的位置
209 //*********************
210 int List::Find(int m_num,unsigned int k)
211 {
212     if (k>length)
213     {
214         cout<<"没找到第"<<k<<""<<m_num<<",请重新输入:\n";
215         cin>>m_num>>k;
216         Find(m_num,k);
217     }
218     else
219     {
220         m_node = head;
221         int i = 0,j = 0;
222         for (;i != length;i++)
223         {
224             if (m_node->data == m_num)
225             {
226                 j++;
227                 if (j == k)
228                 {
229                     return i+1;
230                 }
231             }
232             m_node = m_node->next;
233         }
234         cout<<"false\n";
235         return -1;
236     }
237 }

 

posted @ 2014-12-04 16:24  小痴_r  阅读(93)  评论(0编辑  收藏  举报