#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define QueueSize 100
typedef char DataType;
typedef struct cirqueue
{
DataType data[QueueSize];
int front;
int rear;
}CirQueue;
CirQueue *q;
//置队空
void lnitqueue(CirQueue *q)
{
q->front=q->rear=0;
}
//判队空
int queueempty(CirQueue *q)
{
return q->front==q->rear;
}
//判队满
int queuefull(CirQueue *q)
{
return q->front==q->rear+1;
}
//入队
void enqueue(CirQueue *q,DataType x)
{
if(queuefull(q))
{puts("??");exit(0);}
q->data[q->rear]=x;
q->rear=(q->rear+1)%QueueSize;
}
//出队
DataType dequeue(CirQueue *q)
{
DataType temp;
if(queueempty(q))
{puts("??");exit(0);}
temp=q->data[q->front];
q->front=(q->front+1)%QueueSize;
return temp;
}//取队头元素DataType queuefront(CirQueue *q){if(queueempty(q)){puts("??");exit(0);}return q->data[q->front];}void menu(){printf("/n/t/t*/t 1. 置队空/t/t *");printf("/n/t/t*/t 2. 判队空/t/t *");printf("/n/t/t*/t 3. 判队满/t/t *");printf("/n/t/t*/t 4. 进 队/t/t *");printf("/n/t/t*/t 5. 退 队/t/t *");printf("/n/t/t*/t 6.取队头元素/t/t *");printf("/n/t/t*/t 7.退出 系统/t/t */n");}void main(){DataType x;char ch;q=(CirQueue *)malloc(sizeof(CirQueue)); lnitqueue(q); while(1){system("cls");menu();printf("按照提示操作:");ch=getchar();switch(ch){case '1':lnitqueue(q);break;case '2':if(queueempty(q)) printf("队空");elsegetch();break;case '3':if(queuefull(q)!=0) printf("队满");elsegetch();break;case '4': printf("请输入入队元素:");getchar(); scanf("%c",&x);enqueue(q,x);break;case '5':printf("退队元素为%c",dequeue(q));getch();break;case '6':printf("取队头元素为%c",queuefront(q));getch();break;case '7':printf("欢迎使用,谢谢!/n");exit(0);}}}
原文链接:https://www.f2er.com/datastructure/383273.html