1 #include <string>
2 #include <iostream>
3 using namespace std;
4
5 typedef int DataType;
6
7 class Node
8 {
9 public:
10 DataType data;
11 Node *next;
12 Node *prev;
13 };
14
15 class TwoWayLinkList
16 {
17 public:
18 TwoWayLinkList();
19 ~TwoWayLinkList();
20 int CreateTwoWayLinkList(int size);
21 int BYETwoWayLinkList();/*销毁*/
22 int TravalTwoWayLinkList();/*遍历链表*/
23 int InsertTwoWayLinklList(Node *data, int n);/*头插传0/1,尾插传this->size+1*/
24 int DeleteTwoWayLinklist(int n);/*删除节点*/
25
26 int GetLen();
27 bool IsEmply();
28
29 Node *head;
30 int size;
31 };
32
33 TwoWayLinkList::TwoWayLinkList()
34 {
35 head = new Node;
36 head->data = 8888;/*我在头结点不存数据的,只是一个链表的开始标记而已*/
37 head->next = NULL;
38 head->prev = NULL;/*头节点没prev指针*/
39 size = 0;
40 }
41
42 TwoWayLinkList::~TwoWayLinkList()
43 {
44 delete head;
45 }
46
47 int TwoWayLinkList::CreateTwoWayLinkList(int size)
48 {
49 if (size < 0) {
50 printf("error\n");
51 return -1;
52 }
53 Node *ptemp = NULL;
54 Node *pnew = NULL;
55
56 this->size = size;
57 ptemp = this->head;
58 for (int i = 0; i<size; i++)
59 {
60 pnew = new Node;
61 pnew->next = NULL;
62 pnew->prev = NULL;
63 cout << "输入第" << i + 1 << "个节点值" << endl;
64 cin >> pnew->data;
65 ptemp->next = pnew;
66 pnew->prev = ptemp;
67 ptemp = pnew;
68 }
69 cout << "Create Completion" << endl;
70 return 0;
71 }
72
73 int TwoWayLinkList::BYETwoWayLinkList()
74 {
75 Node *ptemp;
76 if (this->head == NULL) {
77 cout << "The list is empty" << endl;
78 return -1;
79 }
80 while (this->head)
81 {
82 ptemp = head->next;
83 free(head);
84 head = ptemp;
85 }
86 cout << "Destruction List Completion" << endl;
87 return 0;
88 }
89
90 int TwoWayLinkList::TravalTwoWayLinkList()
91 {
92 Node *ptemp = this->head->next;
93 if (this->head == NULL) {
94 cout << "The list is empty" << endl;
95 return -1;
96 }
97 while (ptemp)
98 {
99 cout << ptemp->data << "->";
100 ptemp = ptemp->next;
101 }
102 cout << "NULL" << endl;
103 return 0;
104 }
105
106 int TwoWayLinkList::InsertTwoWayLinklList(Node *data, int n)
107 {
108 Node *ptemp;
109 if (this->head == NULL) {
110 cout << "The list is empty" << endl;
111 return -1;
112 }
113 if (data == NULL) {
114 cout << "The insertion node is NULL" << endl;
115 return -1;
116 }
117 /*不同插入方法*/
118 //头插
119 if (n<2) {
120 Node *pnew = new Node;
121 pnew->data = data->data;
122 pnew->next = this->head->next;
123 pnew->prev = this->head;
124 this->head->next->prev = pnew;
125 this->head->next = pnew;
126 this->size++;
127 return 0;
128 }
129 //尾插
130 if (n > this->size) {
131 ptemp = this->head;
132 while (ptemp->next != NULL) {
133 ptemp = ptemp->next;
134 }
135 Node *pnew = new Node;
136 pnew->data = data->data;
137 pnew->next = NULL;
138 pnew->prev = ptemp;
139 ptemp->next = pnew;
140 this->size++;
141 return 0;
142 }
143 //中间插
144 else {
145 ptemp = this->head;
146 for (int i = 1; i < n; i++) {
147 ptemp = ptemp->next;
148 }
149 Node *pnew = new Node;
150 pnew->data = data->data;
151 pnew->next = ptemp->next;
152 pnew->prev = ptemp;
153 ptemp->next->prev = pnew;
154 ptemp->next = pnew;
155 this->size++;
156 return 0;
157 }
158 }
159
160 int TwoWayLinkList::DeleteTwoWayLinklist(int n)
161 {
162 Node *ptemp;
163 Node *ptemp2;
164 if (n > this->size) {
165 cout << "The Input size is Go beyond" << endl;
166 return -1;
167 }
168 //删头节点
169 if (n < 2) {
170 ptemp = this->head->next;
171 this->head->next = ptemp->next;
172 ptemp->next->prev = this->head;
173 free(ptemp);
174 this->size--;
175 return 0;
176 }
177 //尾部删除
178 if (n == this->size) {
179 ptemp = this->head;
180 for (int i = 1; i < this->size; i++) {
181 ptemp = ptemp->next;
182 }
183 ptemp2 = ptemp->next;
184 ptemp->next = NULL;
185 free(ptemp2);
186 this->size--;
187 return 0;
188 }
189 //中间删除
190 else
191 {
192 ptemp = this->head;
193 for (int i = 1; i < n; i++) {
194 ptemp = ptemp->next;
195 }
196 ptemp2 = ptemp->next;
197 ptemp->next = ptemp2->next;
198 ptemp2->next->prev = ptemp;
199 free(ptemp2);
200 this->size--;
201 return 0;
202 }
203 }
204
205 int TwoWayLinkList::GetLen()
206 {
207 return this->size;
208 }
209
210 bool TwoWayLinkList::IsEmply()
211 {
212 if (this->head->next == NULL) {
213 return true;
214 }
215 else {
216 return false;
217 }
218 }
219
220 void main(void)
221 {
222 Node temp;
223 temp.data = 9;
224 temp.next = NULL;
225 temp.prev = NULL;
226
227 TwoWayLinkList list;
228 TwoWayLinkList *plist = &list;
229 plist->CreateTwoWayLinkList(5);
230 plist->TravalTwoWayLinkList();
231 plist->InsertTwoWayLinklList(&temp, 0);
232 plist->TravalTwoWayLinkList();
233 plist->InsertTwoWayLinklList(&temp, 3);
234 plist->TravalTwoWayLinkList();
235 plist->InsertTwoWayLinklList(&temp, plist->size);
236 plist->TravalTwoWayLinkList();
237 plist->DeleteTwoWayLinklist(0);
238 plist->TravalTwoWayLinkList();
239 plist->DeleteTwoWayLinklist(3);
240 plist->TravalTwoWayLinkList();
241 system("pause");
242 }