到这里为止,所有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的第一个节点进行删除。
实现具体代码如下,这个应该比不带头节点的实现要简单,就不详细说了。
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
代码1
1
#include <stdlib.h>
2
#include <stdio.h>
3![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
typedef int ElementType;
5![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
struct Node;
7
typedef struct Node *PtrToNode;
8
typedef PtrToNode List;
9
typedef PtrToNode Position;
10![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
11
#define Error( Str ) FatalError( Str )
12
#define FatalError( Str ) fprintf( stderr, "%s\n", Str ), exit( 1 )
13![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
14
List MakeEmpty( List L );
15
int IsEmpty( List L );
16
int IsLast( Position P, List L );
17
Position Find( ElementType X, List L );
18
void Delete( ElementType X, List L );
19
Position FindPrevious( ElementType X, List L );
20
void Insert( ElementType X, List L, Position P );
21
void DeleteList( List L );
22
Position Header( List L );
23
Position First( List L );
24
Position Advance( Position P );
25
ElementType Retrieve( Position P );
26![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
27![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//* Place in the interface file */
28
struct Node
29![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
30
ElementType Element;
31
Position Next;
32
};
33![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
34
List MakeEmpty( List L )
35![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
45![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//* START: fig3_8.txt */
46![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//* Return true if L is empty */
47![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
48
int IsEmpty( List L )
49![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
50
return L->Next == NULL;
51
}
52![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//* END */
53![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
54![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//* START: fig3_9.txt */
55![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//* Return true if P is the last position in list L */
56![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//* Parameter L is unused in this implementation */
57![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
58
int IsLast( Position P, List L )
59![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
60
return P->Next == NULL;
61
}
62![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//* END */
63![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
64![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//* START: fig3_10.txt */
65![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//* Return Position of X in L; NULL if not found */
66![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
67
Position Find( ElementType X, List L )
68![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
69
Position P;
70![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
71![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//* 1*/ P = L->Next;
72![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//* 2*/ while( P != NULL && P->Element != X )
73![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//* 3*/ P = P->Next;
74![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
75![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//* 4*/ return P;
76
}
77![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//* END */
78![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
79![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//* START: fig3_11.txt */
80![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//* Delete from a list */
81![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//* Cell pointed to by P->Next is wiped out */
82![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//* Assume that the position is legal */
83![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//* Assume use of a header node */
84![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
85
void Delete( ElementType X, List L )
86![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
87
Position P, TmpCell;
88![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
89
P = FindPrevious( X, L );
90![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
91![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if( !IsLast( P, L ) ) /**//* Assumption of header use */
92![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ /**//* X is found; delete it */
93
TmpCell = P->Next;
94![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
P->Next = TmpCell->Next; /**//* Bypass deleted cell */
95
free( TmpCell );
96
}
97
}
98![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//* END */
99![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
100![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//* START: fig3_12.txt */
101![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//* If X is not found, then Next field of returned value is NULL */
102![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//* Assumes a header */
103![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
104
Position FindPrevious( ElementType X, List L )
105![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
106
Position P;
107![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
108![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//* 1*/ P = L;
109![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//* 2*/ while( P->Next != NULL && P->Next->Element != X )
110![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//* 3*/ P = P->Next;
111![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
112![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//* 4*/ return P;
113
}
114![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//* END */
115![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
116![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//* START: fig3_13.txt */
117![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//* Insert (after legal position P) */
118![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//* Header implementation assumed */
119![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//* Parameter L is unused in this implementation */
120![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
121
void Insert( ElementType X, List L, Position P )
122![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
123
Position TmpCell;
124![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
125![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//* 1*/ TmpCell = malloc( sizeof( struct Node ) );
126![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//* 2*/ if( TmpCell == NULL )
127![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//* 3*/ FatalError( "Out of space!!!" );
128![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
129![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//* 4*/ TmpCell->Element = X;
130![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//* 5*/ TmpCell->Next = P->Next;
131![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//* 6*/ P->Next = TmpCell;
132
}
133![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//* END */
134![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
135
#if 0
136![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//* START: fig3_14.txt */
137![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//* Incorrect DeleteList algorithm */
138![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
139
void DeleteList( List L )
140![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
141
Position P;
142![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
143![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//* 1*/ P = L->Next; /**//* Header assumed */
144![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//* 2*/ L->Next = NULL;
145![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//* 3*/ while( P != NULL )
146![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
147![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//* 4*/ free( P );
148![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//* 5*/ P = P->Next;
149
}
150
}
151![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//* END */
152
#endif
153![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
154![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//* START: fig3_15.txt */
155![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//* Correct DeleteList algorithm */
156![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
157
void DeleteList( List L )
158![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
159
Position P, Tmp;
160![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
161![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//* 1*/ P = L->Next; /**//* Header assumed */
162![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//* 2*/ L->Next = NULL;
163![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//* 3*/ while( P != NULL )
164![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
165![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//* 4*/ Tmp = P->Next;
166![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//* 5*/ free( P );
167![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//* 6*/ P = Tmp;
168
}
169
}
170![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//* END */
171![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
172
Position Header( List L )
173![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
174
return L;
175
}
176![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
177
Position First( List L )
178![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
179
return L->Next;
180
}
181![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
182
Position Advance( Position P )
183![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
184
return P->Next;
185
}
186![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
187
ElementType Retrieve( Position P )
188![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
189
return P->Element;
190
}
191![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)