队列的顺序存储

最后更新于:2022-04-01 09:42:56

~~~ #include<stdio.h> #include<windows.h> #include<malloc.h> #define maxsize 100 typedef char elemtype; typedef struct //队列的定义 { elemtype data[maxsize]; int front,rear; }sqqueue; void initqueue(sqqueue *&q) //队列的初始化 { q=(sqqueue *)malloc(sizeof(sqqueue)); q->front=q->rear=-1; } void enqueue(sqqueue *q) //进队 { char e; getchar(); printf("请输入需进队元素:"); scanf("%c",&e); if(q->rear==maxsize-1) printf("队满,进队失败!\n"); else { q->rear++; q->data[q->rear]=e; printf("进队成功\n"); } } void queueempty(sqqueue *q) //判断队列是否为空 { if(q->rear==q->front) printf("队列为空!\n"); else printf("队列不为空!\n"); } void dequeue(sqqueue *q) //出队 { char m; if(q->rear==q->front) printf("队空,出队失败!\n"); else { q->front++; m=q->data[q->front]; printf("出队元素为:%c\n",m); } } void destroy(sqqueue *q) //销毁队列 { char m; getchar(); printf("确定要销毁队列请输入y 否则不销毁!\n"); scanf("%c",&m); if(m=='y') { free(q); printf("队列已销毁!\n"); exit(0); } else printf("队列未销毁!\n"); } int length(sqqueue *q) //求队列的长度 { int n=q->rear-q->front; return n; } void main() { sqqueue *q; int m; printf(" *************欢迎使用队列的运算系统****************\n"); initqueue(q); while(1) { printf("请选择:"); printf("1 进队\n"); printf(" 2 出队\n"); printf(" 3 判断队列是否为空\n"); printf(" 4 销毁队列\n"); printf(" 5 求队列的长度\n"); printf(" 6 退出\n"); scanf("%d",&m); switch(m) { case 1:enqueue(q);break; case 2:dequeue(q);break; case 3:queueempty(q);break; case 4:destroy(q);break; case 5:printf("队列的长度为:%d\n",length(q));break; case 6:exit(0); default:printf("输入错误,请从新输入!\n"); } } } ~~~
';