19:28:16 2019-08-18
A little earlier today.
Doubly linked list
DList.h
1 #ifndef _DLIST_H 2 #define _DLIST_H 3 #define len sizeof(struct Node) 4 #include<malloc.h> 5 struct Node; 6 typedef struct Node* PtrToNode; 7 typedef PtrToNode DList; 8 typedef PtrToNode DPosition; 9 DList MakeEmety(DList L); 10 int IsEmpty(DList L); 11 int IsLast(DPosition P, DList L); 12 DPosition Find(int Element, DList L); 13 void Delete(int Element, DList L); 14 DPosition FindPrevious(int Element, DList L); 15 void InsertBefore(int Element,DList L, DPosition P); 16 void InsertAfter(int Element, DList L, DPosition P); 17 void DeleteList(DList L); 18 DPosition Header(DList L); 19 DPosition First(DList L); 20 #endif // !_DLIST_H
DList.cpp
1 #include"DList.h" 2 3 struct Node 4 { 5 int Element; 6 DPosition Prior; 7 DPosition Next; 8 }; 9 10 DList MakeEmety(DList L) 11 { 12 L = (DList)malloc(len); 13 L->Prior = NULL; 14 L->Next = NULL; 15 return L; 16 } 17 18 int IsEmpty(DList L) 19 { 20 return L->Next == NULL; 21 } 22 23 int IsLast(DPosition P, DList L) 24 { 25 return P->Next == NULL; 26 } 27 28 DPosition Find(int Element, DList L) 29 { 30 DPosition P = L->Next; 31 while (P!=NULL) 32 { 33 if (P->Element == Element) 34 return P; 35 else 36 P = P->Next; 37 } 38 return NULL; 39 } 40 41 void Delete(int Element, DList L) 42 { 43 DPosition P = FindPrevious(Element, L); 44 P->Next = P->Next->Next; 45 P->Next->Prior = P; 46 } 47 48 DPosition FindPrevious(int Element, DList L) 49 { 50 DPosition P = L; 51 while (P->Next!=NULL) 52 { 53 if (P->Next->Element==Element) 54 return P; 55 else 56 P = P->Next; 57 } 58 } 59 60 void InsertBefore(int Element, DList L, DPosition P) 61 { 62 DPosition P1 = (DPosition)malloc(len); 63 P1->Element=Element; 64 P1->Prior = P->Prior; 65 P1->Next = P; 66 the P-> Prior-> = the Next Pl; 67 the P-> Prior, = Pl; 68 } 69 70 void the InsertAfter ( int the Element, DLIST L, DPosition P) 71 is { 72 DPosition Pl = (DPosition) the malloc (len); 73 is P1-> = the Element the Element; 74 P1-> Prior, = P; 75 P1-> = the Next P1-> the Next; 76 the P-> = the Next Pl; 77 P1-> next-> Prior, Pl =; // if it is inserted at the end of this sentence should be deleted 78 } 79 80 void DeleteList(DList L) 81 { 82 DPosition P1, P2; 83 P1 = P2 = L->Next; 84 while (P2 != NULL) 85 { 86 P2 = P1->Next; 87 free(P1); 88 P1 = P2; 89 } 90 } 91 92 DPosition Header(DList L) 93 { 94 return L; 95 } 96 97 DPosition First(DList L) 98 { 99 return L->Next; 100 }
Single-chain loop is a pointer to the last element in the first node
Circular doubly linked list is the last element in the next pointer points to the first node, prior to the first node pointer pointing to a last node
Stack list implementation
LIFO (Last In First Out)
Stack.h
1 #ifndef _STACK_H 2 #define _STACK_H 3 #define len sizeof(struct Node) 4 struct Node; 5 typedef struct Node* PtrToNode; 6 typedef PtrToNode Stack; 7 int IsEmpty(Stack S); 8 Stack CreatStack(); 9 void DisposeStack(Stack S); 10 void MakeEmpty(Stack S); 11 void Push(int Element, Stack S); 12 int Top(Stack S); 13 void Pop(Stack S); 14 #endif // !_STACK_H
Stack.cpp
1 #include"Stack.h" 2 #include<stdio.h> 3 #include<malloc.h> 4 struct Node 5 { 6 int Element; 7 PtrToNode Next; 8 }; 9 10 int IsEmpty(Stack S) 11 { 12 return S->Next == NULL; 13 } 14 15 Stack CreatStack() 16 { 17 Stack S = (Stack)malloc(len); 18 S->Next = NULL; 19 MakeEmpty(S); 20 return S; 21 } 22 23 void DisposeStack(Stack S) 24 { 25 while (!IsEmpty(S)) 26 { 27 Pop(S); 28 } 29 free(S); 30 } 31 32 void MakeEmpty(Stack S) 33 { 34 while (!IsEmpty(S)) 35 { 36 Pop(S); 37 } 38 } 39 40 void Push(int Element, Stack S) 41 { 42 PtrToNode P = (PtrToNode)malloc(len); 43 P->Element = Element; 44 P->Next = S->Next; 45 S->Next = P; 46 } 47 48 int Top(Stack S) 49 { 50 if (IsEmpty(S)) 51 { 52 printf("No Element"); 53 return -1; 54 } 55 return S->Next->Element; 56 } 57 58 void Pop(Stack S) 59 { 60 PtrToNode P=S->Next; 61 S->Next = S->Next->Next; 62 free(P); 63 }
Achieve the queue list
Queue.h
1 #ifndef _QUEUE_H 2 #define _QUEUE_H 3 #include<malloc.h> 4 #define len sizeof(Node) 5 #define length sizeof(Queue) 6 struct Node; 7 struct Queue; 8 typedef struct Node* PtrToNode; 9 typedef struct Queue* HQueue; 10 int IsEmpty(HQueue H); 11 HQueue CreatQueue(); 12 void DiposeQueue(HQueue H); 13 void Enqueue(int Element, HQueue H); 14 void Dequeue(HQueue H); 15 #endif // !_QUEUE_H
Queue.cpp
1 #include<stdio.h> 2 #include"Queue.h" 3 4 struct Node 5 { 6 int Element; 7 PtrToNode Next; 8 }; 9 struct Queue 10 { 11 PtrToNode front; 12 PtrToNode rear; 13 }; 14 15 int IsEmpty(HQueue H) 16 { 17 return H->rear == NULL || H->front == NULL; 18 } 19 20 HQueue CreatQueue() 21 { 22 HQueue H= (HQueue)malloc(length); 23 H->front = NULL; 24 H->rear = NULL; 25 } 26 27 void DiposeQueue(HQueue H) 28 { 29 while (!IsEmpty(H)) 30 { 31 Queue(H); 32 } 33 free(H); 34 } 35 36 void Enqueue(int Element, HQueue H) 37 { 38 PtrToNode P = (PtrToNode)malloc(len); 39 P->Element = Element; 40 if (IsEmpty(H)) 41 { 42 H->front = P; 43 H->rear = P; 44 } 45 else 46 { 47 H->rear->Next = P; 48 H->rear = P; 49 } 50 } 51 52 void Dequeue(HQueue H) 53 { 54 PtrToNode P; 55 if(The IsEmpty (H)) 56 is return ; 57 is the else 58 { 59 P = H-> Front; 60 / * H-> H-Front => front-> the Next; // only one element in the queue when the time for a special treatment 61 Free (P); * / 62 is } 63 is IF (H-> H-Front ==> REAR) 64 { 65 Free (P); 66 H-> H-Front => REAR = NULL; 67 } 68 the else 69 { 70 H-> H-Front => front-> the Next; 71 is Free(P); 72 } 73 }