c语言释放整个链表,最简单的链表删除第一个节点时释放内存的问题
該樓層疑似違規(guī)已被系統(tǒng)折疊?隱藏此樓查看此樓
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
}
總結(jié)
以上是生活随笔為你收集整理的c语言释放整个链表,最简单的链表删除第一个节点时释放内存的问题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2022年度个税汇算不再需要预约 有人退
- 下一篇: 电动车将继续降价?碳酸锂价格三个月已经大