#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("\t\t1-initial stack\n"); printf("\t\t2-input data\n"); printf("\t\t3-get length\n"); printf("\t\t4-判斷隊空\n"); printf("\t\t5-修改隊頭元素\n"); printf("\t\t6-取出隊頭元素\n"); printf("\t\t7-查找\n"); printf("\t\t8-輸出\n"); printf("\t\t9-判斷隊滿\n"); printf("\t\t#-quit\n"); 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("\nCreated the Queue!\n"); printf("\n\n\n\t\t\t"); break; case '2': printf("\nInputing data ....\n"); scanf("%d",&x); while(x!=0) { isdo=Enter_SequenQueue(Q,x); if(isdo==0) { printf("入隊失敗\n"); } scanf("%d",&x); } printf("\n\n\n\t\t\t"); break; case '3': printf("\nCaculating the Length of the Queue...\n"); len=SequenQueue_Length(Q); printf("隊列的長度為:%d\n",len); printf("\n\n\n\t\t\t"); break; case '4': isdo=SequenQueue_Empty(Q); if(isdo==0) { printf("隊列不為空\n"); } else if(isdo==1) { printf("隊列為空\n"); } printf("\n\n\n\t\t\t"); break; case '5': isdo=Delete_SequenQueue(Q); if(isdo==0) { printf("修改隊頭元素失敗\n"); } else if(isdo==1) { printf("新的隊頭元素為%d\n",Q->data[Q->front]); } printf("\n\n\n\t\t\t"); break; case '6': isdo=GetFront_SequenQueue(Q); if(isdo==0) { printf("取出失敗\n"); } printf("\nGeting the data of the position...\n"); printf("\n\n\n\t\t\t"); break; case '7': printf("請輸入要查找的數值:"); scanf("%d",&key); isdo=find(Q,key); if(isdo==0) { printf("查找失敗\n"); } else { printf("該數值在隊列中的位置:%d\n",isdo+1); } printf("\n\n\n\t\t\t"); break; case '8': printf("\nDisplaying the all data of the Queue!..."); while(Q->front!=Q->rear) { printf("%d\t",Q->data[Q->front]); Q->front=(Q->front+1)%MAXSIZE; } printf("\n\n\n\t\t\t"); break; case '9': isdo=SequenQueue_Full(Q); if(isdo==1) { printf("隊列已滿\n"); } else { printf("隊列不滿"); } } fflush(stdin); system("pause"); menu(); } return 0; }