单链表的相关操作(创建、插入、删除、查找、非递减排序、逆置)
1 #include<iostream>
2 using namespace std;
3 struct node //建立一个结构体
4 {
5 int data;
6 struct node *next;
7 };
8
9 node* InitList(int n, struct node *list) //建立单链表
10 {
11 struct node *head, *p, *q;
12 head = NULL;
13 p = new node;
14 q = new node;
15 for (int i = 0; i <= n; i++)
16 {
17 if (i == 0)
18 {
19 head = p;
20 }
21 else
22 {
23 p->next = q;
24 p = q;
25 cin >> p->data;
26 q = new node;
27
28 }
29 }
30 p->next = NULL;
31 return head;
32 }
33
34 void travleList(node *list) //链表的遍历
35 {
36 node *p0;
37 p0 = list;
38 p0 = p0->next;
39 while (p0!= NULL)
40 {
41 cout << p0->data << " ";
42 p0 = p0->next;
43 }
44 }
45
46 void Listinsert(int n, node *list,int e)
47 {
48 node *p1 = list; node *p2 = new node;
49 for (int i = 0; i < n; i++)
50 {
51 p1 = p1->next;
52 }
53 p2->data = e;
54 p2->next = p1->next;
55 p1->next = p2;
56 }
57
58 void Listdelete(int n, node *list) //删除操作
59 {
60 node *p3 = list; node *p4;
61 for (int i = 0; i < n-1; i++)
62 {
63 p3 = p3->next;
64 }
65 p4 = p3->next;
66 p3->next = p4->next;
67 free(p4);
68 }
69
70 node * nizhiList(node * list) //逆置操作
71 {
72 node *p5, *p6, *p7, *real;
73 real = new node;
74 p5 = list; p6 = p5->next; p7 = p6->next;
75 p5 = NULL;
76 while (p6 != NULL)
77 {
78 p6->next = p5;
79 p5 = p6;
80 p6 = p7;
81 if (p7 != NULL)
82 p7 = p7->next;
83 }
84 real->next = p5;
85 return real;
86 }
87 node * List_search(int n, node* list) //查找操作
88 {
89 node *p = list;
90 for (int i = 0; i < n; i++)
91 p = p->next;
92 return p;
93 }
94 /*
95 //按数值查找,若链表中存在,返回查找的数值的位置,
96 若查找的数值不存在,返回NULL;并提示该数值链表中不存在
97 */
98 //m为查找的数值,n为链表的长度,list为查找的链表
99 node *List_search1(int m,int n,node* list)
100 {
101 node *p = list; bool sign = false; int j = 1;
102 for (int i = 0; i < n; i++)
103 {
104 p = p->next;
105 if (p->data == m)
106 {
107 j++;
108 sign = true;
109 break;
110 }
111 }
112 if (sign == false)
113 {
114 cout << "你查找的数值链表中没有!" << endl;
115 return NULL;
116 }
117 else
118 {
119 cout << "你查找的数值在链表中的位置为" << j << endl;
120 return p;
121 }
122 }
123
124 node* feidijian_List_paixu(node* list1, node* list2) //两个非递减链表排序
125 {
126 node *p8, *q1, *pc, *head_c;
127 p8 = list1; q1 = list2; pc=new node;
128 p8 = p8->next; q1 = q1->next; head_c = pc;
129 while (p8 != NULL && q1 != NULL)
130 {
131 if (p8->data > q1->data)
132 {
133 pc->next = q1;
134 pc = q1;
135 q1 = q1->next;
136 }
137 if (p8->data < q1->data)
138 {
139 pc->next = p8;
140 pc = p8;
141 p8 = p8->next;
142 }
143 }
144 if (p8 == NULL)
145 pc->next = q1;
146 if (q1 == NULL)
147 pc->next = p8;
148 return head_c;
149
150 }
151
152 int main()
153 {
154 struct node *list1=NULL; //建立list1链表
155 cout << "请输入链表一的长度:" << endl;
156 int n;
157 cin >> n;
158 cout << "请输入这个链表中的元素:" << endl;
159 list1=InitList(n, list1);
160
161 cout << "链表一的遍历结果:"<<endl; //链表的遍历
162 travleList(list1);
163 cout << endl;
164
165 //查找输入的数值,若链表中存在返回数值的位置,
166 //不存在返回NULL,并提示链表中不存在该数值
167 cout << "请输入你想查找的数值:" << endl;
168 int n3;
169 cin >> n3;
170 cout<<"该数值的物理储存位置为:"<<List_search1(n3, n,list1)<<endl;
171
172
173 cout << "请输入要查找数据的位置:" << endl; //对链表的查找操作
174 int d;
175 cin >> d;
176 node *p = NULL;
177 p = List_search(d, list1);
178 cout << "你查找的数是:" << p->data << endl;
179
180
181 cout << "请输入你要插在第几个元素之后:" << endl; //链表的插入
182 int m, e;
183 cin >> m;
184 cout << "请输入你要插的数:" << endl;
185 cin >> e;
186 if (m > n)
187 cout << "插入的位置超过链表长度!" << endl;
188 else
189 Listinsert(m, list1, e);
190 cout << "插入后的链表遍历:" << endl; //链表一插入数据后的遍历
191 travleList(list1);
192 cout << endl;
193
194
195 cout << "请输入删除数据的位置:" << endl; //链表删除操作
196 int a;
197 cin >> a;
198 if (a > n)
199 cout << "输入的删除的位置超过链表的长度!" << endl;
200 else
201 Listdelete(a, list1);
202 cout << "删除后的链表遍历:" << endl; //链表一删除后的遍历
203 travleList(list1);
204 cout << endl;
205
206
207 list1=nizhiList(list1); //单链表的逆置
208 cout << "单链表一逆置的遍历:" << endl;
209 travleList(list1); //逆置单链表的遍历
210 cout << endl;
211
212 struct node* list4=NULL; //两个非递减链表排序操作
213 cout << "请输入链表二的长度:" << endl; //建立链表四
214 int b;
215 cin >> b;
216 cout << "请输入这个链表中的元素:" << endl;
217 list4 = InitList(b, list4);
218
219 struct node* list5 = NULL; //建立链表四
220 cout << "请输入链表三的长度:" << endl;
221 int c;
222 cin >> c;
223 cout << "请输入这个链表中的元素:" << endl;
224 list5 = InitList(c, list5);
225 list4=feidijian_List_paixu(list4, list5); //非递减排序
226 cout << "两个非递减链表排序的遍历结果:" << endl;
227 travleList(list4); //链表的遍历
228 cout << endl;
229
230 return 0;
231 }