数据结构 单链表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
#include<iostream.h>
 
#define null 0;
 
#define OK 1;
 
#define ERROR 0;
 
typedef int Status;
 
typedef struct Node{//定义单链表存储结构
 
int data;
 
struct Node *next;
 
}Node,*LinkList;
 
 
 
Status lnitList(LinkList &L)//初始化单链表
 
{
 
L=new Node;
 
L->next=null;
 
return OK;
 
}
 
bool ListEmpty(LinkList &L)//判空
 
{
 
if(L->next) return false;
 
else
 
return true;
 
}
 
Status CreatListTail(LinkList &L, int n){//尾插法
 
     
 
    LinkList p,r;
 
    int i;
 
    r = L;   
 
    for (i = 0; i < n; i++) {
 
         
 
        p = new Node;
 
        cin>>p->data;
 
p->next=null;
 
        r->next = p;
 
        r = p;
 
    }
 
     
 
    r->next = NULL;
 
return OK;
 
}
 
Status CreatListHead(LinkList &L, int n){//头插法
 
 
    LinkList p;
 
    int i;
 
     
 
    for (i = 0; i < n; i++) {
 
         
 
        p = new Node; // 生成新节点
 
        cin>>p->data;
 
        p->next = L->next;
 
        L->next = p;
 
         
 
    }
 
return OK;
 
}
 
void PrintList(LinkList L)//输出
 
{
 
LinkList p;
 
p=L->next;
 
while(p)
 
{
 
cout<<p->data<<" ";
 
p=p->next;
 
}
 
cout<<endl;
 
}
 
 
//取值
 
Status GetElem(LinkList L, int i){
 
    int e;
 
    int j;
 
    LinkList p;  // 声明指针p
 
     
 
    p = L->next;  // p指向链表L的第一个结点
 
    j = 1;      // 当前位置计数器设置为1
 
     
 
    while (p && j<i) {  // 当p不为空 切j<i时候 j继续向后找
 
         
 
        p = p->next;
 
        j++;
 
    }
 
     
 
    if (!p || j>i) return ERROR;  // 到达结尾且没找到
 
     
 
    e = p->data;  // 获取倒找的结果
 
     
 
    return e;
 
}
 
 
 
//插入
 
Status ListInsert(LinkList &L, int i, int e){
 
     
 
    int j;
 
    LinkList p,s;
 
     
 
    p = L;
 
    j = 1;
 
     
 
    while (p && j<i) {  // 用于寻找第i个结点
 
         
 
        p = p->next;
 
        j++;
 
    }
 
     
 
    if (!p || j>i) return ERROR;  // 到达结尾且没找到
 
     
 
    s = new Node;  //生成一个空节点s
 
     
 
    // 插入语句
 
s->data=e;
 
    s->next = p->next;
 
    p->next = s;
 
     
 
    return OK;
 
}
 
 
 
//删除
 
Status ListDelete(LinkList &L, int i){
 
     
 
    int j;
 
    LinkList p,q;
 
     
 
    p = L;
 
    j = 1;
 
     
 
    while (p && j<i) {  // 用于寻找第i个结点
 
         
 
        p = p->next;
 
        j++;
 
    }
 
     
 
    if (!p || j>i) return ERROR;  // 到达结尾且没找到
 
     
 
    q = p->next;
 
     
 
    // 删除语句
 
    p->next = q->next;
 
    delete q;
 
     
 
    return OK;
 
}
 
 
void ClearList(LinkList &L){
 
 
    LinkList p;
 
     
 
    while (L->next) {
 
        p=L->next;
 
L->next=p->next;
 
        delete p;
 
    }
 
}
 
 
//销毁
 
void DestroyList(LinkList &L)
 
{
 
LinkList p;
 
while(L)
 
{
 
p=L;
 
L=L->next;
 
delete p;
 
}
 
}
 
//表长
 
int LisLength(LinkList L)
 
{
 
int count=0;
 
LinkList p;
 
p=L->next;
 
while(p)
 
{
 
count++;
 
p=p->next;
 
}
 
return count;
 
}
 
//返回位序
 
int LocalteElem(LinkList L,int e)
 
{
 
LinkList p;
 
p=L->next;
 
int i=1;
 
while(p&&p->data!=e)
 
{
 
p=p->next;i++;
 
}
 
return i;
 
}
 
void InversionList(LinkList &L){//逆置
 
Node *p,*q;
 
p=L->next;
 
L->next=null;
 
while(p){
 
q=p->next;
 
p->next=L->next;
 
L->next=p;
 
p=q;
 
}
 
}
 
 
void main()
 
{
 
LinkList L;
 
lnitList(L);
 
cout<<"20-8软工2-9-115"<<endl;
 
if(ListEmpty(L))cout<<"初始化单链表"<<endl;
 
cout<<"输入预计元素个数"<<endl;
 
int i,j,x;;
 
cin>>i;
 
cout<<"---创建单链表---"<<endl;
 
cout<<"---1.头插法---"<<endl;
 
cout<<"---2.尾插法---"<<endl;
 
cin>>j;
 
switch(j)
 
{
 
case 1:CreatListHead(L,i);break;
 
case 2:CreatListTail(L,i);break;
 
}
 
cout<<"输出表:";PrintList(L);
 
cout<<"表长为"<<LisLength(L)<<endl;
 
bool one=1;
 
while(one){
 
cout<<endl;
 
cout<<"请输入将要进行的操作"<<endl;
 
cout<<"1.删除表中元素"<<endl;
 
cout<<"2.往表中插入元素"<<endl;
 
cout<<"3.读取表中第i个元素"<<endl;
 
cout<<"4.按值查找"<<endl;
 
cout<<"5.进行逆置运算"<<endl;
 
cout<<"6.退出"<<endl;
 
cin>>j;
 
switch(j)
 
{
 
case 1:cout<<"删除第几个元素?";
 
cin>>i;ListDelete(L,i);cout<<"输出表:";PrintList(L);
 
cout<<"表长为"<<LisLength(L)<<endl;break;
 
case 2:cout<<"第几个位置插入?";
 
cin>>i;
 
cout<<"插入的元素为?";
 
cin>>x;ListInsert(L,i,x);cout<<"输出表:";PrintList(L);
 
cout<<"表长为"<<LisLength(L)<<endl;break;
 
case 3:cout<<"查找第几个";cin>>i;
 
cout<<"第"<<i<<"个元素的值为"<<GetElem(L,i)<<endl;break;
 
case 4:cout<<"要查找的元素的值为?";cin>>x;
 
cout<<x<<"在表中的位序为"<<LocalteElem(L,x)<<endl;break;
 
case 5:InversionList(L);cout<<"输出表:";PrintList(L);
 
cout<<"表长为"<<LisLength(L)<<endl;break;
 
case 6:one=0;break;
 
}
 
}
 
 
ClearList(L);
 
cout<<"清空后,表长为:"<<LisLength(L)<<endl;
 
DestroyList(L);
 
}

 

posted @   霖霖的信箱  阅读(135)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示