使用队列的程序举例(1)
2018-04-03 20:40
211 查看
.h文件:/*循环队列的顺序存储*/
//初始化
void InitQueue(Queue &Q)
{
//把队首和队尾指针置为同一个下标值0表示队空
Q.front = Q.rear = 0;
}
//清空队列
void ClearQueue(Queue &Q)
{
Q.front = Q.rear = 0;
}
//检查队列是否为空
int QueueEmpty(Queue &Q)
{
return (Q.front == Q.rear);
}
//读取队首元素
ElemType QFront(Queue &Q)
{
if(Q.front == Q.rear)
{
cerr<<"Queue is empty!"<<endl;
exit(1);
}
return Q.queue[(Q.front+1) % QueueMaxSize];
//队首元素是队首指针的下一个位置中的元素
}
//检查队列是否已满
int QueueFull(Queue &Q)
{
return (Q.rear + 1) % QueueMaxSize == Q.front;
}
//插入元素
void QInsert(Queue &Q, const ElemType &item)
{
if(QueueFull(Q))
{
cerr<<"Queue overflow!"<<endl;
exit(1);
}
Q.rear = (Q.rear+1) % QueueMaxSize;
Q.queue[Q.rear] = item;
}
//删除元素
ElemType QDelete(Queue &Q)
{
if(QueueEmpty(Q))
{
cerr<<"Queue is empty!"<<endl;
exit(1);
}
Q.front = (Q.front + 1) % QueueMaxSize;
return Q.queue[Q.front];
}
.cpp文件:#include <iostream>
#include <stdlib.h>
using namespace std;
typedef int ElemType;
const int QueueMaxSize = 50;
struct Queue
{
ElemType queue[QueueMaxSize];
int front;
int rear;
};
#include "queue.h"
int main()
{
Queue q;
InitQueue(q);
for(int i = 0; i < 6; i++)
{
int x = rand() % 100;
int y = rand() % 100;
if(!QueueFull(q))
{
QInsert(q,x);
cout<<x<<"进队,";
}
if(!QueueFull(q))
{
QInsert(q,y);
cout<<y<<"进队";
}
cout<<endl;
cout<<QDelete(q)<<"出队"<<endl;
}
cout<<endl;
while(!QueueEmpty(q))
cout<<QDelete(q)<<"出队"<<endl;
return 0;
}
此程序使用一个长度为6的顺序队列,利用此队列保存由计算机产生的随机数。主函数中的fo:循环体共执行6次,每次执行时首先产生出两个100以内的随机整数,接着在队列未满时人队,然后进行一次出栈操作,在主函数的最后使队列中的所有元素依次出队。
//初始化
void InitQueue(Queue &Q)
{
//把队首和队尾指针置为同一个下标值0表示队空
Q.front = Q.rear = 0;
}
//清空队列
void ClearQueue(Queue &Q)
{
Q.front = Q.rear = 0;
}
//检查队列是否为空
int QueueEmpty(Queue &Q)
{
return (Q.front == Q.rear);
}
//读取队首元素
ElemType QFront(Queue &Q)
{
if(Q.front == Q.rear)
{
cerr<<"Queue is empty!"<<endl;
exit(1);
}
return Q.queue[(Q.front+1) % QueueMaxSize];
//队首元素是队首指针的下一个位置中的元素
}
//检查队列是否已满
int QueueFull(Queue &Q)
{
return (Q.rear + 1) % QueueMaxSize == Q.front;
}
//插入元素
void QInsert(Queue &Q, const ElemType &item)
{
if(QueueFull(Q))
{
cerr<<"Queue overflow!"<<endl;
exit(1);
}
Q.rear = (Q.rear+1) % QueueMaxSize;
Q.queue[Q.rear] = item;
}
//删除元素
ElemType QDelete(Queue &Q)
{
if(QueueEmpty(Q))
{
cerr<<"Queue is empty!"<<endl;
exit(1);
}
Q.front = (Q.front + 1) % QueueMaxSize;
return Q.queue[Q.front];
}
.cpp文件:#include <iostream>
#include <stdlib.h>
using namespace std;
typedef int ElemType;
const int QueueMaxSize = 50;
struct Queue
{
ElemType queue[QueueMaxSize];
int front;
int rear;
};
#include "queue.h"
int main()
{
Queue q;
InitQueue(q);
for(int i = 0; i < 6; i++)
{
int x = rand() % 100;
int y = rand() % 100;
if(!QueueFull(q))
{
QInsert(q,x);
cout<<x<<"进队,";
}
if(!QueueFull(q))
{
QInsert(q,y);
cout<<y<<"进队";
}
cout<<endl;
cout<<QDelete(q)<<"出队"<<endl;
}
cout<<endl;
while(!QueueEmpty(q))
cout<<QDelete(q)<<"出队"<<endl;
return 0;
}
此程序使用一个长度为6的顺序队列,利用此队列保存由计算机产生的随机数。主函数中的fo:循环体共执行6次,每次执行时首先产生出两个100以内的随机整数,接着在队列未满时人队,然后进行一次出栈操作,在主函数的最后使队列中的所有元素依次出队。
相关文章推荐
- 使用队列的程序举例(2)
- C#编写Windows服务程序 (服务端),客户端使用 消息队列 实现淘宝 订单全链路效果
- C++ STL入门教程(3)——deque(双向队列)的使用(附完整程序代码)
- Java LinkedList特有方法程序小解 && 使用LinkedList 模拟一个堆栈或者队列数据结构。
- Volley程序集合(使用单例,保证只会创建一个队列)
- C++ STL入门教程(3)——deque(双向队列)的使用(附完整程序代码)
- C#编写Windows服务程序 (服务端),client使用 消息队列 实现淘宝 订单全链路效果
- Android 系列 4.11使用活动线程队列和处理程序在线程之间发送消息
- LabVIEW的Framework(3):在GUI程序里使用消息队列
- C语言队列入门程序举例
- 程序小白天天打卡(栈和队列的综合使用)
- 使用Java程序来实现HTTP文件的队列下载
- C++ STL入门教程(4)——stack(栈),queue(队列),priority_queue(优先队列)的使用(附完整程序代码)
- 使用Java程序来实现HTTP文件的队列下载
- 链表实现的队列程序(仅供自己学习使用)
- java 大量数据提交时,使用队列定时提交思路的程序实现
- 队列和栈面试题(一)— 请编写一个程序,按升序对栈进行排序,要求最多只能使用一个额外的栈存放临时数据
- C++ STL入门教程(3)——deque(双向队列)的使用(附完整程序代码)
- 信息队列使用举例
- 如何在程序中使用自己的库单元