码迷,mamicode.com
首页 > 其他好文 > 详细

链表ADT的实现

时间:2015-12-03 20:54:42      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:

list.h文件

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

 

链表ADT的实现

标签:

原文地址:http://www.cnblogs.com/fazero/p/5017289.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!