//以下是Queue.h
struct Queue{
ElemType queue[QueueMaxSize];
int front,rear;
};
void InitQueue(Queue& Q)
{
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 < <"Queueis empty!"<<endl;
exit(1);
}
return Q.queue[(Q.front+1)%QueueMaxSize];//队首元素是队首指针的下一个位置中的元素
}
void QInsert(Queue& Q,const ElemType& item)
{
int k =(Q.rear+1)%QueueMaxSize;
if(k ==Q.front)
{
cerr<<"Queu overflow!"<<endl;
exit(1);
}
Q.rear =k;
Q.queue[k] =item;
}
ElemType QDelete(Queue& Q)
{
if(Q.front ==Q.rear){
cerr<<"Queue is empty!"<<endl;
exit(1);
}
Q.front =(Q.front+1)%QueueMaxSize;
return Q.queue[Q.front];
}
int QueueFull(Queue& Q)
{
return (Q.rear+1)%QueueMaxSize ==Q.front;
}
//以下是Queue.cpp
#include<iostream.h >
#include < stdlib .h >
const int QueueMaxSize=6;
typedef int ElemType;
#include"Queue.h"
void 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;
}
}
/*
http://f2.9612.org//vcpp/webinfo/WebInfoBata1.asp
QQ群:
34409541 讨论网页
34409326 讨论JAVA 已满
34408784 讨论VC++
34409699 讨论VC++
9143041 讨论MFC编程
10614204 讨论C#
10613030 讨论Win32编程
10613067 讨论游戏开发
18779860 讨论JAVA
*/
struct Queue{
ElemType queue[QueueMaxSize];
int front,rear;
};
void InitQueue(Queue& Q)
{
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 < <"Queueis empty!"<<endl;
exit(1);
}
return Q.queue[(Q.front+1)%QueueMaxSize];//队首元素是队首指针的下一个位置中的元素
}
void QInsert(Queue& Q,const ElemType& item)
{
int k =(Q.rear+1)%QueueMaxSize;
if(k ==Q.front)
{
cerr<<"Queu overflow!"<<endl;
exit(1);
}
Q.rear =k;
Q.queue[k] =item;
}
ElemType QDelete(Queue& Q)
{
if(Q.front ==Q.rear){
cerr<<"Queue is empty!"<<endl;
exit(1);
}
Q.front =(Q.front+1)%QueueMaxSize;
return Q.queue[Q.front];
}
int QueueFull(Queue& Q)
{
return (Q.rear+1)%QueueMaxSize ==Q.front;
}
//以下是Queue.cpp
#include<iostream.h >
#include < stdlib .h >
const int QueueMaxSize=6;
typedef int ElemType;
#include"Queue.h"
void 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;
}
}
/*
http://f2.9612.org//vcpp/webinfo/WebInfoBata1.asp
QQ群:
34409541 讨论网页
34409326 讨论JAVA 已满
34408784 讨论VC++
34409699 讨论VC++
9143041 讨论MFC编程
10614204 讨论C#
10613030 讨论Win32编程
10613067 讨论游戏开发
18779860 讨论JAVA
*/