循环队列(链式存储结构)

本文最后更新于:3 个月前

本文主要总结了循环队列的另一种实现形式,即循环队列的链式存储结构形式,主要是完整实现代码。

循环队列(链式存储结构)

完整代码实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include<iostream>
#include<string>

using namespace std;

/*
循环队列的链式实现
*/

/* 链队列存储结构 */
typedef struct SNode
{
int data;
struct SNode * next;
}SNode,*SqQueue;

typedef struct
{
SqQueue front;
SqQueue rear;
}LinkQueue;

//初始化队列(有头结点)
void InitQueue(LinkQueue& S)
{
S.front = S.rear = new SNode;
S.front->next = NULL;
}

//判断队列空
bool QueueEmpty(LinkQueue S)
{
if (S.front == S.rear)
{
return true;
}
else {
return false;
}
}

//进队列(每次都插在头结点后面)
bool Push(LinkQueue& S, int e)
{
SNode* p = new SNode;
p->data = e;
p->next = NULL;
S.rear->next = p; //修改后继指针
S.rear = p;
return true;
}

//出队列
bool Pop(LinkQueue& S, int& e)
{
if (QueueEmpty(S))
{
return false;
}
e = S.front->next->data;
SNode* p = S.front->next;
S.front->next = p->next;
if (S.rear == p)
{
S.rear = S.front;
}
delete p;
return true;
}

//读取队列顶元素
void GetTop(LinkQueue S, int& e)
{
if (QueueEmpty(S))
{
return;
}
e = S.front->next->data;
}

int main()
{
LinkQueue S;
InitQueue(S);
cout << "队列是否为空:" << QueueEmpty(S) << endl;
int a = 10;
int b;

cout << "入队列:" << Push(S, a) << endl;
GetTop(S, b);
cout << "读取队列顶元素:" << b << endl;
cout << "出队列:" << Pop(S, b) << endl;
cout << "队列是否为空:" << QueueEmpty(S) << endl;
return 0;
}

循环队列(链式存储结构)
https://superlovelace.top/2023/10/13/LinkQueue/
作者
棱境
发布于
2023年10月13日
更新于
2023年10月31日
许可协议