使用队列的程序举例(2)
2018-04-03 20:49
344 查看
.h文件:
/*循环队列的链式存储*/
//初始化
void InitQueue(LinkQueue &HQ)
{
HQ.front = HQ.rear = NULL;
}
//清空队列
void ClearQueue(LinkQueue &HQ)
{
LNode *p = HQ.front;
while(p != NULL)
{
HQ.front = p->next;
delete p;
p = HQ.front->next;
}
HQ.front->next = NULL;
}
//检查队列是否为空
int QueueEmpty(LinkQueue &HQ)
{
return (HQ.front == NULL);
}
//读取队首元素
ElemType QFront(LinkQueue &HQ)
{
if(HQ.front == NULL)
{
cerr<<"Linked queue is empty!"<<endl;
exit(1);
}
return HQ.front->data;
}
//插入元素
void QInsert(LinkQueue &HQ,const ElemType &item)
{
LNode *newptr = new LNode;
if(newptr == NULL)
{
cerr<<"Memory allocation failare!"<<endl;
exit(1);
}
newptr->data = item;
newptr->next = NULL;
if(HQ.rear == NULL)
HQ.front = HQ.rear = newptr;
else
{
HQ.rear->next = newptr;
HQ.rear = newptr;
}
}
//删除元素
ElemType QDelete(LinkQueue &HQ)
{
ElemType temp = QFront(HQ);
LNode *p = HQ.front; //暂存队首指针以便回收队首结点
HQ.front = p->next;
if(HQ.front == NULL)
HQ.rear = NULL; //若队列变为空,则需同时使队尾指针变为空
delete p; //回收原队首结点
return temp;
}
.cpp文件:#include <iostream>
using namespace std;
typedef int ElemType;
const int QueueMaxSize = 50;
struct LNode
{
ElemType data;
LNode *next;
};
struct LinkQueue
{
LNode *front;
LNode *rear;
};
#include "queue.h"
int main()
{
LinkQueue q1,q2;
InitQueue(q1);
InitQueue(q2);
for(int i = 0; i < 20; i++)
{
int x = rand() % 100;
cout<<x<<" ";
if(x % 2)
QInsert(q1,x);
else
QInsert(q2,x);
}
cout<<endl;
cout<<"q1 q2"<<endl;
while(!QueueEmpty(q1) && !QueueEmpty(q2))
cout<<QDelete(q1)<<" "<<QDelete(q2)<<endl;
return 0;
}
此程序使用了两个链队q1和够,用来分别存储由计算机随机产生的20个100以内的奇数和偶数,然后每行输出q1和q2中的一个值,即奇数和偶数配对输出,直到任一队列为空时止。
/*循环队列的链式存储*/
//初始化
void InitQueue(LinkQueue &HQ)
{
HQ.front = HQ.rear = NULL;
}
//清空队列
void ClearQueue(LinkQueue &HQ)
{
LNode *p = HQ.front;
while(p != NULL)
{
HQ.front = p->next;
delete p;
p = HQ.front->next;
}
HQ.front->next = NULL;
}
//检查队列是否为空
int QueueEmpty(LinkQueue &HQ)
{
return (HQ.front == NULL);
}
//读取队首元素
ElemType QFront(LinkQueue &HQ)
{
if(HQ.front == NULL)
{
cerr<<"Linked queue is empty!"<<endl;
exit(1);
}
return HQ.front->data;
}
//插入元素
void QInsert(LinkQueue &HQ,const ElemType &item)
{
LNode *newptr = new LNode;
if(newptr == NULL)
{
cerr<<"Memory allocation failare!"<<endl;
exit(1);
}
newptr->data = item;
newptr->next = NULL;
if(HQ.rear == NULL)
HQ.front = HQ.rear = newptr;
else
{
HQ.rear->next = newptr;
HQ.rear = newptr;
}
}
//删除元素
ElemType QDelete(LinkQueue &HQ)
{
ElemType temp = QFront(HQ);
LNode *p = HQ.front; //暂存队首指针以便回收队首结点
HQ.front = p->next;
if(HQ.front == NULL)
HQ.rear = NULL; //若队列变为空,则需同时使队尾指针变为空
delete p; //回收原队首结点
return temp;
}
.cpp文件:#include <iostream>
using namespace std;
typedef int ElemType;
const int QueueMaxSize = 50;
struct LNode
{
ElemType data;
LNode *next;
};
struct LinkQueue
{
LNode *front;
LNode *rear;
};
#include "queue.h"
int main()
{
LinkQueue q1,q2;
InitQueue(q1);
InitQueue(q2);
for(int i = 0; i < 20; i++)
{
int x = rand() % 100;
cout<<x<<" ";
if(x % 2)
QInsert(q1,x);
else
QInsert(q2,x);
}
cout<<endl;
cout<<"q1 q2"<<endl;
while(!QueueEmpty(q1) && !QueueEmpty(q2))
cout<<QDelete(q1)<<" "<<QDelete(q2)<<endl;
return 0;
}
此程序使用了两个链队q1和够,用来分别存储由计算机随机产生的20个100以内的奇数和偶数,然后每行输出q1和q2中的一个值,即奇数和偶数配对输出,直到任一队列为空时止。
相关文章推荐
- 使用队列的程序举例(1)
- 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(双向队列)的使用(附完整程序代码)
- 信息队列使用举例
- 如何在程序中使用自己的库单元