1 #ifndef _LIKEDLIST_H_
2 #define _LIKEDLIST_H_
3
4 #include <iostream>
5
6 template <class T>
7 struct Node
8 {
9 T data;
10 Node<T> *next;
11 };
12
13 template <class T>
14 class linkedlist
15 {
16 public:
17 linkedlist(); /*无参构造函数*/
18 linkedlist(T array[], int n); /*头插法构造函数*/
19 linkedlist(int n, T array[]); /*尾插法构造函数*/
20 ~linkedlist(); /*析构函数*/
21
22 int GetLength(); /*返回线性表长度*/
23 T GetElement(int i); /*返回第i个位置的元素值*/
24 void InsertElement(int i, T x); /*在i位置处插入新元素x*/
25 void DeleteElement(int i); /*删除i位置处的元素*/
26
27 void Printlinkedlist(); /*打印链表元素*/
28 private:
29 Node<T> *first; /*头指针*/
30 };
31
32 /*无参构造函数*/
33 template <class T>
34 linkedlist<T>::linkedlist()
35 {
36 /*新建头指针*/
37 first = new Node<T>;
38 /*指向空*/
39 first->next = NULL;
40 }
41
42 /*前插法构造函数*/
43 template <class T>
44 linkedlist<T>::linkedlist(T array[], int n)
45 {
46 /*新建头指针*/
47 first = new Node<T>;
48 first->next = NULL;
49 /*线性表元素赋值*/
50 for(int i = 0; i < n; i++)
51 {
52 Node<T> *s = new Node<T>;
53 s->data = array[i];
54 s->next = first->next;
55 first->next = s;
56 }
57 }
58
59 /*后插法构造函数*/
60 template <class T>
61 linkedlist<T>::linkedlist(int n, T array[])
62 {
63 /*新建头指针*/
64 first = new Node<T>;
65 first->next = NULL;
66 Node<T> *r = first;
67 /*线性表元素赋值*/
68 for(int i = 0; i < n; i++)
69 {
70 Node<T> *s = new Node<T>;
71 s->data = array[i];
72 r->next = s;
73 r = s;
74 }
75 r->next = NULL;
76 }
77
78 /*析构函数*/
79 template <class T>
80 linkedlist<T>::~linkedlist()
81 {
82 /*判断是否为空表*/
83 if (first != NULL)
84 {
85 /*暂存删除节点*/
86 Node<T> *s = first;
87 /*更新头节点*/
88 first = first->next;
89 /*删除暂存节点*/
90 delete s;
91 }
92 }
93
94 /*打印线性表元素*/
95 template <class T>
96 int linkedlist<T>::GetLength()
97 {
98 /*新建一个指向第一个节点的指针*/
99 Node<T> *s = new Node<T>;
100 s = first->next;
101 int j = 0;
102 while (s != NULL)
103 {
104 j++;
105 s = s->next;
106 }
107 return j;
108 }
109
110 /*返回第i个位置的元素值*/
111 template <class T>
112 T linkedlist<T>::GetElement(int i)
113 {
114 Node<T> *s = new Node<T>;
115 s = first->next;
116 int j = 1;
117 /*判断位置是否合理*/
118 while (s != NULL)
119 {
120 if (j == i)
121 return s->data;
122 s = s->next;
123 j++;
124 }
125 if (j < i)
126 {
127 throw"不存在位置i";
128 cout<<"不存在位置i"<<endl;
129 return -1;
130 }
131 }
132
133 /*在i位置处插入新元素x*/
134 template <class T>
135 void linkedlist<T>::InsertElement(int i, T x)
136 {
137 /*新建一个指向头节点的指针*/
138 Node<T> *s = new Node<T>;
139 s = first;
140 int j = 0;
141 /*判断位置是否合理*/
142 while (s != NULL)
143 {
144 /*寻找第i-1位置*/
145 if (j == i-1)
146 {
147 Node<T> *r = new Node<T>;
148 r->data = x;
149 r->next = s->next;
150 s->next = r;
151 break;
152 }
153 else
154 {
155 s = s->next;
156 j++;
157 }
158 }
159 if (j < i-1)
160 {
161 throw "不存在位置i";
162 cout<<"不存在位置i"<<endl;
163 }
164 }
165
166 /*删除i位置处的元素*/
167 template <class T>
168 void linkedlist<T>::DeleteElement(int i)
169 {
170 /*新建一个指向头节点的指针*/
171 Node<T> *s = new Node<T>;
172 s = first;
173 int j = 0;
174 /*判断位置是否合理*/
175 while (s != NULL)
176 {
177 /*寻找第i-1位置*/
178 if (j == i-1)
179 {
180 s->next = s->next->next;
181 break;
182 }
183 else
184 {
185 s = s->next;
186 j++;
187 }
188 }
189 if (j < i-1)
190 {
191 throw "不存在位置i";
192 cout<<"不存在位置i"<<endl;
193 }
194 }
195
196 /*打印线性表元素*/
197 template <class T>
198 void linkedlist<T>::Printlinkedlist()
199 {
200 /*新建一个指向第一个节点的指针*/
201 Node<T> *s = new Node<T>;
202 s = first->next;
203 while (s != NULL)
204 {
205 cout<<s->data<<" ";
206 s = s->next;
207 }
208 cout << endl;
209 }
210 #endif
1 #include "linkedlist.h"
2 #include <iostream>
3 using namespace std;
4
5 int main()
6 {
7 int array[5] = {1,2,3,4,5};
8 int len = 5;
9 /*头插法链表list1*/
10 linkedlist<int> list1 = linkedlist<int>(array,len);
11 /*头插法链表list2*/
12 linkedlist<int> list2 = linkedlist<int>(len,array);
13 cout<<"头插法链表list1:";
14 list1.Printlinkedlist();
15 cout<<"尾插法链表list2:";
16 list2.Printlinkedlist();
17 cout<<"在list1链表的第2个位置插入6"<<endl;
18 int a = 6;
19 list1.InsertElement(2,a);
20 cout<<"list1链表为:";
21 list1.Printlinkedlist();
22 a = list1.GetElement(2);
23 cout<<"list1链表第2个元素为:"<<a<<endl;
24 int b = list1.GetLength();
25 cout<<"list1的长度为"<<b<<endl;
26 cout<<"删除list1链表的第4个位置的元素"<<endl;
27 list1.DeleteElement(4);
28 cout<<"list1链表为:";
29 list1.Printlinkedlist();
30 return 0;
31 }