一.链队列
#include<iostream>
#include<cstdlib>
#include<stdio.h>
#include<iomanip>
#define OK 1
#define OVERFLOW 0
#define ERROR 0
typedef int QElemType;
using namespace std;
typedef struct QNode{
QElemType data;
struct QNode *next;
}QNode,*QueuePtr;
typedef struct{
QueuePtr front;//队头指针
QueuePtr rear;//队尾指针
}LinkQueue;
//1.初始化链队列
int InitQueue(LinkQueue *Q)
{
Q->front=Q->rear=(QueuePtr)malloc(sizeof(QNode));
if(!Q->front)
exit(OVERFLOW);//存储分配失败
Q->front->next=NULL;
return OK;
}
//2.清空队列
int ClearQueue(LinkQueue Q)
{
Q.front=Q.rear;
return 1;
}
//3.销毁队列
int DestoryQueue(LinkQueue *Q)
{
while (Q->front)
{
Q->rear = Q->front->next;
free(Q->front);
Q->front = Q->rear;
}
return OK;
}
//4.入队操作
int EnQueue(LinkQueue *Q,QElemType e)
{
QNode *p;
p=(QueuePtr)malloc(sizeof(QNode));
if (!p) exit(OVERFLOW); // 存储分配失败
p->data=e;
p->next=NULL;
Q->rear->next=p; // 修改尾结点的指针
Q->rear=p; // 移动队尾指针
return OK;
}
//5.出队操作
int DeQueue(LinkQueue *Q,QElemType &e)
{// 若队列不空,则删除Q的队头元素,用e返回其值, 并返回OK;否则返回ERROR
if(Q->front==Q->rear) return ERROR;
QNode *p;
p=Q->front->next;
e=p->data;
Q->front->next=p->next;
if(Q->rear==p)
Q->rear=Q->front;
free(p);
return OK;
}
//6.判断链队列是否为空
int QueueEmpty(LinkQueue *Q)
{
return(Q->front==Q->rear);
}
//7.求链队列的队头元素
int GetHead(LinkQueue *Q, QElemType &e)
{
if(Q->front==Q->rear) return ERROR;
e=Q->front->next->data;
return OK;
}
//8.显示队列中的元素
void Print(LinkQueue *Q)
{
QNode *p;
p=Q->front->next;
while(p)
{
cout<<p->data<<" ";
p=p->next;
}
}
//9.求队列长度
int QueueLength(LinkQueue Q)
{
int i=0;
while(Q.front->next)
{
Q.front=Q.front->next;
i++;
}
return i;
}
void show_help()
{
cout<<"1----初始化空队列"<<endl;
cout<<"2----清空队列"<<endl;
cout<<"3----销毁队列"<<endl;
cout<<"4----入队"<<endl;
cout<<"5----出队"<<endl;
cout<<"6----判空"<<endl;
cout<<"7----取出队列对头元素"<<endl;
cout<<"8----显示队列的元素"<<endl;
cout<<"9----求队列长度"<<endl;
cout<<" 退出,输出一个负数!"<<endl;
}
int main()
{
int operate_code;
show_help();
LinkQueue Q;
QElemType e;
InitQueue(&Q);
while(1)
{
cout<<"请输入操作代码:";
cin>>operate_code;
if(operate_code==1)
{
InitQueue(&Q);
}
else if(operate_code==2)
{
DestoryQueue(&Q);
}
else if(operate_code==3)
{
cout<<"请输入要入队的元素:";
int e;
cin>>e;
EnQueue(&Q,e);
}
else if(operate_code==4)
{
DeQueue(&Q,e);
}
else if(operate_code==5)
{
if(QueueEmpty(&Q))
cout<<"The Queue is empty."<<endl;
else
cout<<"The Queue is not empty"<<endl;
}
else if(operate_code==6)
{
GetHead(&Q,e);
cout<<e<<endl;
}
else if(operate_code==7)
{
Print(&Q);
}
else if(operate_code<0)
{
break;
}
else
{
cout<<"\n操作码错误!!!"<<endl;
show_help();
}
}
return 0;
}
二.循环队列
#include<iostream>
#include<cstdlib>
#include<stdio.h>
#include<iomanip>
#define OK 1
#define OVERFLOW 0
#define ERROR 0
#define MAXQSIZE 100 //最大队列长度
using namespace std;
typedef struct {
int *base; //初始化的动态分配存储空间
int front; //头指针
int rear; //尾指针
}SqQueue;
//1.初始化循环队列
int InitQueue(SqQueue &Q)
{
Q.base=(int *)malloc(MAXQSIZE*sizeof(int));
if(!Q.base)
exit(OVERFLOW);
Q.front=Q.rear=0;
return OK;
}
//2.求循环队列的长度
int QueueLength(SqQueue Q)
{
return (Q.rear-Q.front+MAXQSIZE)%MAXQSIZE;
}
//3.循环队列入队
int EnQueue(SqQueue &Q,int e)
{
if((Q.rear+1)%MAXQSIZE==Q.front)
return ERROR;
Q.base[Q.rear]=e;
Q.rear=(Q.rear+1)%MAXQSIZE;
return OK;
}
//4.循环队列出队
int DeQueue (SqQueue &Q,int &e)
{
if(Q.front==Q.rear) return ERROR;
e=Q.base[Q.front];
Q.front=(Q.front+1) % MAXQSIZE;
return OK;
}
//6.取出循环队列头结点
int GetHead(SqQueue &Q)
{
if(Q.front!=Q.rear)
return Q.base[Q.front];
}
//7.删除队列头结点
void Deququ(SqQueue &Q,int &e)
{
if(Q.front==Q.rear)
{
cout<<"为空"<<endl;
return ;
}
e=Q.base[Q.front];
Q.front=(Q.front+1)%MAXQSIZE;
cout<<"删除的值为:";
cout<<e<<endl;
}
//8.输出循环队列的各个结点值
void visit(SqQueue Q)
{
int i;
i=Q.front;
while(i!=Q.rear)
{
cout<<Q.base[i];
i=(i+1)%MAXQSIZE;
}
}
//9.销毁队列
int DestoryQueue(SqQueue &Q)
{
if(Q.base)
delete[]Q.base;
Q.front=Q.rear=0;
}
void show_help()
{
cout<<"1----初始化循环队列"<<endl;
cout<<"2----循环队列的长度 "<<endl;
cout<<"3----循环队列入队"<<endl;
cout<<"4----循环队列出队 "<<endl;
cout<<"5----判空"<<endl;
cout<<"6-----取出循环队列头结点"<<endl;
cout<<"7-----删除队列头结点"<<endl;
cout<<"8-----出循环队列的各个结点值"<<endl;
cout<<"9-----销毁队列"<<endl;
cout<<" 退出,输出一个负数!"<<endl;
}
int main()
{
int operate_code;
show_help();
SqQueue head;
InitQueue(head);
while(1)
{
cout<<"请输入操作代码:";
cin>>operate_code;
if(operate_code==1)
{
InitQueue(head);
}
else if(operate_code==2)
{
cout<<"该循环队列的长度为:"<<QueueLength(head)<<endl;
}
else if(operate_code==3)
{
int e;
cout<<"请输入要入队的元素:";
cin>>e;
EnQueue(head,e);
}
else if(operate_code==4)
{
int e;
DeQueue(head,e);
cout<<"出队的元素为:"<<e<<endl;
}
else if(operate_code==5)
{
if(QueueLength(head))
cout<<"The Queue is not empty."<<endl;
else
cout<<"The Queue is empty."<<endl;
}
else if(operate_code==6)
{
cout<<GetHead(head)<<endl;
}
else if(operate_code==7)
{
int e;
DeQueue(head,e);
}
else if(operate_code==8)
{
visit(head);
cout<<endl;
}
else if(operate_code==9)
{
DestoryQueue(head);
}
else if(operate_code<0)
{
break;
}
else
{
cout<<"\n操作码错误!!!"<<endl;
show_help();
}
}
}