顺序循环队列和链式存储队列(带头结点和不带头结点)

1.顺序存储的循环队列

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <stdbool.h>
 4 
 5 typedef int ElementType;
 6 typedef int Position;  
 7 typedef struct QNode* PtrToNode;
 8 struct QNode {
 9     ElementType *Data;
10     Position Front, Rear;
11     int MaxSize;
12     
13 };
14 typedef PtrToNode Queue;
15 
16 
17 //创建一个空队列
18 Queue CreateQueue(int MaxSize)
19 {
20     Queue Q = (Queue)malloc(sizeof(struct QNode));
21     Q->Data = (ElementType*)malloc(sizeof(ElementType) * MaxSize);
22     Q->Front = Q->Rear = 0;
23     Q->MaxSize =  MaxSize;
24     return Q;
25 }
26 
27 //判断队列是否已满
28 bool IsFull(Queue Q)
29 {
30     return ((Q->Rear + 1) % Q->MaxSize == Q->Front);
31 }
32 
33 bool AddQ(Queue Q, ElementType X)
34 {
35     if (IsFull(Q))
36     {
37         printf("The queue is full!\n");
38         return false;
39     }
40     else
41     {
42         Q->Data[Q->Rear] = X;
43         Q->Rear = (Q->Rear + 1) % Q->MaxSize;
44         
45         return true;    
46     }
47 }
48 
49 //判断队列是否为空
50 bool IsEmpty(Queue Q)
51 {
52     return Q->Front == Q->Rear;
53 }
54 
55 //删除一个元素
56 ElementType DeleteQ(Queue Q)
57 {
58     ElementType elem;
59     if (IsEmpty(Q))
60     {
61         printf("The queue is empty!\n");
62         return -999;
63     }
64     else
65     {
66         elem = Q->Data[Q->Front];
67         Q->Front = (Q->Front + 1) % Q->MaxSize;
68         return elem;
69     }
70 }
71 
72 int main()
73 {
74     Queue Q = CreateQueue(10);
75 
76     for (int i = 0; i < 10; i++)
77         AddQ(Q, i);
78     for (int i = 0; i < 10; i++)
79         printf("%d ", DeleteQ(Q));
80     AddQ(Q, 10);
81     printf("%d ", DeleteQ(Q));
82     return 0;
83 }

 

 

2.不带头结点的链式存储队列

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <stdbool.h>
 4 
 5 typedef int ElementType;
 6 typedef struct Node* PtrToNode;
 7 struct Node {        //队列中的结点
 8     ElementType Data;
 9     PtrToNode Next;
10 };
11 
12 typedef struct QNode* PtrToQNode;
13 typedef struct Node* Position;
14 struct QNode {
15     Position Front, Rear;    //队列的头尾指针
16     //int MaxSize;            //队列的最大容量
17 };
18 
19 typedef PtrToQNode Queue;
20 
21 Queue CreateQueue()
22 {
23     Queue Q = (Queue)malloc(sizeof(struct QNode));
24     Q->Front = Q->Rear = NULL;
25     //Q->MaxSize = MaxSize;
26     return Q;
27 }
28 
29 void InsertQ(Queue Q, ElementType X)
30 {
31     PtrToNode cell = (PtrToNode)malloc(sizeof(struct Node));
32     cell->Data = X;
33     cell->Next = NULL;
34     if (!Q->Front)
35     {
36         Q->Front = Q->Rear = cell;
37     }
38     else
39     {
40         Q->Rear->Next = cell;
41         Q->Rear = cell;
42     }
43 }
44 
45 bool IsEmpty(Queue Q)
46 {
47     return (Q->Front == NULL);
48 }
49 
50 ElementType DeleteQ(Queue Q)
51 {
52     Position FrontCell;
53     ElementType FrontElem;
54     if (IsEmpty(Q))
55     {
56         printf("The queue is empty!\n");
57         return -999;
58     }
59     else
60     {
61         FrontCell = Q->Front;
62         if (Q->Front == Q->Rear)
63             Q->Front = Q->Rear = NULL;
64         else
65         Q->Front = Q->Front->Next;
66         
67         
68         FrontElem = FrontCell->Data;
69         free(FrontCell);
70         return FrontElem;
71     }
72 }
73 
74 int main()
75 {
76     Queue Q = CreateQueue();
77     for (int i = 0; i < 10; i++)
78         InsertQ(Q, i);
79     for (int i = 0; i < 10; i++)
80         printf("%d ", DeleteQ(Q));
81     DeleteQ(Q);
82 
83     return 0;
84 }

3.带头结点的链式存储队列

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <stdbool.h>
 4 
 5 typedef int ElementType;
 6 typedef struct Node* PtrToNode;
 7 struct Node {        //队列中的结点
 8     ElementType Data;
 9     PtrToNode Next;
10 };
11 
12 typedef struct QNode* PtrToQNode;
13 typedef struct Node* Position;
14 struct QNode {
15     Position Front, Rear;    //队列的头尾指针
16     //int MaxSize;            //队列的最大容量
17 };
18 
19 typedef PtrToQNode Queue;
20 
21 Queue CreateQueue()
22 {
23     Queue Q = (Queue)malloc(sizeof(struct QNode));
24     Q->Front = Q->Rear = (PtrToNode)malloc(sizeof(struct Node));
25     Q->Front->Next = NULL;
26     //Q->MaxSize = MaxSize;
27     return Q;
28 }
29 
30 void InsertQ(Queue Q, ElementType X)
31 {
32     PtrToNode cell = (PtrToNode)malloc(sizeof(struct Node));
33     cell->Data = X;
34     cell->Next = NULL;
35     Q->Rear->Next = cell;
36     Q->Rear = cell;
37 }
38 
39 bool IsEmpty(Queue Q)
40 {
41     return (Q->Front->Next == NULL);
42 }
43 
44 ElementType DeleteQ(Queue Q)
45 {
46     Position FrontCell;
47     ElementType FrontElem;
48     if (IsEmpty(Q))
49     {
50         printf("The queue is empty!\n");
51         return -999;
52     }
53     else
54     {
55         FrontCell = Q->Front->Next;
56         if (Q->Rear == Q->Front->Next)
57         {
58             Q->Rear = Q->Front;
59             Q->Front->Next = NULL;
60         }
61         else
62             Q->Front->Next = FrontCell->Next;
63 
64 
65         FrontElem = FrontCell->Data;
66         free(FrontCell);
67         return FrontElem;
68     }
69 }
70 
71 int main()
72 {
73     Queue Q = CreateQueue();
74     for (int i = 0; i < 10; i++)
75         InsertQ(Q, i);
76     for (int i = 0; i < 10; i++)
77         printf("%d ", DeleteQ(Q));
78     DeleteQ(Q);
79 
80     return 0;
81 }

 

上一篇:2021-02-09


下一篇:注解和反射