穷究链表(九)
到这里为止,所有C相关的实现暂时已经完成,里面的代码经过一些不完整的测试,如果有问题,非常感谢能够提供给我,我会尽快将其进行更新。
下面就是对C相关网络上找到的较为可靠的实现进行的分析
到这里为止,所有C相关的实现暂时已经完成,里面的代码经过一些不完整的测试,如果有问题,非常感谢能够提供给我,我会尽快将其进行更新。
下面就是对C相关网络上找到的较为可靠的实现进行的分析,现在有两个实现可以看,一个为书《Data Structures and Algorithm Analysis in C》第二版的链表的实现;一个为linux内核源码中的链表的实现。
这里先讨论第一个实现。有电子书或实体书的同学可以先看一下第三章。其代码下载地址为:Source Code for Data Structures and Algorithm Analysis in C (Second Edition) http://users.cs.fiu.edu/~weiss/dsaa_c2e/files.html
在该书中的实现是使用带有头节点的单向链表来进行了实现。
主要还是看一下Insert和Delete的实现。
Insert将节点值为X的新节点加入到位置P。
Delete将节点值为X的第一个节点进行删除。
实现具体代码如下,这个应该比不带头节点的实现要简单,就不详细说了。
代码1
1#include <stdlib.h>
2#include <stdio.h>
3
4typedef int ElementType;
5
6struct Node;
7typedef struct Node *PtrToNode;
8typedef PtrToNode List;
9typedef PtrToNode Position;
10
11#define Error( Str ) FatalError( Str )
12#define FatalError( Str ) fprintf( stderr, "%s\n", Str ), exit( 1 )
13
14List MakeEmpty( List L );
15int IsEmpty( List L );
16int IsLast( Position P, List L );
17Position Find( ElementType X, List L );
18void Delete( ElementType X, List L );
19Position FindPrevious( ElementType X, List L );
20void Insert( ElementType X, List L, Position P );
21void DeleteList( List L );
22Position Header( List L );
23Position First( List L );
24Position Advance( Position P );
25ElementType Retrieve( Position P );
26
27/**//* Place in the interface file */
28struct Node
29{
30 ElementType Element;
31 Position Next;
32};
33
34List MakeEmpty( List L )
35{
36 if( L != NULL )
37 DeleteList( L );
38 L = malloc( sizeof( struct Node ) );
39 if( L == NULL )
40 FatalError( "Out of memory!" );
41 L->Next = NULL;
42 return L;
43}
44
45/**//* START: fig3_8.txt */
46/**//* Return true if L is empty */
47
48int IsEmpty( List L )
49{
50 return L->Next == NULL;
51}
52/**//* END */
53
54/**//* START: fig3_9.txt */
55/**//* Return true if P is the last position in list L */
56/**//* Parameter L is unused in this implementation */
57
58int IsLast( Position P, List L )
59{
60 return P->Next == NULL;
61}
62/**//* END */
63
64/**//* START: fig3_10.txt */
65/**//* Return Position of X in L; NULL if not found */
66
67Position Find( ElementType X, List L )
68{
69 Position P;
70
71 /**//* 1*/ P = L->Next;
72 /**//* 2*/ while( P != NULL && P->Element != X )
73 /**//* 3*/ P = P->Next;
74
75 /**//* 4*/ return P;
76}
77/**//* END */
78
79/**//* START: fig3_11.txt */
80/**//* Delete from a list */
81/**//* Cell pointed to by P->Next is wiped out */
82/**//* Assume that the position is legal */
83/**//* Assume use of a header node */
84
85void Delete( ElementType X, List L )
86{
87 Position P, TmpCell;
88
89 P = FindPrevious( X, L );
90
91 if( !IsLast( P, L ) ) /**//* Assumption of header use */
92 { /**//* X is found; delete it */
93 TmpCell = P->Next;
94 P->Next = TmpCell->Next; /**//* Bypass deleted cell */
95 free( TmpCell );
96 }
97}
98/**//* END */
99
100/**//* START: fig3_12.txt */
101/**//* If X is not found, then Next field of returned value is NULL */
102/**//* Assumes a header */
103
104Position FindPrevious( ElementType X, List L )
105{
106 Position P;
107
108 /**//* 1*/ P = L;
109 /**//* 2*/ while( P->Next != NULL && P->Next->Element != X )
110 /**//* 3*/ P = P->Next;
111
112 /**//* 4*/ return P;
113}
114/**//* END */
115
116/**//* START: fig3_13.txt */
117/**//* Insert (after legal position P) */
118/**//* Header implementation assumed */
119/**//* Parameter L is unused in this implementation */
120
121void Insert( ElementType X, List L, Position P )
122{
123 Position TmpCell;
124
125 /**//* 1*/ TmpCell = malloc( sizeof( struct Node ) );
126 /**//* 2*/ if( TmpCell == NULL )
127 /**//* 3*/ FatalError( "Out of space!!!" );
128
129 /**//* 4*/ TmpCell->Element = X;
130 /**//* 5*/ TmpCell->Next = P->Next;
131 /**//* 6*/ P->Next = TmpCell;
132}
133/**//* END */
134
135#if 0
136/**//* START: fig3_14.txt */
137/**//* Incorrect DeleteList algorithm */
138
139void DeleteList( List L )
140{
141 Position P;
142
143 /**//* 1*/ P = L->Next; /**//* Header assumed */
144 /**//* 2*/ L->Next = NULL;
145 /**//* 3*/ while( P != NULL )
146 {
147 /**//* 4*/ free( P );
148 /**//* 5*/ P = P->Next;
149 }
150}
151/**//* END */
152#endif
153
154/**//* START: fig3_15.txt */
155/**//* Correct DeleteList algorithm */
156
157void DeleteList( List L )
158{
159 Position P, Tmp;
160
161 /**//* 1*/ P = L->Next; /**//* Header assumed */
162 /**//* 2*/ L->Next = NULL;
163 /**//* 3*/ while( P != NULL )
164 {
165 /**//* 4*/ Tmp = P->Next;
166 /**//* 5*/ free( P );
167 /**//* 6*/ P = Tmp;
168 }
169}
170/**//* END */
171
172Position Header( List L )
173{
174 return L;
175}
176
177Position First( List L )
178{
179 return L->Next;
180}
181
182Position Advance( Position P )
183{
184 return P->Next;
185}
186
187ElementType Retrieve( Position P )
188{
189 return P->Element;
190}
191
1#include <stdlib.h>
2#include <stdio.h>
3
4typedef int ElementType;
5
6struct Node;
7typedef struct Node *PtrToNode;
8typedef PtrToNode List;
9typedef PtrToNode Position;
10
11#define Error( Str ) FatalError( Str )
12#define FatalError( Str ) fprintf( stderr, "%s\n", Str ), exit( 1 )
13
14List MakeEmpty( List L );
15int IsEmpty( List L );
16int IsLast( Position P, List L );
17Position Find( ElementType X, List L );
18void Delete( ElementType X, List L );
19Position FindPrevious( ElementType X, List L );
20void Insert( ElementType X, List L, Position P );
21void DeleteList( List L );
22Position Header( List L );
23Position First( List L );
24Position Advance( Position P );
25ElementType Retrieve( Position P );
26
27/**//* Place in the interface file */
28struct Node
29{
30 ElementType Element;
31 Position Next;
32};
33
34List MakeEmpty( List L )
35{
36 if( L != NULL )
37 DeleteList( L );
38 L = malloc( sizeof( struct Node ) );
39 if( L == NULL )
40 FatalError( "Out of memory!" );
41 L->Next = NULL;
42 return L;
43}
44
45/**//* START: fig3_8.txt */
46/**//* Return true if L is empty */
47
48int IsEmpty( List L )
49{
50 return L->Next == NULL;
51}
52/**//* END */
53
54/**//* START: fig3_9.txt */
55/**//* Return true if P is the last position in list L */
56/**//* Parameter L is unused in this implementation */
57
58int IsLast( Position P, List L )
59{
60 return P->Next == NULL;
61}
62/**//* END */
63
64/**//* START: fig3_10.txt */
65/**//* Return Position of X in L; NULL if not found */
66
67Position Find( ElementType X, List L )
68{
69 Position P;
70
71 /**//* 1*/ P = L->Next;
72 /**//* 2*/ while( P != NULL && P->Element != X )
73 /**//* 3*/ P = P->Next;
74
75 /**//* 4*/ return P;
76}
77/**//* END */
78
79/**//* START: fig3_11.txt */
80/**//* Delete from a list */
81/**//* Cell pointed to by P->Next is wiped out */
82/**//* Assume that the position is legal */
83/**//* Assume use of a header node */
84
85void Delete( ElementType X, List L )
86{
87 Position P, TmpCell;
88
89 P = FindPrevious( X, L );
90
91 if( !IsLast( P, L ) ) /**//* Assumption of header use */
92 { /**//* X is found; delete it */
93 TmpCell = P->Next;
94 P->Next = TmpCell->Next; /**//* Bypass deleted cell */
95 free( TmpCell );
96 }
97}
98/**//* END */
99
100/**//* START: fig3_12.txt */
101/**//* If X is not found, then Next field of returned value is NULL */
102/**//* Assumes a header */
103
104Position FindPrevious( ElementType X, List L )
105{
106 Position P;
107
108 /**//* 1*/ P = L;
109 /**//* 2*/ while( P->Next != NULL && P->Next->Element != X )
110 /**//* 3*/ P = P->Next;
111
112 /**//* 4*/ return P;
113}
114/**//* END */
115
116/**//* START: fig3_13.txt */
117/**//* Insert (after legal position P) */
118/**//* Header implementation assumed */
119/**//* Parameter L is unused in this implementation */
120
121void Insert( ElementType X, List L, Position P )
122{
123 Position TmpCell;
124
125 /**//* 1*/ TmpCell = malloc( sizeof( struct Node ) );
126 /**//* 2*/ if( TmpCell == NULL )
127 /**//* 3*/ FatalError( "Out of space!!!" );
128
129 /**//* 4*/ TmpCell->Element = X;
130 /**//* 5*/ TmpCell->Next = P->Next;
131 /**//* 6*/ P->Next = TmpCell;
132}
133/**//* END */
134
135#if 0
136/**//* START: fig3_14.txt */
137/**//* Incorrect DeleteList algorithm */
138
139void DeleteList( List L )
140{
141 Position P;
142
143 /**//* 1*/ P = L->Next; /**//* Header assumed */
144 /**//* 2*/ L->Next = NULL;
145 /**//* 3*/ while( P != NULL )
146 {
147 /**//* 4*/ free( P );
148 /**//* 5*/ P = P->Next;
149 }
150}
151/**//* END */
152#endif
153
154/**//* START: fig3_15.txt */
155/**//* Correct DeleteList algorithm */
156
157void DeleteList( List L )
158{
159 Position P, Tmp;
160
161 /**//* 1*/ P = L->Next; /**//* Header assumed */
162 /**//* 2*/ L->Next = NULL;
163 /**//* 3*/ while( P != NULL )
164 {
165 /**//* 4*/ Tmp = P->Next;
166 /**//* 5*/ free( P );
167 /**//* 6*/ P = Tmp;
168 }
169}
170/**//* END */
171
172Position Header( List L )
173{
174 return L;
175}
176
177Position First( List L )
178{
179 return L->Next;
180}
181
182Position Advance( Position P )
183{
184 return P->Next;
185}
186
187ElementType Retrieve( Position P )
188{
189 return P->Element;
190}
191