链表ADT的实现

list.h文件

 1         /*链表的类型声明*/
 2 
 3         typedef int ElementType;
 4 
 5 /* START: fig3_6.txt */
 6         #ifndef _List_H
 7         #define _List_H
 8 
 9         struct Node;
10         typedef struct Node *PtrToNode;
11         typedef PtrToNode List;
12         typedef PtrToNode Position;
13 
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 
27         #endif    /* _List_H */
28 /* END */

 

list.c文件

  1         #include "list.h"
  2         #include <stdlib.h>
  3         #include "fatal.h"
  4 
  5         /* Place in the interface file */
  6         struct Node
  7         {
  8             ElementType Element;
  9             Position    Next;
 10         };
 11 
 12         /*创建空链表*/
 13         List
 14         MakeEmpty( List L )
 15         {
 16             if( L != NULL )
 17                 DeleteList( L );
 18             L = malloc( sizeof( struct Node ) );
 19             if( L == NULL )
 20                 FatalError( "Out of memory!" );
 21             L->Next = NULL;
 22             return L;
 23         }
 24 
 25 /* START: fig3_8.txt */
 26         /* Return true if L is empty */
 27 
 28         /*判断链表是否为空*/
 29         int
 30         IsEmpty( List L )
 31         {
 32             return L->Next == NULL;
 33         }
 34 /* END */
 35 
 36 /* START: fig3_9.txt */
 37         /* Return true if P is the last position in list L */
 38         /* Parameter L is unused in this implementation */
 39 
 40         /*判断是否为链表的最后元素*/
 41         int IsLast( Position P, List L ) 
 42         {
 43             return P->Next == NULL;
 44         }
 45 /* END */
 46 
 47 /* START: fig3_10.txt */
 48         /* Return Position of X in L; NULL if not found */
 49 
 50         /*找某元素的位置*/
 51         Position
 52         Find( ElementType X, List L )
 53         {
 54             Position P;
 55 
 56 /* 1*/      P = L->Next;
 57 /* 2*/      while( P != NULL && P->Element != X )
 58 /* 3*/          P = P->Next;
 59 
 60 /* 4*/      return P;
 61         }
 62 /* END */
 63 
 64 /* START: fig3_11.txt */
 65         /* Delete from a list */
 66         /* Cell pointed to by P->Next is wiped out */
 67         /* Assume that the position is legal */
 68         /* Assume use of a header node */
 69 
 70         /*删除链表某个元素*/
 71         void
 72         Delete( ElementType X, List L )
 73         {
 74             Position P, TmpCell;
 75 
 76             P = FindPrevious( X, L );
 77 
 78             if( !IsLast( P, L ) )  /* Assumption of header use */
 79             {                      /* X is found; delete it */
 80                 TmpCell = P->Next;
 81                 P->Next = TmpCell->Next;  /* Bypass deleted cell */
 82                 free( TmpCell );
 83             }
 84         }
 85 /* END */
 86 
 87 /* START: fig3_12.txt */
 88         /* If X is not found, then Next field of returned value is NULL */
 89         /* Assumes a header */
 90 
 91         /*找某个元素的前一个元素*/
 92         Position
 93         FindPrevious( ElementType X, List L )
 94         {
 95             Position P;
 96 
 97 /* 1*/      P = L;
 98 /* 2*/      while( P->Next != NULL && P->Next->Element != X )
 99 /* 3*/          P = P->Next;
100 
101 /* 4*/      return P;
102         }
103 /* END */
104 
105 /* START: fig3_13.txt */
106         /* Insert (after legal position P) */
107         /* Header implementation assumed */
108         /* Parameter L is unused in this implementation */
109 
110         /*在某个位置后面插入一个元素*/
111         void
112         Insert( ElementType X, List L, Position P )
113         {
114             Position TmpCell;
115 
116 /* 1*/      TmpCell = malloc( sizeof( struct Node ) );
117 /* 2*/      if( TmpCell == NULL )
118 /* 3*/          FatalError( "Out of space!!!" );
119 
120 /* 4*/      TmpCell->Element = X;
121 /* 5*/      TmpCell->Next = P->Next;
122 /* 6*/      P->Next = TmpCell;
123         }
124 /* END */
125 
126 #if 0
127 /* START: fig3_14.txt */
128         /* Incorrect DeleteList algorithm */
129 
130         void
131         DeleteList( List L )
132         {
133             Position P;
134 
135 /* 1*/      P = L->Next;  /* Header assumed */
136 /* 2*/      L->Next = NULL;
137 /* 3*/      while( P != NULL )
138             {
139 /* 4*/          free( P );
140 /* 5*/          P = P->Next;
141             }
142         }
143 /* END */
144 #endif
145 
146 /* START: fig3_15.txt */
147         /* Correct DeleteList algorithm */
148 
149         /*删除链表*/
150         void
151         DeleteList( List L )
152         {
153             Position P, Tmp;
154 
155 /* 1*/      P = L->Next;  /* Header assumed */
156 /* 2*/      L->Next = NULL;
157 /* 3*/      while( P != NULL )
158             {
159 /* 4*/          Tmp = P->Next;
160 /* 5*/          free( P );
161 /* 6*/          P = Tmp;
162             }
163         }
164 /* END */
165 
166         /*返回头结点*/
167         Position
168         Header( List L )
169         {
170             return L;
171         }
172 
173         /*返回第一个节点*/
174         Position
175         First( List L )
176         {
177             return L->Next;
178         }
179         /*返回某节点的下一个节点*/
180         Position
181         Advance( Position P )
182         {
183             return P->Next;
184         }
185 
186         /*返回某节点的值*/
187         ElementType
188         Retrieve( Position P )
189         {
190             return P->Element;
191         }

 

posted @ 2015-12-03 19:05  樱风凛  阅读(1410)  评论(0编辑  收藏  举报