#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct node { int number; struct node *next; }Node,*pNode; typedef struct queue { pNode front; pNode rear; }Queue,*pQueue; void InitQueue(pQueue p) { p->front = p->rear = (pNode*)malloc(sizeof(Node)); if ( !p ) { printf("內存分配失敗......"); exit(-1); } p->rear->next = NULL; p->front->next = NULL; } int InsertQueue(pQueue p,int data) { pNode pNew = (pNode*)malloc(sizeof(Node)); if ( !pNew ) { printf("內存分配失敗......"); exit(-1); } pNew->number = data; pNew->next = NULL; p->rear->next = pNew; p->rear = pNew; return 1; } int DeleteQueue(pQueue p) { pNode pSwap = NULL; int val; if ( Empty(p) ) { exit(-1); } val = p->front->next->number; pSwap = p->front; p->front = p->front->next; free(pSwap); return val; } int Empty(pQueue p) { if ( p->front == p->rear ) { return 1; } else { return 0; } } void TravseQueue(pQueue p) { pNode pNew; pNew = p->front->next ; while ( pNew != p->rear ) { printf("%d==>",pNew->number); pNew = pNew->next; } printf("%d==>END\n",pNew->number); } int GetLength(pQueue p) { int count=0; pNode pNew; pNew = p->front; while ( pNew != p->rear ) { pNew = pNew->next; count++; } return count; } int GetTop(pQueue p) { if ( Empty(p) ) { return -1; } return p->front->next->number; } void DestoryQueue(pQueue p) { pNode pSwap = NULL; while ( p->front != p->rear ) { pSwap = p->front; p->front = p->front->next; free(pSwap); } } int main(void) { Queue s; // 定義一個棧 int k=1,choose; int i,r,r1,r2; int num; int data; // 臨時保存用戶輸入的數據 int re_num; // 保存Pop函數的返回值 while ( k == 1 ) { puts("++++++++++++++++++++++++++++++++++++++++++++++++"); puts("++ 主菜單:\n++"); puts("++ 1. 初始化隊列\n++"); puts("++ 2. 元素入隊\n++"); puts("++ 3. 元素出隊\n++"); puts("++ 4. 獲取隊頭元素\n++"); puts("++ 5. 遍歷隊元素\n++"); puts("++ 6. 隊長度\n++"); puts("++ 7. 銷燬隊列\n++"); puts("++ 0. 退出\n++"); puts("++++++++++++++++++++++++++++++++++++++++++++++++"); scanf("%d",&choose); flushall(); if ( choose>=0 && choose<=7 ) { switch(choose) { case 1: InitQueue(&s); printf("\n"); break; case 2: do{ printf("你想輸入幾個數據啊:"); r = scanf("%d",&num); flushall(); if ( r == 1 ) { for ( i =0 ; i<num ; i++ ) { do { printf("第 %d 個數:",i+1); r1 = scanf("%d",&data); flushall(); if ( r1 == 1 ) { if (InsertQueue(&s,data)) // 調用Push函數 { continue; } else { printf("進行進棧操作失敗!\n"); exit(-1); } } }while ( !(r1 == 1) ); } } }while ( !(r == 1) ); printf("\n"); break; case 3: do{ printf("你想去掉幾個數啊: "); r2 = scanf("%d",&data); flushall(); if ( r2 == 1 && data <= num) { printf("你去掉的數字是:"); for (i = 0; i < data;i++) { re_num = DeleteQueue(&s); //調用Pop函數,並把返回值賦給re_num; printf("%d ",re_num); } } }while ( !( r2 == 1 && data <= num) ); printf("\n"); break; case 4: printf("隊頭元素是:%d\n",GetTop(&s)); printf("\n"); break; case 5: TravseQueue(&s); // 調用遍歷函數 printf("\n"); break; case 6: printf("隊列長度爲:%d\n",GetLength(&s)); printf("\n"); break; case 7: DestoryQueue(&s); // 調用清空棧函數 printf("遍歷下看看隊列清空沒····\n"); TravseQueue(&s); printf("\n"); break; case 0: DestoryQueue(&s); k = 0; break; } } } getch(); return 0; }
隊列
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.