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

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

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 }


相关文章
|
2月前
特殊链表(循环单链表,循环双链表,静态链表)
特殊链表(循环单链表,循环双链表,静态链表)
23 3
|
2月前
|
存储
数据结构:图文详解单链表的各种操作(头插法,尾插法,任意位置插入,删除节点,查询节点,求链表的长度,清空链表)
数据结构:图文详解单链表的各种操作(头插法,尾插法,任意位置插入,删除节点,查询节点,求链表的长度,清空链表)
373 0
|
2月前
单链表实现【队列】
单链表实现【队列】
50 0
用循环链表实现队列
用循环链表实现队列
77 0
|
9月前
|
存储
链表(一) 单链表操作详解
链表(一) 单链表操作详解
35 0
|
11月前
|
C++
7.5 C/C++ 实现链表队列
链表队列是一种基于链表实现的队列,相比于顺序队列而言,链表队列不需要预先申请固定大小的内存空间,可以根据需要动态申请和释放内存。在链表队列中,每个节点包含一个数据元素和一个指向下一个节点的指针,头节点表示队头,尾节点表示队尾,入队操作在队尾插入元素,出队操作在队头删除元素,队列的长度由节点数量决定。由于链表队列没有容量限制,因此可以处理任意数量的元素,但是相比于顺序队列,链表队列的访问速度较慢,因为需要通过指针来访问下一个节点。
55 0
1.移除链表元素 2.反转链表 3.链表的中间结点
1.移除链表元素 2.反转链表 3.链表的中间结点
56 0
顺序循环队列与链队列
今天学习了队列,一种是顺序循环队列,一种是链队列,我个人认为链队列相对好用一点,毕竟链队列不用考虑“假溢出”的问题,下面是我整理的关于队列的一些基本操作
116 0
顺序循环队列与链队列
线性表的链式存储实现(不带头结点)
线性表的链式存储实现(不带头结点)