我是靠谱客的博主 凶狠八宝粥,这篇文章主要介绍队列(Queuing)——简单队列,现在分享给大家,希望可以做个参考。

队列是一种特殊的线性结构,它只允许在队列的头部(head)进行“出队”操作,在队列的尾部(tail)进行“入队”操作。
当队首和队尾相等时(head == tail),为空队列。
进行一个简单的小游戏,已知一串数字,得到它“解密”后的数字。规则为删除第一个,把第二个放队列最后面,删除第三个,第四个数字放最后面,以此类推,直到删除所有的数字,按删除的顺序排的数字即为“解密”后的数字。

复制代码
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
#include<iostream> using namespace std; int main() { int q[1001]={0,6,3,1,7,5,8,9,2,4}; int i,head,tail; head = 1; tail = 10; while(tail>head) { //打印队首 cout<<q[head]; head++; //新队首后移 q[tail]=q[head]; tail++; //队首出队 head++; } return 0; }

还可以用结构体解决此类问题

复制代码
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
#include<iostream> using namespace std; struct queue { int date[1001]; int head; int tail; }; int main() { struct queue q; q.head =1; q.tail =1; for(int i=1;i<=9;i++) { cin >>q.date [q.tail ]; q.tail++ ; } while(q.head<q.tail) { cout<<q.date [q.head]; q.head++; q.date [q.tail]=q.date [q.head]; q.tail++; q.head++; } getchar(); return 0; }

最后

以上就是凶狠八宝粥最近收集整理的关于队列(Queuing)——简单队列的全部内容,更多相关队列(Queuing)——简单队列内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(53)

评论列表共有 0 条评论

立即
投稿
返回
顶部