1.list.h
typedef int ElementType; #ifndef _List_H struct Node; typedef struct Node *PtrNode; typedef PtrNode List; typedef PtrNode Position; List MakeEmpty(List L); int IsEmpty(List L); int IsLast(Position P, List L); Position Find(ElementType X, List L); void Delete(ElementType X, List L); Position FindPrevious(ElementType X, List L); void Insert(ElementType X, List L, Position P); void DeleteList(List L); Position Header(List L); Position First(List L); Position Advance(Position P); ElementType Retrive(Position P); #endif
2. fatal.h
#include <stdio.h> #include <stdlib.h> #define Error( Str ) FatalError( Str ) #define FatalError( Str ) fprintf( stderr, "%s ", Str ), getchar(); exit( 1 )
3.list.c
#include "list.h" #include <stdio.h> #include "fatal.h" struct Node { ElementType Element; Position Next; }; Position Header(List L) { return L; } Position First(List L) { return L->Next; } Position Advance(Position P) { return P->Next; } ElementType Retrieve(Position P) { return P->Element; } int IsEmpty(List L) { return L->Next == NULL; } int IsLast(Position P, List L) { return P->Next == NULL; } Position Find(ElementType X, List L) { Position P; P = L->Next; while (P != NULL && P->Element != X) P = P->Next; return P; } void Delete(ElementType X, List L) { Position P, TmpCell; P = FindPrevious(X, L); if (!IsLast(P,L)) { TmpCell = P->Next; P->Next = TmpCell->Next; free(TmpCell); } } Position FindPrevious(ElementType X, List L) { Position P; P = L; while (P->Next != NULL && P->Next->Element != X) P = P->Next; return P; } void Insert(ElementType X, List L, Position P) { Position TmpCell; TmpCell = malloc(sizeof(struct Node)); if (TmpCell == NULL) { FatalError("Out of Space!!!"); } TmpCell->Element = X; TmpCell->Next = P->Next; P->Next = TmpCell; } List MakeEmpty(List L) { if (L!= NULL) { DeleteList(L); } L = malloc(sizeof(struct Node)); if (L == NULL) { FatalError("out of space!!!"); } L->Next = NULL; return L; } void DeleteList(List L) { Position P, TmpCell; P = L->Next; L->Next = NULL; while (P != NULL) { TmpCell = P->Next; free(P); P = TmpCell; } } ElementType MaxValue(List L) { ElementType MaxData = 0; Position P = L->Next; while (P != NULL) { if (MaxData < P->Element) { MaxData = P->Element; } } return MaxData; }
4.testlist.c
#include "list.h" #include <stdio.h> #include <stdlib.h>//system("pause"); void PrintList(const List L) { Position P = Header(L); if (IsEmpty(L)) printf("Empty list "); else { do { P = Advance(P); printf("%d ", Retrieve(P)); } while (!IsLast(P, L)); printf(" "); } } int mainTest() { List L; Position P; int i; L = MakeEmpty(NULL); P = Header(L); PrintList(L); for (i = 0; i < 10; i++) { Insert(i, L, P); PrintList(L); P = Advance(P); } for (i = 0; i < 10; i += 2) Delete(i, L); for (i = 0; i < 10; i++) { if ((i % 2 == 0) == (Find(i, L) != NULL)) printf("Find fails "); printf("%d %% 2 =%d %d ", i, i % 2, Find(i, L) != NULL); } printf("Finished deletions "); PrintList(L); DeleteList(L); system("pause"); return 0; } int main() { List L; Position P; int i; L = MakeEmpty(NULL); P = Header(L); PrintList(L); int data = 0; for (i = 0; i < 10; i++) { data = rand() % 1000; Insert(data, L, P); PrintList(L); P = Advance(P); } printf("Finished deletions "); PrintList(L); DeleteList(L); PrintList(L); system("pause"); return 0; }