生活随笔
收集整理的這篇文章主要介紹了
单循环链表(C语言实现)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
//CList.h
//結構體定義以及函數聲明
#ifndef CLIST_H
#define CLIST_H#include <stdio.h>
#include <assert.h>
#include <malloc.h>
#include <iostream>typedef int ElemType;
typedef struct Node
{ElemType data;
struct Node *next;
}Node, *PNode;
typedef struct List
{PNode head;PNode tail;
int size;
}List,*PList;
bool InitList(PList
list);
void Create_t(PList
list,ElemType x);
void Create_h(PList
list,ElemType x);
void del_back(PList
list);
void del_front(PList
list);
void sortList(PList
list);
void insert_val(PList
list,ElemType x);
PNode find(PList
list,ElemType x);
void del_val(PList
list,ElemType x);
void modify(PList
list,ElemType x1,ElemType x2);
void clear(PList
list);
void destroy(PList
list);
void reserve(PList
list);
int length(PList
list);
void menu();
void showList(PList
list);
void show_tail(PList
list);
ElemType next(PList
list,ElemType x);
ElemType prio(PList
list,ElemType x);
PNode prev(PList
list,PNode p);
#endif- 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
//CList.cpp
//函數實現
#include"CList.h"void menu()
{printf(
"***************************************************************\n");printf(
"* [0] quit_system [1] Create_t [2] Create_h [3] showList *\n");printf(
"* [4] del_back [5] del_front [6] insert_val [7] show_tail*\n");printf(
"* [8] find [9] del_val [10] sortList [11] modify *\n");printf(
"* [12]clear [13]destroy [14] resver [15]length *\n");printf(
"* [16] next [17]prio *\n");printf(
"***************************************************************\n");
}
bool InitList(PList
list)
{
list->head
= (PNode)malloc(sizeof(Node));
/初始化一個頭結點assert(
list->head
!= NULL);
list->head
->next
= list->head;
list->tail
= list->head;
list->size
= 0;
return true;
}
void Create_t(PList
list,ElemType x)
{PNode s
= (PNode)malloc(sizeof(Node));assert(s
!= NULL);s
->data = x;
list->tail
->next
= s; s
->next
= list->head;
list->tail
= s;
list->size
++;
}
void showList(PList
list)
{
if(
1>list->size){printf(
"--鏈表為空\n");
return ;}PNode p
= list->head
->next;
while(
list->head
!= p) {printf(
"%d ",p
->data);p
=p
->next;}printf(
"\n");
}
void Create_h(PList
list,ElemType x)
{PNode s
= (PNode)malloc(sizeof(Node));assert(s
!= NULL);s
->data = x; s
->next
= list->head
->next;
list->head
->next
= s;
if(
list->size
== 0) {
list->tail
= s;
list->tail
->next
= list->head; }
list->size
++;
}
void del_back(PList
list)
{
if(
0==list->size)
return;PNode p
= list->head;
while(
list->head
!= p
->next
->next) {p
= p
->next;} p
->next
= list->head; free(
list->tail);
list->tail
= p; printf(
"--尾節點已刪除\n");
list->size
--;
}
void del_front(PList
list)
{
if(
0==list->size)
return;
else{PNode p
= list->head
->next;
if(
1==list->size) {
list->tail
= list->head;
list->head
->next
= list->head;}
else{
list->head
->next
= p
->next; }free(p); } printf(
"--頭節點已刪除\n");
list->size
--;
}
void show_tail(PList
list)
{printf(
"--鏈表的尾節點是:》%d \n",
list->tail
->data);
}
void sortList(PList
list)
{
if(
2>list->size)
return ;PNode p
= list->head
->next;PNode q
= p
->next;for(int i
= 0;i
<list->size
-1;i
++,p
= list->head
->next,q
= p
->next) {for(int j
= 0;j
<list->size
-1-i;j
++,p
=q,q
=q
->next) {
if(p
->data > q
->data) {p
->data = p
->data + q
->data;q
->data = p
->data - q
->data;p
->data = p
->data - q
->data;}}}
}
void insert_val(PList
list,ElemType x)
{PNode p
= list->head
->next,q
= list->head;
while(
list->head
!= p) {
if(x
<p
->data)break;q
= p;p
= p
->next;}PNode s
= (PNode)malloc(sizeof(Node)); s
->data = x;q
->next
= s; s
->next
= p;
if(
list->head
== p) {
list->tail
= s;
list->tail
->next
=list->head;}
list->size
++;
}PNode find(PList
list,ElemType x)
{PNode p
= list->head;
while(
list->tail
!= p
&& list->head
!= p
->next
&& x
!= p
->next
->data){p
= p
->next; }
if(
list->head
== p
->next) {printf(
"--沒找到!\n");
return NULL;}
return p;
}
void del_val(PList
list,ElemType x)
{
if(
0 == list->size)
return ;PNode p
= find(
list,x);PNode q
= NULL;
if(
NULL != p){q
= p
->next;
if(q
== list->tail) {p
->next
= list->head;
list->tail
= p;}
else{p
->next
= q
->next;}free(q);
list->size
--;printf(
"--%d已刪除!\n",x);}
return ;
}
void modify(PList
list,ElemType x1,ElemType x2)
{PNode p
= find(
list,x1);
if(
NULL != p)p
->next
->data = x2;
elsereturn ;
}
void clear(PList
list)
{PNode p
= list->head
->next;PNode q
= p;
while(
list->head
!= p) {p
= p
->next; free(q);q
= p;}
list->tail
= list->head;
list->head
->next
= list->head;
list->size
= 0;printf(
"--鏈表已被清空!\n");
}
void destroy(PList
list)
{clear(
list);free(
list->head);
list->head
= NULL;
list->tail
= NULL;printf(
"--鏈表已被摧毀!\n");
}PNode prev(PList
list,PNode p)
{
if(p
!= list->head){PNode q
= list->head
->next;
while(q
!= list->head
&& q
->next
!= p) q
=q
->next;
if(q
->next
== p)
return q;}
return NULL;
}
void reserve(PList
list)
{PNode s
= (PNode)malloc(sizeof(Node)); s
->next
= list->tail;PNode p
= list->tail;
while(
list->tail
!= list->head
->next) {
list->tail
= prev(
list,
list->tail);
list->tail
->next
= list->head;p
->next
= list->tail;p
=p
->next;}p
->next
= s; free(
list->head);
list->head
= s;
}int length(PList
list)
{
return list->size;
}ElemType next(PList
list,ElemType x)
{PNode p
= find(
list,x);
if(
NULL == p)
return -1;
if(p
->next
== list->tail) {
return list->head
->next
->data; }p
=p
->next;
return p
->next
->data;
}
ElemType prio(PList
list,ElemType x)
{PNode p
= find(
list,x);
if(
NULL != p){
if(p
== list->head
|| p
== list->tail){
return list->tail
->data;}
return p
->data;}
return -1;
}
- 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
//main.cpp
//測試函數
#include "CList.h"int main()
{List mylist;InitList(&mylist);ElemType item =
0;
int pos =
0;
int chose =
1;PNode p = NULL;
while(chose){menu();
printf(
"給出想要操作的序號:\n");
scanf(
"%d",&chose);
switch(chose){
case 0:destroy(&mylist);chose =
0;
break;
case 1:
printf(
"輸入要尾插的數據[-1結束]:\n");
while(
scanf(
"%d",&item),item!=-
1){Create_t(&mylist,item);}
break;
case 2:
printf(
"輸入要頭插的數據:\n");
while(
scanf(
"%d",&item),item!=-
1){Create_h(&mylist,item);}
break;
case 3:showList(&mylist);
break;
case 4:del_back(&mylist);
break;
case 5:del_front(&mylist);
break;
case 6:
printf(
"給出要插入的數:\n");
scanf(
"%d",&item);insert_val(&mylist,item);
break;
case 7:show_tail(&mylist);
break;
case 8:
printf(
"輸入要查找的數:\n");
scanf(
"%d",&item);p = find(&mylist,item);
if(NULL!=p)
printf(
"%d\n",p->next->data);
break;
case 9:
printf(
"輸入要刪除的數:\n");
scanf(
"%d",&item);del_val(&mylist,item);
break;
case 10:sortList(&mylist);
break;
case 11:
printf(
"輸入要修改的數和修改后的數\n");
scanf(
"%d %d",&item,&pos);modify(&mylist,item,pos);
break;
case 12:clear(&mylist);
break;
case 13:destroy(&mylist);
break;
case 14:reserve(&mylist);
break;
case 15:
printf(
"鏈表長度為:%d\n",length(&mylist));
break;
case 16:
printf(
"輸入想要找哪個一數的后繼:\n");
scanf(
"%d",&item);
printf(
"%d 的后繼是:%d\n",item,next(&mylist,item));
break;
case 17:
printf(
"輸入想要找哪個一數的前驅:\n");
scanf(
"%d",&item);
printf(
"%d 的前驅是:%d\n",item,prio(&mylist,item));
break;
default:
printf(
"重新輸入\n");
break;} }
return 0;
}
- 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
版權聲明:本文為博主原創文章,未經博主允許不得轉載。 http://blog.csdn.net/Chengzi_comm/article/details/51387151
總結
以上是生活随笔為你收集整理的单循环链表(C语言实现)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。