队列的特点:先进先出
核心算法
#ifndef _QUEUE_H_
#define _QUEUE_H_
#define _QUEUE_H_
#define MAXSIZE 4
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int ElemType;
typedef int Status;
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int ElemType;
typedef int Status;
typedef struct
{
ElemType *base;
int front;
int rear;
}SqQueue;
{
ElemType *base;
int front;
int rear;
}SqQueue;
Status InitQueue(SqQueue &Q);
Status DestoryQueue(SqQueue &Q);
Status ClearQueue(SqQueue &Q);
Status EmptyQueue(SqQueue &Q);
int Queuelength(SqQueue Q);
Status GetHead(SqQueue Q,ElemType &e);
Status EnterQueue(SqQueue &Q,ElemType e);
Status DeleteQueue(SqQueue &Q,ElemType &e);
void printQueue(SqQueue Q);
Status DestoryQueue(SqQueue &Q);
Status ClearQueue(SqQueue &Q);
Status EmptyQueue(SqQueue &Q);
int Queuelength(SqQueue Q);
Status GetHead(SqQueue Q,ElemType &e);
Status EnterQueue(SqQueue &Q,ElemType e);
Status DeleteQueue(SqQueue &Q,ElemType &e);
void printQueue(SqQueue Q);
#endif
#include"queue.h"
#include<iostream>
using namespace std;
#include<iostream>
using namespace std;
//构造空队列
Status InitQueue(SqQueue &Q)
{
Q.base=(ElemType*)malloc(MAXSIZE*sizeof(ElemType));
if(!Q.base)
{
return OVERFLOW;
}
else
{
Q.front=Q.rear=0;
return OK;
}
}
Status InitQueue(SqQueue &Q)
{
Q.base=(ElemType*)malloc(MAXSIZE*sizeof(ElemType));
if(!Q.base)
{
return OVERFLOW;
}
else
{
Q.front=Q.rear=0;
return OK;
}
}
//销毁队列
Status DestoryQueue(SqQueue &Q)
{
free(Q.base);
return TRUE;
}
Status DestoryQueue(SqQueue &Q)
{
free(Q.base);
return TRUE;
}
//清空队列
Status ClearQueue(SqQueue &Q)
{
Q.rear=Q.front;
return OK;
}
Status ClearQueue(SqQueue &Q)
{
Q.rear=Q.front;
return OK;
}
//置空队列
Status EmptyQueue(SqQueue &Q)
{
if(Q.rear!=Q.front)
{
return FALSE;
}
else
{
return TRUE;
}
}
Status EmptyQueue(SqQueue &Q)
{
if(Q.rear!=Q.front)
{
return FALSE;
}
else
{
return TRUE;
}
}
//遍历
void printQueue(SqQueue Q)
{
int i=0,j=Queuelength(Q);
if(Q.front==Q.rear)
{
cout<<"队列为空!"<<endl;
}
while(i<j)
{
cout<<Q.base[(Q.front+i++)%MAXSIZE]<<" ";
}
cout<<endl;
}
void printQueue(SqQueue Q)
{
int i=0,j=Queuelength(Q);
if(Q.front==Q.rear)
{
cout<<"队列为空!"<<endl;
}
while(i<j)
{
cout<<Q.base[(Q.front+i++)%MAXSIZE]<<" ";
}
cout<<endl;
}
//求队列的长度
int Queuelength(SqQueue Q)
{
return (Q.rear-Q.front+MAXSIZE)%MAXSIZE;
}
int Queuelength(SqQueue Q)
{
return (Q.rear-Q.front+MAXSIZE)%MAXSIZE;
}
//取出队头元素
Status GetHead(SqQueue Q,ElemType &e)
{
if(Q.front==Q.rear)
{
return ERROR;
}
else
{
e=Q.base[Q.front];
return OK;
}
}
Status GetHead(SqQueue Q,ElemType &e)
{
if(Q.front==Q.rear)
{
return ERROR;
}
else
{
e=Q.base[Q.front];
return OK;
}
}
//入队
Status EnterQueue(SqQueue &Q,ElemType e)
{
if((Q.rear+1)%MAXSIZE==Q.front)
{
return ERROR;
}
else
{
Q.base[Q.rear]=e;
Q.rear=(Q.rear+1)%MAXSIZE;
return OK;
}
}
Status EnterQueue(SqQueue &Q,ElemType e)
{
if((Q.rear+1)%MAXSIZE==Q.front)
{
return ERROR;
}
else
{
Q.base[Q.rear]=e;
Q.rear=(Q.rear+1)%MAXSIZE;
return OK;
}
}
//出队
Status DeleteQueue(SqQueue &Q,ElemType &e)
{
if(Q.front==Q.rear)
{
return ERROR;
}
else
{
e=Q.base[Q.front];
Q.front=(Q.front+1)%MAXSIZE;
return OK;
}
}
Status DeleteQueue(SqQueue &Q,ElemType &e)
{
if(Q.front==Q.rear)
{
return ERROR;
}
else
{
e=Q.base[Q.front];
Q.front=(Q.front+1)%MAXSIZE;
return OK;
}
}
#include"queue.h"
#include<iostream>
using namespace std;
#include<iostream>
using namespace std;
int main()
{
SqQueue Q;
ElemType e;
int length,select,i;
if(InitQueue(Q)==OVERFLOW)
{
cout<<"内存分配失败!"<<endl;
}
else
{
cout<<"请输入队列的长度:"<<endl;
cin>>length;
for(i=0;i<length;i++)
{
cin>>e;
EnterQueue(Q,e);
}
}
do
{
cout<<"1.遍历队列!"<<endl;
cout<<"2.求队列的长度!"<<endl;
cout<<"3.取出队头元素!"<<endl;
cout<<"4.入队!"<<endl;
cout<<"5.出队!"<<endl;
cout<<"0.操作结束!"<<endl;
cout<<"请选择:"<<endl;
cin>>select;
switch(select)
{
case 1:
printQueue(Q);
break;
case 2:
cout<<"队列的长度为:"<<Queuelength(Q)<<endl;
break;
case 3:
if(GetHead(Q,e)==ERROR)
{
cout<<"队列为空!"<<endl;
}
else
{
cout<<"队头元素为:"<<e<<endl;
}
break;
case 4:
cout<<"请输入入队的元素:"<<endl;
cin>>e;
if(EnterQueue(Q,e)==ERROR)
{
cout<<"队列满!"<<endl;
}
else
{
printQueue(Q);
}
break;
case 5:
if(DeleteQueue(Q,e)==ERROR)
{
cout<<"队列为空!"<<endl;
}
else
{
cout<<"出队的元素为:"<<e<<endl;
}
break;
case 0:
cout<<"操作结束!"<<endl;
break;
default:
cout<<"输入错误!"<<endl;
}
}while(select!=0);
DestoryQueue(Q);
}
{
SqQueue Q;
ElemType e;
int length,select,i;
if(InitQueue(Q)==OVERFLOW)
{
cout<<"内存分配失败!"<<endl;
}
else
{
cout<<"请输入队列的长度:"<<endl;
cin>>length;
for(i=0;i<length;i++)
{
cin>>e;
EnterQueue(Q,e);
}
}
do
{
cout<<"1.遍历队列!"<<endl;
cout<<"2.求队列的长度!"<<endl;
cout<<"3.取出队头元素!"<<endl;
cout<<"4.入队!"<<endl;
cout<<"5.出队!"<<endl;
cout<<"0.操作结束!"<<endl;
cout<<"请选择:"<<endl;
cin>>select;
switch(select)
{
case 1:
printQueue(Q);
break;
case 2:
cout<<"队列的长度为:"<<Queuelength(Q)<<endl;
break;
case 3:
if(GetHead(Q,e)==ERROR)
{
cout<<"队列为空!"<<endl;
}
else
{
cout<<"队头元素为:"<<e<<endl;
}
break;
case 4:
cout<<"请输入入队的元素:"<<endl;
cin>>e;
if(EnterQueue(Q,e)==ERROR)
{
cout<<"队列满!"<<endl;
}
else
{
printQueue(Q);
}
break;
case 5:
if(DeleteQueue(Q,e)==ERROR)
{
cout<<"队列为空!"<<endl;
}
else
{
cout<<"出队的元素为:"<<e<<endl;
}
break;
case 0:
cout<<"操作结束!"<<endl;
break;
default:
cout<<"输入错误!"<<endl;
}
}while(select!=0);
DestoryQueue(Q);
}