大数据结构:队列子系统_第1页
大数据结构:队列子系统_第2页
大数据结构:队列子系统_第3页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、/*题目:设计一个整型的链队列。* 编写队列的进队、出队、读队头元素、显示队列中全部元素程序。*题目:设计一个输入限制性的双队列,*要求:输入只能在一端进展,而输出可以选择从队头输出* 或队尾输出,全部选择完毕后能显示所选择的输出结果。*题目:设计一个选择式菜单,以菜单方式选择队列的各种根本操作。* 队列子系统* *1-进队*2-出队*3-读队头元素*4-显示*5-双队列*0-返回*请选择菜单号0-5:*/#i nclude <stdio.h>#i nclude <stdlib.h>#defi ne QUEUEMAX 30typedef struct顺序队列int qu

2、euQUEUEMAX;int front;/ 队头int rear;/ 队尾li nkQueues;typedef struct queue nodeint data;struct queue node *n ext;queueNode;typedef structqueueNode *front;/ 队头指针queueNode *rear;/ 队尾指针li nkQueue;void in Queue(l in kQueue *p);void outQueue(l in kQueue *p);void readFr on t(li nkQueue *p);void showQueue(l in

3、 kQueue *p);void in Queues(l in kQueues *p);void outQueue_fro nt(l in kQueues *p);void outQueue_rear(l in kQueues *p);void doubleQueue();/*Function: main()Description:主调函数Calls: in Queue()outQueue()readQueue()showQueue()In put: NULLReturn: voidOthers: NULL*/void mai n()int choice, i = 1;lin kQueue *

4、p = (li nkQueue *)malloc(sizeof(li nkQueue);p->front = p->rear = NULL;/链队列队头队尾的指针初始化while (i)prin tf("n队列子系统n");*n");printf(prin tf("*1进队*n");prin tf("*2出队*n");prin tf("*3读队头元素*n");prin tf("*4显prin tf("*5双prin tf("*0返示*n");队列*n&q

5、uot;);回*n");*n");printf( printf("请选择菜单号0-5:");fflush(stdi n); choice = getchar();/清空输入的缓存区switch(choice)case '1':in Queue(p);/ 入队break;case 2:outQueue(p); / 出队break;case 3:readFro nt(p);/读对头元素break;case 4:showQueue(p);II读队中所有元素break;case '5':doubleQueue();break;ca

6、se 'O':i = 0; break;default:i = 1;break;/*Function: in Queue()Description:入队Calls: NULLIn put:p链队列指针Return: voidOthers: NULL*/void in Queue(l in kQueue *p)queueNode *q;int兀q = (queueNode *)malloc(sizeof(queueNode);printf(”请输入入队的整数:”);scan f("%d", &x);getchar();q->data = x;q-

7、>next = NULL;if (p->front = NULL)/队头指针为空时p->front = q;/队头指针指向结点p->rear = q;II队尾指针指向结点elsep->rear->next = q; II队列元素增加p->rear = q;if (q) II判断新的元素指针是否正确printf("%d 入队成功! n", x);/*Function: outQueue()Description:出队Calls: NULLIn put: p链队列指针Return: voidOthers: NULL*/void outQ

8、ueue(l in kQueue *p)queueNode *q;int兀if (p->front = NULL) /队头指针为空时printf("出队失败!n");elseq = p->fro nt;x = q_>data;p->fro nt = p->fro nt-> next;if (p->fro nt = NULL)/假如队头指针为空,如此队尾指针也要为空。p->rear = NULL; free(q);printf("%d 出队成功 n", x);/*Function: readFro nt()D

9、escription: 读队头元素Calls: NULLIn put: p链队列指针Return: voidOthers: NULL*/void readFr on t(li nkQueue *p)int兀if (p = NULL | p->front = NULL) /队列不存在或队头指针为空elsex = p->fr on t->data;printf(” 队头元素为:%dn", x);/*Function: showQueue()Description:显示队列中全部元素Calls: NULLIn put:p链队列指针Return: voidOthers: N

10、ULL*/void showQueue(l in kQueue *p)queueNode *q;if (p->fro nt = NULL)elseprintf(”队列元素为:”);q = p->fro nt;while (q != NULL)printf("%4d", q->data); q = q_>n ext;prin tf("n ”);/*Function: in Queues。Description:顺序队列入队Calls: NULLIn put: p:顺序队列指针Return: voidOthers: NULL*/void in

11、Queues(l in kQueues *p)int兀scan f("%d", &x);p->rear = (p->rear+)%(QUEUEMAX);/ 确定插入位置if (p->front = p->rear)/判断队列是否已满printf(” 队列已满!n");elsep->queup->rear = x;/ 入队printf("%d 入队成功!n", x);/*Function: outQueue_fr on t()Description:顺序队列队头出队Calls: NULLIn put:

12、p:顺序队列指针Return: voidOthers: NULL*/void outQueue_fro nt(l in kQueues *p)int y;if (p->fro nt = p->rear)printf("队列为空! n");elsey = p->queu+(p->front);/先确定出队位置再队头出队printf("队头%d出队成功! n", y);if (p->front = QUEUEMAX)/ 队列循环p->fro nt = 0;/*Function: outQueue_rear()Descrip

13、tion:顺序队列队尾出队Calls: NULLIn put: p:顺序队列指针Return: voidOthers: NULL*void outQueue_rear(l in kQueues *p)int j;if (p->fro nt = p->rear)printf("队列为空!n");else/先确定出队位置再队尾出队j = p_>queup_>rear-_;printf(”队尾%d出队成功!n", j);if (p->rear < 0 && p->front != -1)/ 队列循环p->rear = QUEUEMAX-1;/*Function: doubleQueue()Description:有限制的双队列Calls: in Queues()outQueue_fr ont()outQueue_rear()In put: voidReturn: voidOthers: NULL*void doubleQueue()linkQueues p; / 初始化p.front = -1;p.rear = -1;int i, j;for (i = 0; i < 6; i+)inQueues(&p);/ 入队j

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论