顺序队列

#include using namespace std; const int max_size = 1000; typedef struct { int data[max_size]; int front; int rear; }queue; void Initqueue( queue &qu) { qu.rear = qu.front = 0; }//构造一个空对列 int Enqueue( queue &qu, int x) { if((qu.rear + 1) % max_size == qu.front)//判断队列是否已满 { printf("/n/t/t队列已满"); return 0; } else { qu.rear = (qu.rear + 1) % max_size; qu.data[qu.rear] = x; return 1; } }//入队 int Dequeue(queue &qu, int &x) { if(qu.front == qu.rear)//判断是否为空队列 { printf("/n/t/t此队列为空队列"); return 0; } else { qu.front = (qu.front + 1) % max_size; x = qu.data[qu.front]; return 1; } }//出队 int Emtptyqueue(queue qu) { if(qu.front == qu.rear) { printf("/n/t/t此队列为空队列"); return 0; } else { printf("/n/t/t此队列为非空队列"); return 1; } }//判断队列是否为空 int display(queue qu) { int i; i = qu.front; if(qu.front == qu.rear) { printf("/n/t/t此队列为空队列"); return 0; } else { printf("/n/t/t"); while(1) { i = (i + 1) % max_size; printf("%d ", qu.data[i]); if(i == qu.rear) break; } } return 1; } //显示队列 int main() { queue q; int n; printf("/n/t/t*****************************************"); printf("/n/t/t*********** 1 构造一个空对列 *******"); printf("/n/t/t*********** 2 入队 *******"); printf("/n/t/t*********** 3 出对 *******"); printf("/n/t/t*********** 4 显示队列 *******"); printf("/n/t/t*****************************************"); Initqueue(q); while(1) { printf("/n/t/t输入你要选择的操作 n = "); scanf("%d", &n); if( n == 1) { Initqueue(q); } else if(n == 2) { int x; printf("/n/t/t输入你要插入队列的元素x = "); scanf("%d", &x); Enqueue(q, x); } else if(n == 3) { int x; Dequeue(q, x); printf("/n/t/t显示出对的元素 x = %d", x); printf("/n/t/t显示队列"); display(q); } else if(n == 4) { printf("/n/t/t显示队列 : "); display(q); } else { printf("/n/t/t没有你要选择的操作"); } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值