c语言实现链表的基本操作

前言

本文是用C语言实现的关于链表的增删查改等基本操作。

编译环境

dev c++

下面贴出代码:

代码

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
#include <stdlib.h>
typedef struct
{
int data;
struct *next;
}Node, *ptr_Node;
typedef enum Status
{
SUCCESS, ERROR
}Status;
const int MAXN = 1e5+7;
const int totalPerLine = 8;
int totalNode;
ptr_Node create(int *arr, int n);
void destroy(ptr_Node head);
Status _insert(ptr_Node *head, ptr_Node node, int index);
Status _delete(ptr_Node *head, int index, int *data);
int search(ptr_Node head, int data);
Status edit(ptr_Node head, int index, int *data);
void print(ptr_Node head);
Status sort(ptr_Node *head);
int main()
{
int i, n;
int data[MAXN];
printf("please input n:n");
scanf("%d", &n);
printf("please input %d numbers:n",n);
for(i = 0; i < n; ++i) scanf("%d", &data[i]);
totalNode = n;
ptr_Node head = create(data, n);//将数组的内容输入到链表中;
printf("Now the List is:n");
print(head);//输出链表的内容;
Node* node = (Node*)calloc(1, sizeof(Node*));//初始化要插入的结点;
node->data = 122344;
node->next = NULL;
printf("insert node to 2n");
_insert(&head, node, 2);//将node插入到第2位之后;
print(head);//输出插入后的结果;
int k = 0;
printf("delete a number:n");
_delete(&head, 2, &k);//删除第2个数字的后一位;
print(head);
printf("the delete number is:n");
printf("%dn", k);//输出删除后的结果 ;
k = search(head, 122344);//在链表中寻找122344;
printf("%dn", k);
k = search(head, 12);
printf("%dn", k);
printf("edit the (index+1) number:n");
edit(head, 2, &k);
print(head);
printf("%dn", k);
printf("the sort numbers are:n");
sort(&head);
print(head);
destroy(head);
return 0;
}
ptr_Node create(int *arr, int n)
{
Node *head = (Node*)calloc(1, sizeof(Node));
if(head == NULL) return NULL;
int cur = 0;
head->data = arr[cur++];
Node *curNode = head;
while(cur < n){
Node* temp = (Node*)calloc(1, sizeof(Node));
if(temp == NULL)
{
destroy(head);
return NULL;
}
temp->data = arr[cur++];
curNode->next = temp;
curNode = temp;
}
return head;
}
void destroy(ptr_Node head)
{
Node *curNode = head;
while(curNode != NULL)
{
Node *temp = curNode->next;
free(curNode);
curNode = temp;
}
}
Status _insert(ptr_Node *head, ptr_Node node, int index)
{
int cur = 1;
ptr_Node curNode = *head;
while(cur < index)
{
curNode = curNode->next;
++cur;
}
Node* rest = curNode->next;
curNode->next = node;
node->next = rest;
++totalNode;
return SUCCESS;
}
Status _delete(ptr_Node *head, int index, int *data)
{
if(index == totalNode) return ERROR;
int cur = 1;
ptr_Node curNode = *head;
while(cur < index)
{
curNode = curNode->next;
++cur;
}
Node *rest = curNode->next->next;
Node *temp = curNode->next;
curNode->next = rest;
*data = temp->data;
free(temp);
--totalNode;
return SUCCESS;
}
int search(ptr_Node head, int data)
{
int cur = 0;
ptr_Node curNode = head;
while(curNode->data != data)
{
curNode = curNode->next;
if(curNode == NULL) return -1;
++cur;
}
return cur;
}
Status edit(ptr_Node head, int index, int *data)
{
if(index == totalNode) return ERROR;
int cur = 0;
ptr_Node curNode = head;
while(cur < index)
{
curNode = curNode->next;
++cur;
}
printf("%dn", *data);
int temp = curNode->data;
curNode->data = *data;
*data = temp;
return SUCCESS;
}
void print(ptr_Node head)
{
int curPerLine = 0;
ptr_Node curNode = head;
while(curNode != NULL)
{
if(curPerLine == totalPerLine)
{
puts("");
curPerLine = 0;
}
printf("%d ", curNode->data);
++curPerLine;
curNode = curNode->next;
}
puts("");
}
Status sort(ptr_Node *head)
{
ptr_Node curNodeA = *head;
while(curNodeA != NULL)
{
ptr_Node curNodeB = curNodeA;
ptr_Node temp = curNodeA;
while(curNodeB != NULL)
{
if(temp->data > curNodeB->data) temp = curNodeB;
curNodeB = curNodeB->next;
}
int tempData = curNodeA->data;
curNodeA->data = temp->data;
temp->data = tempData;
curNodeA = curNodeA->next;
}
return SUCCESS;
}

总结

这是第一次做这么长的东西,感觉有点凌乱,有时候找bug找到想哭,不过在这个过程中收获了特别多。