头文件LinkedList.h
View Code
1 //LinkedList.h
2 #include<iostream>
3 #ifndef LINKEDLIST_H_INCLUDED
4 #define LINKEDLIST_H_INCLUDED
5
6 #endif // LINKEDLIST_H_INCLUDED
7
8 using namespace std;
9
10 //const int NULL=0;
11
12 template <class Type> struct LinkNode
13 {
14 public:
15 Type data;
16 LinkNode<Type> *link; //这里不声明type也能通过吧
17
18 LinkNode(LinkNode<Type> *ptr=NULL)
19 {
20 link=ptr;
21 }
22 LinkNode(const Type& item,LinkNode<Type> *ptr=NULL) //注意第二个形参的const可否存在
23 {
24 data=item;
25 link=ptr; //link是一个非const指针
26 }
27 };
28
29
30
31
32 template <class Type> class List
33 {
34 public:
35 List()
36 {
37 first=new LinkNode<Type>;
38 }
39 List(const Type& x)
40 {
41 first=new LinkNode<Type>(x);
42 }
43
44 List(List<Type> &L);
45
46 ~List()
47 {
48 makeEmpty();
49 }
50
51 void makeEmpty();
52 int Length() const;
53 LinkNode<Type>* getHead() const
54 {
55 return first;
56 }
57 LinkNode<Type>* search(Type x);
58 LinkNode<Type>* locate(int i);
59
60 bool getData(int i,Type& x);//将x值置为位置为i的数据元素值
61 void setData(int i,Type& x);//将位置i的数据元素置为x
62
63 bool insert(int i,const Type& x);
64 bool remove(int i,Type& x);
65
66 bool isEmpty() const
67 {
68 return first->link==NULL?true:false;
69 }
70 bool isFull() const
71 {
72 return false;
73 }
74
75 void sort();
76 void input();
77 void output();
78
79 void inputFront(Type endtag);
80 void inputRear(Type endtag);
81 List<Type>& operator=(List<Type>& L);
82
83 protected:
84 LinkNode<Type>* first;
85 };
86
87
88
89
90
91 template<class Type> List<Type>::List(List<Type>& L)
92 {
93 Type value;
94 LinkNode<Type> *srcptr=L.getHead();
95 LinkNode<Type> *destptr=first=new LinkNode<Type>;
96 while(srcptr->link!=NULL)
97 {
98 value=srcptr->link->data;
99 destptr->link=new LinkNode<Type>(value);
100 destptr=destptr->link;
101 srcptr=srcptr->link;
102 }
103 destptr->link=NULL;
104 }
105
106 template<class Type> void List<Type>::makeEmpty()
107 {
108 LinkNode<Type> *q;
109 while(first->link!=NULL)
110 {
111 q=first->link;
112 first->link=q->link;
113 delete q;
114 }
115 }
116
117 template<class Type> int List<Type>::Length() const
118 {
119 int count=0;
120 LinkNode<Type> *q=first->link;
121 while(q!=NULL)
122 {
123 q=q->link;
124 count++;
125 }
126 return count;
127 }
128
129 template<class Type> LinkNode<Type>* List<Type>::search(Type x)
130 {
131 LinkNode<Type> *current=first->link;
132 while(current!=NULL) //写的有技巧
133 if(current->data==x) break;
134 else current=current->link;
135 return current;
136 }
137
138 template<class Type> LinkNode<Type>* List<Type>::locate(int i)
139 {
140 if(i<0) return NULL;
141 LinkNode<Type> *current=first;
142 int k=0;
143 while(current!=NULL&&k<i)
144 {
145 current=current->link;
146 k++;
147 }
148 return current;
149 }
150
151 template<class Type> bool List<Type>::getData(int i,Type& x)
152 {
153 if(i<0)
154 return false;
155 LinkNode<Type> *current=locate(i);
156 if(current==NULL)
157 return false;
158 else
159 {
160 x=current->data;
161 return true;
162 }
163 }
164
165 template<class Type> void List<Type>::setData(int i,Type& x)
166 {
167 if(i<=0)
168 return ;
169 LinkNode<Type> *current=locate(i);
170 if(current==NULL)
171 return ;
172 else current->data=x;
173 }
174
175 template<class Type> bool List<Type>::insert(int i,const Type& x)
176 {
177 LinkNode<Type> *current=locate(i);
178 if(current==NULL)
179 return false;
180 LinkNode<Type> *newNode=new LinkNode<Type>(x);
181 if(newNode==NULL)
182 {
183 cerr<<"存储分配错误!"<<endl;
184 return false;
185 }
186 newNode->link=current->link;
187 current->link=newNode;
188 return true;
189 }
190
191 template<class Type> bool List<Type>::remove(int i,Type& x)
192 {
193 LinkNode<Type> *current=locate(i-1);
194 if(current==NULL||current->link==NULL)
195 return false;
196 LinkNode<Type> *del=current->link;
197 current->link=del->link;
198 x=del->data;
199 delete del;
200 return true;
201 }
202
203 template<class Type> void List<Type>::output()
204 {
205 LinkNode<Type> *current=first->link;
206 while(current!=NULL)
207 {
208 cout<<current->data<<endl;
209 current=current->link;
210 }
211 }
212
213 template<class Type> List<Type>& List<Type>::operator=(List<Type>& L)
214 {
215 Type value;
216 LinkNode<Type> *srcptr=L.getHead();
217 LinkNode<Type> *destptr=first=new LinkNode<Type>;
218 while(srcptr->link!=NULL)
219 {
220 value=srcptr->link->data;
221 destptr->link=new LinkNode<Type>(value);
222 destptr=destptr->link;
223 srcptr=srcptr->link;
224 }
225 destptr->link=NULL;
226 return *this;
227 }
228
229 template<class Type> void List<Type>::inputFront(Type endtag)
230 {
231 LinkNode<Type> *newNode;
232 Type val;
233 makeEmpty();
234 cin>>val;
235 while(val!=endtag)
236 {
237 newNode=new LinkNode<Type>(val);
238 if(newNode==NULL)
239 {
240 cerr<<"输入时存储分配错误!"<<endl;
241 return false;
242 }
243 newNode->link=first->link;
244 first->link=newNode;
245 cin>>val;
246 }
247 }
248
249 template<class Type> void List<Type>::inputRear(Type endtag)
250 {
251 LinkNode<Type> *newNode,*last;
252 Type val;
253 makeEmpty();
254 cin>>val;
255 last=first;
256 while(val!=endtag)
257 {
258 newNode=new LinkNode<Type>(val);
259 if(newNode==NULL)
260 {
261 cerr<<"输入存储分配错误!"<<endl;
262 return ;
263 }
264 last->link=newNode;
265 last=newNode;
266 cin>>val;
267 }
268 }
TestList.cpp
View Code
1 //TestList.cpp
2 #include<iostream>
3 #include"LinkedList.h"
4
5 using namespace std;
6
7 int main()
8 {
9 List<string> list;
10 list.inputRear("a");
11 list.insert(2,"abc");
12 string b;
13 list.remove(10,b);
14 list.output();
15 return 0;
16 }