-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_operation.c
265 lines (200 loc) · 5.45 KB
/
list_operation.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
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
typedef int elemType;
typedef struct {
elemType element;
Node *next;
}Node;
elemType gElement;
/* think elemType as int value now */
int getInput() {
printf("begin to input one element :\n");
scanf("%d", &gElement);
if (gElement < 0) {
printf("expect an element larger than 0 \n");
return -1;
}
return 0;
}
void printError() {
printf("errno = %d \n", errno);
printf("_errno2() = %08x %s", __errno2(), errnojr);
}
void initList(Node **pNode) {
*pNode = NULL;
printf("initList successfullly ...\n");
}
/*create a list */
/* keep adding element until input is less than 0 */
/* see getInput() for more info */
/* return List header address */
Node *createList() {
int rc = 0;
Node *p1 = NULL;
Node *p2 = NULL;
Node *pHead = NULL;
while (getInput() == 0) {
p1 = (Node *)malloc(sizeof(Node));
if (p1 == NULL) {
printError();
return __LINE__;
}
memset(p1, 0, sizeof(Node));
p1->element = gElement;
if (pHead == NULL) {
pHead = p1;
} else {
p2->next = p1;
}
p2 = p1;
}
return pHead;
}
/* print List */
void pirntList(Node *pHead) {
if (pHead == NULL) {
printf("pHead is NULL \n");
}
printf("Begin to print List: \n");
while (pHead != NULL) {
printf("%d ", pHead->element);
pHead = pHead->next;
}
}
/* clear the whole List */
void clearList(Node *pHead) {
Node *pNext = NULL;
while (pHead != NULL) {
pNext = pHead->next;
free(pHead);
pHead = pNext;
}
printf("clear List done... \n");
}
/* sizeof List */
int sizeofList(Node *pHead) {
int size = 0;
if (pHead == NULL) {
printf("pHead is NULL size of pHead is 0 \n");
return 0;
}
while (pHead != NULL) {
++size;
pHead = pHead->next;
}
printf("List size is %d \n",size);
return size;
}
/* delete 1 element from List when it appears on the first time */
Node * delListElem(Node *pHead, elemType element) {
Node *pDel = NULL;
Node *pTemp = pHead; /* store header of List */
int place = 1; /* record place of this element */
if (pTemp == NULL) {
printf("pHead is NULL, input List header !\n");
return NULL;
}
if (pTemp->element == element) {
pHead = pTemp->next;
free(pTemp);
printf("Find it at %d place of List and delete it successfully \n", place);
return pHead;
}
while (pTemp->next != NULL) {
place++;
if (pTemp->next->element == element) {
pDel = pTemp->next;
pTemp->next = pDel->next;
free(pDel);
printf("Find it at %d place of List and delete it successfully \n", place);
return pHead;
}
pTemp = Ptemp->next;
}
printf("There is no this element in the List\n ");
printf("We have searched %d elements of the List \n", place);
return NULL;
}
/* get an element at pos of pHead and output it at pElem */
int getElem(Node *pHead, int pos, elemType *pElem) {
int place = 1;
if (pHead == NULL) {
printf("pHead is NULL, input valid list header! \n");
return __LINE__;
}
if (pos < 1 || pos > sizeofList(pHead)) {
printf("input position is invalid \n");
return __LINE__;
}
if (pos == place) {
pElem = &(pHead->element);
printf("change %d element successfully !", pos);
return 0;
}
while (place < pos) {
place++;
pHead = pHead->next;
}
pElem = &(pHead->element);
printf("change %d element successfully !", pos);
return 0;
}
/*change an element at pos of pHead to a new one *pElem */
/* return 0 successfully, otherwise failed */
int changeElem(Node *pHead, int pos, elemType* pElem) {
int place = 1;
if (pHead == NULL) {
printf("pHead is NULL, input valid list header !\n");
return __LINE__;
}
if (pos < 1 || pos > sizeofList(pHead)) {
printf("input position is invalid \n");
return __LINE__;
}
if (pos == place) {
memcpy(&(pHead->element), pElem, sizeof(elemType));
printf("change %d element successfully !", pos);
return 0;
}
while (place < pos) {
place++;
pHead = pHead->next;
}
memcpy(&(pHead->element), pElem, sizeof(elemType));
printf("change %d element successfully !", pos);
return 0;
}
/* insert element 'pElem' into List 'pHead' at 'pos' place*/
/* return List header Node * */
Node * insertElem(Node *pHead, int pos, elemType* pElem) {
Node *pTemp = NULL;
Node *pPrev = NULL;
Node *pTempH = pHead;
int place = 1;
if (pHead == NULL) {
printf("pHead is NULL, input valid list header !\n");
return NULL;
}
if (pos < 1 || pos > sizeofList(pHead)) {
printf("input position is invalid \n");
return NULL;
}
while (place < pos) {
place++;
pPrev = pHead;
pHead = pHead->next;
}
pTemp = (Node *)malloc(sizeof(Node));
if (pTemp == NULL) {
printf("memory malloc() failed, insert failed... \n");
return NULL;
}
memset(pTemp, 0, sizeof(Node));
memcpy(&(pTemp->element), pElem, sizeof(elemType));
(pPrev != NULL) ? (pPrev->next = pTemp) : (pTempH = pTemp);
pTemp->next = pHead;
printf("insert at %d successfully !\n", pos);
return pTempH;
}