Basic operations for a single-chain table-insert, delete, commit, and exchange of Adjacent Elements-basic operations for a single-chain table
This linked list is a single-chain table with a header. Implement some standard operations, initialization, insertion, and deletion of the linked list. Includes two header files: list. h, fatal. h, library function list. c, and test function testlist. c. The header files are all stored in the function declaration, which defines the function of the library function list. c.
Header file list. h
1 typedef int ElementType; 2 # ifndef _ List_H // If 3 struct Node has not been compiled; 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 vo Id 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
Header file 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);
Library Function list. c:
// Reference the header file # include "list. h "# include <stdlib. h> # include "fatal. h "// struct definition struct Node {ElementType Element; Position Next ;}; // initialize List MakeEmpty (List L) {if (L! = NULL) DeleteList (L); // if the linked list is not empty, delete the linked list L = malloc (sizeof (struct Node); if (L = NULL) fatalError ("Out of memory! "); L-> Next = NULL; return L;} // Delete the linked table void DeleteList (List L) {Position P, Temp; P = L-> Next; l-> Next = NULL; while (P! = NULL) {Temp = P-> Next; free (P); P = Temp ;}}// checks whether the linked List is empty. int IsEmpty (List L) {return L-> Next = NULL;} // determines whether the current pointer P points to the last element of the linked List, int IsLast (Position P, List L) {return P-> Next = NULL;} // return Position of X in L; NULL if not foundPosition Find (ElementType X, List L) {Position P; P = L-> Next; while (P! = NULL & P-> Element! = X) P = P-> Next; return P;} // Delete element X in the linked list. If NULL is returned, the Xvoid Delete (ElementType X, list L) {Position P, TempCell; P = FindPrevious (X, L); if (! IsLast (P, L) // when P is not the last needle, {TempCell = P-> Next; P-> Next = TempCell-> Next; free (TempCell); TempCell = NULL ;}// if the returned P points to the last element, it indicates that it is not found. 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;} // Insert element X to 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;} // get the Position Header (List L) of the linked List Header) {return L;} // obtain the Position of the First element in the linked List. Position First (List L) {return L-> Next ;} // obtain Position P Next Position Advance (Position P) {return P-> Next;} // extract the value ElementType Retrieve (Position P) in the structure of Position P) {return P-> Element;} // print the linked table 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") ;}// print the elements at the position specified by P in the linked list L. For example, P = 1st, 3rd, and 4th elements in l // are printed out. 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) ;}// two adjacent elements are exchanged by adjusting only the pointer. BeforeP is the first/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 ;}}}} // calculate the intersection 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;} // calculate the Union List UnionList (Position L1, Position L2) {List ResultList; ElementType InsertElement; Position L1Po S, 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 ;}
Test function 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 // Pr IntList (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); // the first two elements 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}