這個鏈表是帶有表頭的單鏈表。實現鏈表的一些規範操作,初始化,插入,刪除等。包括兩個頭文件list.h,fatal.h,庫函數list.c,測試函數testlist.c。頭文件放的都是函數聲明,庫函數list.c放的的函數的定義。 頭文件list.h 頭文件fatal.h: 庫函數list.c: 測試 ...
這個鏈表是帶有表頭的單鏈表。實現鏈表的一些規範操作,初始化,插入,刪除等。包括兩個頭文件list.h,fatal.h,庫函數list.c,測試函數testlist.c。頭文件放的都是函數聲明,庫函數list.c放的的函數的定義。
頭文件list.h
1 typedef int ElementType; 2 #ifndef _List_H//如果沒有編譯過 3 struct Node; 4 typedef struct Node *PtrToNode; 5 typedef PtrToNode List; 6 typedef PtrToNode Position; 7 8 List MakeEmpty(List L); 9 void DeleteList(List L); 10 int IsEmpty(List L); 11 int IsLast(Position P, List L); 12 Position Find(ElementType X, List L); 13 void Delete(ElementType X, List L); 14 Position FindPrevious(ElementType X, List L); 15 void Insert(ElementType X, List L,Position P); 16 Position Header(List L); 17 Position First(List L); 18 Position Advance(Position P); 19 ElementType Retrieve(Position P); 20 void PrintList(const List L); 21 void PrintLots(List L, List P); 22 void SwapWithNext(Position BeforeP, List L); 23 List IntersectList(List L, List P); 24 List UnionList(Position L, Position P); 25 #endif // !_List_H
頭文件fatal.h:
1 #include<stdio.h> 2 #include<stdlib.h> 3 #define Error(Str) FatalError(Str) 4 #define FatalError(Str) fprintf(stderr,"%s\n",Str),exit(1);
庫函數list.c:
//引用頭文件 #include "list.h" #include<stdlib.h> #include "fatal.h" //結構體定義 struct Node { ElementType Element; Position Next; }; //初始化鏈表 List MakeEmpty(List L) { if (L != NULL) DeleteList(L);//如果鏈表非空,則刪除鏈表 L = malloc(sizeof(struct Node)); if (L == NULL) FatalError("Out of memory!"); L->Next = NULL; return L; } //刪除鏈表 void DeleteList(List L) { Position P, Temp; P = L->Next; L->Next = NULL; while (P != NULL) { Temp = P->Next; free(P); P = Temp; } } //判斷鏈表是否為空 int IsEmpty(List L) { return L->Next==NULL; } //判斷當前指針P是否指向鏈表最後一個元素 int IsLast(Position P, List L) { return P->Next==NULL; } //return Position of X in L;NULL if not found Position Find(ElementType X, List L) { Position P; P = L->Next; while (P != NULL && P->Element != X) P = P->Next; return P; } //刪除鏈表中的元素X,若返回NULL,說明在鏈表中沒找到元素X void Delete(ElementType X, List L) { Position P, TempCell; P = FindPrevious(X, L); if (!IsLast(P, L))//當P不是尾針,說明找到了 { TempCell = P->Next; P->Next = TempCell->Next; free(TempCell); TempCell = NULL; } } //如果返回的P指向最後一個元素,說明沒有找到,1==IsLast(P,L) Position FindPrevious(ElementType X, List L) { Position P; P = L; while (P->Next != NULL&&P->Next->Element != X) P = P->Next; return P; } //插入元素X到位置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; } //獲取鏈表頭 Position Header(List L) { return L; } //獲取鏈表第一個元素的位置 Position First(List L) { return L->Next; } //獲取位置P的下一個位置 Position Advance(Position P) { return P->Next; } //提取位置P處結構裡面的值 ElementType Retrieve(Position P) { return P->Element; } //列印鏈表 void PrintList(const List L) { Position P=Header(L); if (IsEmpty(L)) printf("Empty list\n"); else { do { P = Advance(P); printf("%d ", Retrieve(P)); } while (!IsLast(P, L)); printf("\n"); } } //列印鏈表L中那些由P所指定的位置上的元素。例如P=1,3,4,6,將L //中的第1,第3,第4,第6個元素列印出來 void PrintLots(List L, List P) { int count = 1; Position Lpos, Ppos; Lpos = First(L); Ppos = First(P); while (Lpos != NULL&&Ppos != NULL) { if ( Ppos->Element == count++) { printf("%d ", Ppos->Element); Ppos = Advance(Ppos); } Lpos = Advance(Lpos); } } //通過只調整指針來交換兩個相鄰的元素,BeforeP是要調換兩個元素的前一 //個指針 void SwapWithNext(Position BeforeP, List L) { Position P, AfterP; if (BeforeP != NULL) { P = Advance(BeforeP); if (P != NULL) { AfterP = Advance(P); if (AfterP != NULL) { P->Next = AfterP->Next; BeforeP->Next = AfterP; AfterP->Next = P; } } } } //求兩個鏈表的交集 List IntersectList(List L1, List L2) { List ResultList; Position L1Pos, L2Pos, ResultPos; ResultList = MakeEmpty(NULL); L1Pos = First(L1); L2Pos = First(L2); ResultPos = Header(ResultList); while (L1Pos!=NULL&&L2Pos!=NULL) { if (L1Pos->Element < L2Pos->Element) L1Pos = Advance(L1Pos); else if (L1Pos->Element > L2Pos->Element) L2Pos = Advance(L2Pos); else { Insert(L1Pos->Element, ResultList, ResultPos); ResultPos= Advance(ResultPos); L1Pos = Advance(L1Pos); L2Pos = Advance(L2Pos); } } return ResultList; } //求兩個鏈表的並集 List UnionList(Position L1, Position L2) { List ResultList; ElementType InsertElement; Position L1Pos, L2Pos, ResultPos; ResultList = MakeEmpty(NULL); L1Pos = First(L1); L2Pos = First(L2); ResultPos = Header(ResultList); while (L1Pos != NULL&&L2Pos != NULL) { if (L1Pos->Element < L2Pos->Element) { InsertElement = L1Pos->Element; L1Pos = Advance(L1Pos); } else if (L1Pos->Element > L2Pos->Element) { InsertElement = L2Pos->Element; L2Pos = Advance(L2Pos); } else { InsertElement = L1Pos->Element; L1Pos = Advance(L1Pos); L2Pos = Advance(L2Pos); } Insert(InsertElement, ResultList, ResultPos); ResultPos = Advance(ResultPos); } while (L1Pos != NULL) { Insert(L1Pos->Element, ResultList, ResultPos); ResultPos = Advance(ResultPos); L1Pos = Advance(L1Pos); } while (L2Pos != NULL) { Insert(L2Pos->Element, ResultList, ResultPos); ResultPos = Advance(ResultPos); L2Pos = Advance(L2Pos); } return ResultList; }
測試函數testlist.c
1 #include<stdlib.h> 2 #include "list.h" 3 main() 4 { 5 List L,L1; 6 Position P,P1; 7 int i; 8 L = MakeEmpty(NULL); 9 P = Header(L); 10 PrintList(L); 11 12 L1 = MakeEmpty(NULL); 13 P1 = Header(L1); 14 PrintList(L1); 15 16 17 for (i = 0; i < 50; i+=2) 18 { 19 Insert(i, L, P); 20 //PrintList(L); 21 P = Advance(P); 22 } 23 PrintList(L); 24 printf("\n"); 25 for (i = 1; i < 100; i+=3) 26 { 27 Insert(i, L1, P1); 28 //PrintList(L); 29 P1 = Advance(P1); 30 } 31 PrintList(L1); 32 printf("\n"); 33 34 PrintList(IntersectList(L, L1)); 35 printf("\n"); 36 PrintList(UnionList(L, L1)); 37 //PrintLots(L, L1); 38 39 //SwapWithNext(L, L);//換頭兩個元素 40 41 //for (i = 0; i < 10; i += 2) 42 // Delete(i, L); 43 //for (i = 0; i < 10; i++) 44 //{ 45 // if ((i % 2 == 0) == (Find(i, L) != NULL)) 46 // printf("Find fails\n"); 47 //} 48 //printf("Finished deletions\n"); 49 //PrintList(L); 50 DeleteList(L); 51 DeleteList(L1); 52 return 0; 53 }