#include<stdio.h> #include<stdlib.h> #define MAXSIZE 1024 typedef int elemtype; typedef struct SequenQueue { elemtype data[MAXSIZE]; int front; int rear; }SequenQueue; SequenQueue *Init_SequenQueue(); int SequenQueue_Empty(SequenQueue * Q); int SequenQueue_Full(SequenQueue * Q); int SequenQueue_Length(SequenQueue *Q); int Enter_SequenQueue(SequenQueue * Q,elemtype x); int Delete_SequenQueue(SequenQueue * Q); int GetFront_SequenQueue(SequenQueue * Q); int find(SequenQueue * Q,elemtype key); void menu(); //初始化 SequenQueue *Init_SequenQueue() { SequenQueue * Q; Q=(SequenQueue *)malloc(sizeof(SequenQueue)); if(Q!=NULL) { Q->front=0; Q->rear=0; } return Q; } //判队列空 int SequenQueue_Empty(SequenQueue * Q) { if(Q->front==Q->rear) { return 1; } else { return 0; } } //判断队列满 int SequenQueue_Full(SequenQueue * Q) { if((Q->rear+1)%MAXSIZE==Q->front) { return 1; } else { return 0; } } //循环队列的长度 int SequenQueue_Length(SequenQueue * Q) { return((Q->rear-Q->front+MAXSIZE)%MAXSIZE); } //入队 int Enter_SequenQueue(SequenQueue * Q,elemtype x) { if(SequenQueue_Full(Q)) { return 0; } Q->data[Q->rear]=x; Q->rear=(Q->rear+1)%MAXSIZE; return 1; } //出队 int Delete_SequenQueue(SequenQueue * Q) { if(Q->front==Q->rear) { return 0; } else { printf("取出的队头元素:%d",Q->data[Q->front]); Q->front=(Q->front+1)%MAXSIZE; return 1; } } //取队头数据元素 int GetFront_SequenQueue(SequenQueue * Q) { if(Q->front==Q->rear) { return 0; } else { printf("队头元素:%d",Q->data[Q->front]); return 1; } } int find(SequenQueue * Q,elemtype key) { int j=0; if(Q->front==Q->rear) { return 0; } else { while(Q->data[Q->front]!=key) { j++; Q->front=(Q->front+1)%MAXSIZE; } } return j; } void menu() { system("cls"); printf(" 1-initial stack "); printf(" 2-input data "); printf(" 3-get length "); printf(" 4-判断队空 "); printf(" 5-修改队头元素 "); printf(" 6-取出队头元素 "); printf(" 7-查找 "); printf(" 8-输出 "); printf(" 9-判断队满 "); printf(" #-quit "); printf("Please select: "); } int main() { SequenQueue * Q=NULL; char cmd; int i,len,isdo; elemtype x,key; system("cls"); menu(); while((cmd=getchar())!='#') { switch(cmd) { case '1': Q=Init_SequenQueue(); printf(" Created the Queue! "); printf(" "); break; case '2': printf(" Inputing data .... "); scanf("%d",&x); while(x!=0) { isdo=Enter_SequenQueue(Q,x); if(isdo==0) { printf("入队失败 "); } scanf("%d",&x); } printf(" "); break; case '3': printf(" Caculating the Length of the Queue... "); len=SequenQueue_Length(Q); printf("队列的长度为:%d ",len); printf(" "); break; case '4': isdo=SequenQueue_Empty(Q); if(isdo==0) { printf("队列不为空 "); } else if(isdo==1) { printf("队列为空 "); } printf(" "); break; case '5': isdo=Delete_SequenQueue(Q); if(isdo==0) { printf("修改队头元素失败 "); } else if(isdo==1) { printf("新的队头元素为%d ",Q->data[Q->front]); } printf(" "); break; case '6': isdo=GetFront_SequenQueue(Q); if(isdo==0) { printf("取出失败 "); } printf(" Geting the data of the position... "); printf(" "); break; case '7': printf("请输入要查找的数值:"); scanf("%d",&key); isdo=find(Q,key); if(isdo==0) { printf("查找失败 "); } else { printf("该数值在队列中的位置:%d ",isdo+1); } printf(" "); break; case '8': printf(" Displaying the all data of the Queue!..."); while(Q->front!=Q->rear) { printf("%d ",Q->data[Q->front]); Q->front=(Q->front+1)%MAXSIZE; } printf(" "); break; case '9': isdo=SequenQueue_Full(Q); if(isdo==1) { printf("队列已满 "); } else { printf("队列不满"); } } fflush(stdin); system("pause"); menu(); } return 0; }