#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; }
队列
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.