该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
1 #include
2
#include "list.h"
3
4 int IsEmpty(List L)
5 {
6 return L->Next == NULL;
7 }
8
9 int IsLast(Position P, List L)
10
{
11 return P->Next == NULL;
12 }
13
14 Position Find(int X, List L)
15 {
16 Position P;
17
18 P = L->Next;
19 while (P != NULL && P->Element != X)
20 {
21 P =
P->Next;
22 }
23
24 return P;
25 }
26
27 void Delete(int X, List
L)
28 {
29
Position P, TmpCell;
30
31 P = FindPrevious(X, L);
32
33 if (!IsLast(P, L))
34
{
35 TmpCell =
P->Next;
36 P->Next =
TmpCell->Next;
37
free(TmpCell);
38 }
39
40 return;
41 }
42
43 Position
FindPrevious(int X, List L)
44
{
45 Position P;
46
47 P = L;
48 while (P->Next !=
NULL && P->Next->Element != X)
49 {
50 P =
P->Next;
51 }
52
53 return P;
54 }
55
56 void Insert(int X, List L,
Position P)
57 {
58
Position TmpCell;
59
60 TmpCell = malloc(sizeof(struct Node));
61
62 if (TmpCell == NULl)
63
{
64 fprintf(stderr, "Out of
space!!!");
65 exit(1);
66 }
67
68
TmpCell->Element = X;
69
TmpCell->Next = P->Next;
70
P->Next = TmpCell;
71
72 return;
73 }
74
75 void DeleteList(List
L)
76 {
77
Position P, Tmp;
78
79 p = L->Next;
80
L->Next = NULL;
81 while (P != NULL)
82 {
83 Tmp = P->Next;
84
free(P);
85 P =
Tmp;
86 }
87
}