1、顺序表
1 #define MAX 20
2 #define OK 1
3 #define ERRO 0
4 //typedef int linear_TYPE;
5
6 template <typename linear_TYPE> //模板的定义
7 class linear //类的创建
8 {
9 public:
10 linear(); //构造函数(初始化)
11 ~linear(); //析构函数(释放内存变量)
12 void append(linear_TYPE value); //向列表末尾添加元素
13 int insert(int key,linear_TYPE value); //向列表中插入元素
14 void del(); //删除列表中的元素
15 linear_TYPE judge_null(); //判断是否是空列表
16 private:
17 linear_TYPE x_biao[MAX];
18 int x_length;
19
20 };
21
22 //构造函数
23 template <typename linear_TYPE>
24 linear<linear_TYPE>::linear()
25 {
26 x_length = 0;
27 x_biao[MAX]=NULL;
28 }
29
30 //析构函数
31 template <typename linear_TYPE>
32 linear<linear_TYPE>::~linear()
33 {
34 printf("所有任务已执行完毕!");
35 getchar();
36 }
37
38 //判断列表是否为空
39 template <typename linear_TYPE>
40 linear_TYPE linear<linear_TYPE>::judge_null()
41 {
42 if (this->x_length ==0)
43 return OK;
44 return ERRO;
45 }
46
47 //插入
48 template <typename linear_TYPE>
49 int linear<linear_TYPE>::insert(int key,linear_TYPE value)
50 {
51 for (int i=this->x_length;i>=key-1;i--)
52 {
53 this->x_biao[i] = this->x_biao[i-1];
54 }
55 this->x_biao[key-1] = value;
56 this->x_length++;
57 return ERRO;
58 }
59
60 //添加
61 template <typename linear_TYPE>
62 void linear<linear_TYPE>::append(linear_TYPE value)
63 {
64 this->x_biao[this->x_length]= value;
65 this->x_length++;
66 //return ERRO;
67 }
68
69 //删除
70 template <typename Lstatic_TYPE>
71 void linear<linear_TYPE>::del()
72 {
73 if (judge_null())
74 {
75 printf("此表为空表!");
76 //return ERRO;
77 }
78 this->x_biao[this->x_length-1]=NULL;
79 this->x_length = this->x_length-1;
80 }
2、单列表
1 #include <iostream>
2
3 template <class TYPE>
4 class SList
5 {
6 public:
7 SList();
8 ~SList();
9 bool IsEmpty(); //判断是否是空链表
10 int GetElement(int dwIndex); //根据索引获取元素
11 int GetElementIndex(TYPE Element); //根据元素获取下标
12 int Insert(TYPE Element); //新增元素
13 int Insert(int dwIndex,TYPE Element); //插入元素
14 int Delete(int dwIndex); //根据索引删除元素
15 int GetSize(); //获取链表中元素的数量
16 int FindList();
17
18 private:
19 typedef struct LNode{
20 TYPE data;
21 struct LNode *next;
22 };
23 int ListLen;
24 LNode *p_Head;
25 LNode *P_next;
26
27 };
28
29 //构造函数
30 template <class TYPE> SList<TYPE>::SList():p_Head(NULL),ListLen(0)
31 {
32
33 }
34
35 //析构函数
36 template <class TYPE>
37 SList<TYPE>::~SList()
38 {
39 free(p_Head);
40 free(P_next);
41 printf("析构函数执行了!\n");
42 }
43
44
45
46 //判断列表是否为空
47 template <class TYPE> bool SList<TYPE>::IsEmpty()
48 {
49 if (p_Head==NULL && ListLen == 0)
50 {
51 return true;
52 }
53 return false;
54 }
55
56 //插入
57 template <class TYPE> int SList<TYPE>::Insert(TYPE Element)
58 {
59
60 if (IsEmpty())
61 {
62 p_Head = new LNode;
63 p_Head->data = Element;
64 ListLen++;
65 P_next=p_Head;
66 printf("插入成功!\n");
67 // printf("%d\n",ListLen);
68 return 0;
69
70 }
71
72 P_next->next=new LNode;
73 P_next=P_next->next;
74 P_next->data=Element;
75 ListLen++;
76 //printf("%d\n",ListLen);
77 return 0;
78 }
79
80 //指定位置添加元素
81 template <class TYPE> int SList<TYPE>::Insert(int dwIndex,TYPE Element)
82 {
83 LNode *P_next=p_Head;
84 if (IsEmpty())
85 {
86 if (dwIndex > 1 || dwIndex <1)
87 {
88 printf("链表为空表,下标只能为1");
89 return 0;
90 }
91 }
92
93 for(int i=1;i<=ListLen+1;i++)
94 {
95
96 if (i==(dwIndex-1))
97 {
98 LNode *xh;
99 xh=P_next->next;
100 P_next->next=new LNode;
101 P_next=P_next->next;
102 P_next->data=Element;
103 P_next->next=xh;
104 ListLen++;
105
106 return 0;
107 }
108 P_next=P_next->next;
109 }
110
111 return 0;
112 }
113
114 //遍历列表
115 template <class TYPE> int SList<TYPE>::FindList()
116 {
117 printf("\n");
118 LNode *P_next=p_Head;
119 int subscript=0;
120 while(subscript<ListLen)
121 {
122
123
124 printf("%d ",P_next->data);
125 subscript++;
126 P_next=P_next->next;
127
128 }
129 return 0;
130 }
131
132 //根据位置找元素
133 template <class TYPE> int SList<TYPE>::GetElement(int dwIndex)
134 {
135 LNode *P_next=p_Head;
136 int subscript=0;
137 while(subscript<=ListLen)
138 {
139
140 if (subscript == (dwIndex-1) )
141 {
142 printf("\n%d\n ",P_next->data);
143 return 0;
144 }
145
146 subscript++;
147 P_next=P_next->next;
148
149 }
150 return 0;
151 }
152
153 //根据元素找位置
154 template <class TYPE> int SList<TYPE>::GetElementIndex(TYPE Element)
155 {
156 LNode *P_next=p_Head;
157 int subscript=0;
158 while(subscript<ListLen)
159 {
160
161 if (P_next->data == Element)
162 {
163 printf("\n%d\n ",subscript+1);
164 return 0;
165 }
166
167 subscript++;
168 P_next=P_next->next;
169
170 }
171 return 0;
172 }
173
174 //删除指定位置的元素
175 template <class TYPE> int SList<TYPE>::Delete(int dwIndex)
176 {
177 LNode *P_next=p_Head;
178 if (IsEmpty())
179 {
180 printf("链表为空表,下标只能为1");
181 }
182
183 for(int i=1;i<=ListLen+1;i++)
184 {
185
186 if (i==(dwIndex-1))
187 {
188 LNode *xh;
189 xh=P_next->next;
190 xh=xh->next;
191 free(P_next->next);
192 P_next->next=xh;
193 ListLen--;
194
195 return 0;
196 }
197 P_next=P_next->next;
198 }
199
200 return 0;
201 }
202
203 //获取列表的长度
204 template <class TYPE> int SList<TYPE>::GetSize()
205 {
206 printf("\n链表长度:%d",ListLen);
207 return 0;
208 }
2、双列表
1 #define MAX 20
2 #define OK 1
3 #define ERRO 0
4 using namespace std;
5
6 template <typename linear_TYPE> //定义模板
7 class Two_dxl //创建类
8 {
9 public:
10 Two_dxl();
11 ~Two_dxl();
12 void append(linear_TYPE value); //添加元素
13 int Blank_nodes(); //判断链表是否为空
14 void del(linear_TYPE value); //删除
15 private:
16 typedef struct Lclass //创建结构体
17 {
18 Lclass *lchild;
19 Lclass *rchild;
20 linear_TYPE data;
21
22 };
23 Lclass *head;
24 Lclass *next_add;
25
26 };
27
28 //构造函数
29 template<typename linear_TYPE>
30 Two_dxl<linear_TYPE>::Two_dxl()
31 {
32 head = NULL;
33 printf("构造函数执行了");
34
35 }
36
37 //析构函数
38 template<typename linear_TYPE>
39 Two_dxl<linear_TYPE>::~Two_dxl()
40 {
41 printf("析构函数执行了");
42
43 }
44
45 //判断列表是否为空
46 template<typename linear_TYPE>
47 int Two_dxl<typename linear_TYPE>::Blank_nodes()
48 {
49 if (this->head==NULL)
50 {
51 return ERRO;
52 }
53 return OK;
54 }
55
56 //I在列表末尾添加元素
57 template<typename linear_TYPE>
58 void Two_dxl<linear_TYPE>::append(linear_TYPE value)
59 {
60 if (Blank_nodes())
61 {
62 Lclass *p =NULL;
63 p = next_add;
64 next_add = new Lclass;
65 p->rchild = next_add;
66 next_add->lchild = p;
67 next_add->data = value;
68 next_add->rchild = NULL;
69 return ;
70
71 }
72 this->head = new Lclass;
73 head->data = value;
74 head->lchild = NULL;
75 next_add = head;
76 return ;
77 }
78
79 //删除列表中的元素
80 template<typename linear_TYPE>
81 void Two_dxl<linear_TYPE>::del(linear_TYPE value)
82 {
83 Lclass *p =NULL;
84 Lclass *x=NULL;
85 if (Blank_nodes()== ERRO)
86 {
87 printf("此链表为空!");
88 return ;
89 }
90 if (head->data == value)
91 {
92 p=head->rchild;
93 free(head);
94 head = p;
95 return ;
96 }
97 else
98 p=head->rchild;
99 while(OK)
100 {
101
102 if (p->data==value)
103 {
104 x=p->lchild;
105 p->data=0;
106 x->rchild = p->rchild;
107 free(p);
108 return ;
109 }
110 p=head->rchild;
111
112 }
113
114 }
3、循环列表
1 #define MAX 20
2 #define OK 1
3 #define ERRO 0
4 //using namespace std;
5
6
7 template <typename linear_TYPE> //定义模板
8 class Two_dxl //创建类
9 {
10 public:
11 Two_dxl();
12 ~Two_dxl();
13 void append(linear_TYPE value); //添加元素
14 int Blank_nodes(); //判断链表是否为空
15 void del(linear_TYPE value);
16 private:
17 typedef struct Lclass //创建结构体
18 {
19 Lclass *lchild;
20 Lclass *rchild;
21 linear_TYPE data;
22
23 };
24 Lclass *head;
25 Lclass *next_add;
26
27 };
28
29 //构造函数
30 template<typename linear_TYPE>
31 Two_dxl<linear_TYPE>::Two_dxl()
32 {
33 head = NULL;
34 printf("构造函数执行了");
35
36 }
37
38 //析构函数
39 template<typename linear_TYPE>
40 Two_dxl<linear_TYPE>::~Two_dxl()
41 {
42 printf("析构函数执行了");
43
44 }
45
46 //判断列表是否为空
47 template<typename linear_TYPE>
48 int Two_dxl<typename linear_TYPE>::Blank_nodes()
49 {
50 if (this->head==NULL)
51 {
52 return ERRO;
53 }
54 return OK;
55 }
56
57 //添加元素
58 template<typename linear_TYPE>
59 void Two_dxl<linear_TYPE>::append(linear_TYPE value)
60 {
61 if (Blank_nodes())
62 {
63 Lclass *p =NULL;
64 p = next_add;
65 next_add = new Lclass;
66 p->rchild = next_add;
67 next_add->lchild = p;
68 next_add->data = value;
69 next_add->rchild = head;
70 return ;
71
72 }
73 this->head = new Lclass;
74 head->data = value;
75 head->lchild = NULL;
76 next_add = head;
77 return ;
78 }
79
80 //删除元素
81 template<typename linear_TYPE>
82 void Two_dxl<linear_TYPE>::del(linear_TYPE value)
83 {
84 Lclass *p =NULL;
85 Lclass *x=NULL;
86 if (Blank_nodes()== ERRO)
87 {
88 printf("此链表为空!");
89 return ;
90 }
91 if (head->data == value)
92 {
93 p=head->rchild;
94 free(head);
95 head = p;
96 return ;
97 }
98 else
99 p=head->rchild;
100 while(OK)
101 {
102
103 if (p->data==value)
104 {
105 x=p->lchild;
106 p->data=0;
107 x->rchild = p->rchild;
108 free(p);
109 return ;
110 }
111 p=head->rchild;
112
113 }
114
115 }