1. 程式人生 > 實用技巧 >vs2019 常用資料結構 純C語言 標頭檔案實現 (持續更新改錯中)單鏈表,

vs2019 常用資料結構 純C語言 標頭檔案實現 (持續更新改錯中)單鏈表,

1.單鏈表:

 1 #pragma once
 2 #ifndef _List_H
 3 #include<stdio.h>
 4 #include<stdlib.h>
 5 #define ElementType int
 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 Rretrieve(Position P);
24 #endif // !_List_H 25 26 struct Node 27 { 28 ElementType Element; 29 Position Next; 30 }; 31 32 inline List MakeEmpty(List L) 33 { 34 return List(); 35 } 36 37 int IsEmpty(List L) 38 { 39 return L->Next == NULL; 40 } 41 42 int IsLast(Position P, List L) 43 { 44 return P->Next == NULL;
45 } 46 47 Position Find(ElementType X, List L) 48 { 49 Position P; 50 P = L->Next; 51 while (P != NULL && P->Element != X) { 52 P = P->Next; 53 } 54 return P; 55 } 56 57 void Delete(ElementType X, List L) 58 { 59 Position P, TmpCell = nullptr; 60 P = FindPrevious(X, L); 61 if (!IsLast(P, L)) { 62 TmpCell = P->Next; 63 P->Next = TmpCell->Next; 64 free(TmpCell); 65 } 66 } 67 68 Position FindPrevious(ElementType X, List L) 69 { 70 Position P; 71 P = L; 72 while (P->Next != NULL && P->Next->Element != X) { 73 P = P->Next; 74 } 75 return P; 76 } 77 78 void Insert(ElementType X, List L, Position P) 79 { 80 Position TmpCell; 81 TmpCell = (Node*)malloc(sizeof(struct Node)); 82 if (TmpCell == NULL) { 83 printf("fatal error:out of space!\n"); 84 exit(1); 85 } 86 TmpCell->Element = X; 87 } 88 89 void DeleteList(List L) 90 { 91 L = NULL; 92 free(L); 93 }

2.順序棧有頭結點

 1 #pragma once
 2 #ifndef _sqStack_h
 3 #include<stdio.h>
 4 #include<stdlib.h>
 5 #define StackSize 100
 6 typedef int ElementType;
 7 typedef struct {
 8     ElementType data[StackSize];
 9     int top;
10 }sqStack;
11 void InitStack(sqStack* s);
12 int IsEmpty(sqStack* s);
13 void PrintStack(sqStack* s);
14 int Push(sqStack* s, ElementType* val);
15 int Pop(sqStack* s, ElementType* val);
16 int GetTop(sqStack* s, ElementType* val);
17 int ClearStack(sqStack* s);
18 #endif // !_sqStack_h
19 void InitStack(sqStack* s)
20 {
21     s->top = -1;
22 }
23 
24 int IsEmpty(sqStack* s)
25 {
26     if (s->top == -1) {
27         return 1;
28     }
29     else return 0;
30 }
31 
32 void PrintStack(sqStack* s)
33 {
34     if (IsEmpty(s)) {
35         printf("Empty Stack!");
36     }
37     else {
38         for (int i = 0; i < (s->top) + 1; i++) {
39             printf("%d\t", s->data[i]);
40         }
41         printf("\n");
42     }
43 }
44 int Push(sqStack* s, ElementType* val)
45 {
46     if (s->top < StackSize - 1) {
47         s->top = s->top + 1;
48         s->data[s->top] = *val;
49         return 1;
50     }
51     else {
52         printf("full stack!");
53         exit(1);
54     }
55 }
56 
57 int Pop(sqStack* s, ElementType* val)
58 {
59     if (s->top == -1) {
60         printf("Empty Stack!");
61         exit(-1);
62     }
63     else {
64         *val = s->data[s->top];
65         s->top--;
66     }
67     return 1;
68 }
69 
70 int GetTop(sqStack* s, ElementType* val)
71 {
72     if (IsEmpty(s)) {
73         printf("Empty Stack!");
74         exit(-1);
75     }
76     else {
77         *val = s->data[s->top];
78     }
79 
80     return 1;
81 }
82 
83 int ClearStack(sqStack* s)
84 {
85     s->top = -1;
86     return 1;
87 }

3.鏈棧(無頭結點)

 1 #pragma once
 2 #ifndef _LStack_H
 3 #include<stdio.h>
 4 #include<stdlib.h>
 5 typedef int ElementType;
 6 typedef struct stnode {
 7     ElementType data;
 8     struct  stnode* next;
 9     size_t LenStack;
10 }Lstack;
11 
12 Lstack* LstackCreate();
13 void InitStack(Lstack* s);
14 int push(Lstack* s, ElementType val);
15 int pop(Lstack* s, ElementType* val);
16 int gettop(Lstack* s, ElementType* val);
17 int IsEmpty(Lstack* s);
18 void print(Lstack* s);
19 #endif // !_LStack_H
20 Lstack* LstackCreate()
21 {
22     Lstack* s = (Lstack*)malloc(sizeof(Lstack));
23     InitStack(s);
24     return s;
25 }
26 //帶頭結點的鏈棧
27 void InitStack(Lstack* s)
28 {
29     s->next = NULL;
30     s->LenStack = 0;
31 }
32 int push(Lstack* s, ElementType val)
33 {
34     Lstack* p;
35     p = LstackCreate();
36     p->data = val;
37     if (IsEmpty(s)) {
38         s = p;
39     }
40     else {
41         p->next = s;
42         s->next = p;
43     }
44     (s->LenStack)++;
45     return 1;
46 }
47 
48 int pop(Lstack* s, ElementType* val)
49 {
50     if (IsEmpty(s)) {
51         printf("Empty Stack!");
52         exit(1);
53     }
54     Lstack* tmp;
55     tmp = LstackCreate();
56     tmp = s->next;
57     *val = tmp->data;
58     s->next = tmp->next;
59     free(tmp);
60     return 1;
61 }
62 
63 int gettop(Lstack* s, ElementType* val)
64 {
65     if (IsEmpty(s)) {
66         printf("Empty Stack!");
67         exit(1);
68     }
69     *val = s->next->data;
70     return 1;
71 }
72 
73 int IsEmpty(Lstack* s)
74 {
75     if (s->LenStack == 0) {
76         return 1;
77     }
78     return 0;
79 }
80 
81 void print(Lstack* s)
82 {
83     ;//懶得寫了,也挺簡單的
84 }

4.鏈棧

 1 #pragma once
 2 #ifndef _LStack_H
 3 #include<stdio.h>
 4 #include<stdlib.h>
 5 typedef int ElementType;
 6 typedef struct stnode {
 7     ElementType data;
 8     struct  stnode* next;
 9     size_t LenStack;
10 }Lstack;
11 
12 Lstack* LstackCreate();
13 void InitStack(Lstack* s);
14 int push(Lstack* s, ElementType val);
15 int pop(Lstack* s, ElementType* val);
16 int gettop(Lstack* s, ElementType* val);
17 int IsEmpty(Lstack* s);
18 void print(Lstack* s);
19 #endif // !_LStack_H
20 Lstack* LstackCreate()
21 {
22     Lstack* s = (Lstack*)malloc(sizeof(Lstack));
23     InitStack(s);
24     return s;
25 }
26 //帶頭結點的鏈棧
27 void InitStack(Lstack* s)
28 {
29     s->next = NULL;
30     s->LenStack = 0;
31 }
32 int push(Lstack* s, ElementType val)
33 {
34     Lstack* p;
35     p = LstackCreate();
36     p->data = val;
37     if (IsEmpty(s)) {
38         s->next = p;
39     }
40     else {
41         p->next = s->next;
42         s->next = p;
43     }
44     (s->LenStack)++;
45     return 1;
46 }
47 
48 int pop(Lstack* s, ElementType* val)
49 {
50     if (IsEmpty(s)) {
51         printf("Empty Stack!");
52         exit(1);
53     }
54     Lstack* tmp;
55     tmp = LstackCreate();
56     tmp = s->next;
57     *val = tmp->data;
58     s->next = tmp->next;
59     free(tmp);
60     return 1;
61 }
62 
63 int gettop(Lstack* s, ElementType* val)
64 {
65     if (IsEmpty(s)) {
66         printf("Empty Stack!");
67         exit(1);
68     }
69     *val = s->next->data;
70     return 1;
71 }
72 
73 int IsEmpty(Lstack* s)
74 {
75     if (s->LenStack == 0) {
76         return 1;
77     }
78     return 0;
79 }
80 
81 void print(Lstack* s)
82 {
83     ;
84 }

5.順序佇列

 1 #pragma once
 2 #ifndef _sqQueue_h
 3 #include<stdio.h>
 4 #include<stdlib.h>
 5 #define MaxSize 20
 6 typedef int ElementType;
 7 typedef struct asqQueue {
 8     ElementType data[MaxSize];
 9     int front, rear;
10 }squeue;
11 
12 void Initqueue(squeue* q);
13 squeue* squeueCreate();
14 int IsEmpty(squeue q);
15 int EQueue(squeue* q, ElementType val);
16 int OQueue(squeue* q, ElementType* val);
17 int GetQhead(squeue* q, ElementType* val);
18 void ClearQueue(squeue* q);
19 
20 #endif // !_sqQueue_h
21 void Initqueue(squeue* q)
22 {
23     q->rear = 0;
24     q->front = 0;
25 }
26 squeue* squeueCreate()
27 {
28     squeue* q = (squeue*)malloc(sizeof(squeue));
29 
30     return q;
31 }
32 
33 int IsEmpty(squeue q)
34 {
35     if (q.rear == q.front) {
36         return 1;
37     }
38     return 0;
39 }
40 
41 inline int EQueue(squeue* q, ElementType val)
42 {
43     if (((q->rear) + 1) % MaxSize == q->front) {
44         printf("overflow queue!");
45         exit(1);
46     }
47     else {
48         q->rear = ((q->rear) + 1) % MaxSize;
49         q->data[q->rear] = val;
50     }
51 
52     return 1;
53 }
54 
55 inline int OQueue(squeue* q, ElementType* val)
56 {
57     if (q->front == q->rear) {
58         printf("Empty Queue!");
59         exit(1);
60     }
61     else {
62         q->front = ((q->front) + 1) % MaxSize;
63         *val = q->data[q->front];
64     }
65 
66     return 1;
67 }
68 
69 inline int GetQhead(squeue* q, ElementType* val)
70 {
71     if (q->front == q->rear) {
72         printf("Empty Queue!");
73         exit(1);
74     }
75     else {
76         *val = (q->data[q->front + 1]) % MaxSize;
77     }
78     return 1;
79 }
80 
81 inline void ClearQueue(squeue* q)
82 {
83     q->front = q->rear = 0;
84     printf("Clear Success!");
85 }

6.鏈式佇列

  1 #pragma once
  2 #ifndef _LQueue_H
  3 #include<stdio.h>
  4 #include<stdlib.h>
  5 typedef int QElementType;
  6 typedef int ElementType;
  7 typedef struct LQNode {
  8     QElementType data;
  9     struct LQNode* next;
 10 }LQNode, * QueuePtr;
 11 
 12 typedef struct {
 13     QueuePtr front;
 14     QueuePtr rear;
 15 }LQueue;
 16 
 17 void InitQueue(LQueue* LQ);
 18 void EQueue(LQueue* LQ, ElementType val);
 19 void OQueue(LQueue* LQ, ElementType* val);
 20 int IsEmpty(LQueue* LQ);
 21 int GetQhead(LQueue* LQ, ElementType* val);
 22 void ClearQueue(LQueue* LQ);
 23 
 24 #endif // !_LQueue_H
 25 void InitQueue(LQueue* LQ)
 26 {
 27     LQ->front = NULL;
 28     LQ->rear = NULL;
 29 }
 30 
 31 LQueue* LQueueCreate()
 32 {
 33     LQueue* LQ = (LQueue*)malloc(sizeof(LQueue));
 34     InitQueue(LQ);
 35     return LQ;
 36 }
 37 
 38 void EQueue(LQueue* LQ, ElementType val)
 39 {
 40     QueuePtr tmp;
 41     tmp = (QueuePtr)malloc(sizeof(LQNode));
 42     tmp->data = val;
 43     tmp->next = NULL;
 44     if (LQ->front == NULL && LQ->rear == NULL) {
 45         LQ->rear = tmp;
 46         LQ->front = tmp;
 47     }
 48     else {
 49         LQ->rear->next = tmp;
 50         LQ->rear = tmp;
 51     }
 52 }
 53 
 54 void OQueue(LQueue* LQ, ElementType* val)
 55 {
 56     QueuePtr tmp;
 57     if (IsEmpty(LQ)) {
 58         printf("Empty LinkQueue!");
 59         exit(1);
 60     }
 61     else {
 62         tmp = LQ->front;
 63         *val = LQ->front->data;
 64         if (LQ->front == LQ->rear) {
 65             LQ->front = NULL;
 66             LQ->rear = NULL;
 67         }
 68         else {
 69             LQ->front = LQ->front->next;
 70         }
 71     }
 72     free(tmp);
 73 }
 74 
 75 inline int IsEmpty(LQueue* LQ)
 76 {
 77     if (LQ->front == NULL && LQ->rear == NULL) {
 78         return 1;
 79     }
 80     return 0;
 81 }
 82 
 83 inline int GetQhead(LQueue* LQ, ElementType* val)
 84 {
 85     if (IsEmpty(LQ)) {
 86         printf("Empty LQueue!");
 87         exit(1);
 88     }
 89     else {
 90         *val = LQ->front->data;
 91     }
 92     return 0;
 93 }
 94 
 95 inline void ClearQueue(LQueue* LQ)
 96 {
 97     QueuePtr tmp1, tmp2;
 98     tmp1 = LQ->front;
 99     while (tmp1) {
100         tmp2 = tmp1;
101         free(tmp2);
102         tmp1 = tmp1->next;
103     }
104     LQ->front = NULL;
105     LQ->rear = NULL;
106 }