码迷,mamicode.com
首页 > 其他好文 > 详细

2.2 先进先出结构:队列

时间:2016-07-19 09:18:37      阅读:275      评论:0      收藏:0      [点我收藏+]

标签:

2-8SeqQueue.h

 1 //顺序队列操作 
 2 #define QUEUEMAX 15
 3 typedef struct
 4 {
 5     DATA data[QUEUEMAX]; //队列数组 
 6     int head; //队头 
 7     int tail; //队尾 
 8 }SeqQueue;
 9 SeqQueue *SeqQueueInit()
10 {
11     SeqQueue *q;
12     if(q=(SeqQueue *)malloc(sizeof(SeqQueue))) //申请保存队列的内存 
13     {
14         q->head = 0;//设置队头 
15         q->tail = 0;//设置队尾 
16         return q;
17     }else
18         return NULL; //返回空 
19 }
20 void SeqQueueFree(SeqQueue *q) //释放队列
21 {
22     if (q!=NULL)
23         free(q);
24 } 
25 int SeqQueueIsEmpty(SeqQueue *q)  //队列是否为空 
26 {
27     return (q->head==q->tail);
28 }
29 int SeqQueueIsFull(SeqQueue *q)//队列是否已满 
30 {
31     return (q->tail==QUEUEMAX);
32 }
33 int SeqQueueLen(SeqQueue *q) //获取队列长度 
34 {
35     return(q->tail-q->head);
36 }
37 int SeqQueueIn(SeqQueue *q,DATA data)//顺序队列的入队函数
38 {
39     if(q->tail==QUEUEMAX)
40     { 
41         printf("队列已满!\n");
42         return(0);
43     }else{
44         q->data[q->tail++]=data;
45         return(1);
46     }
47 }
48 DATA *SeqQueueOut(SeqQueue *q)//顺序队列的出队
49 {
50     if(q->head==q->tail)
51     {
52         printf("\n队列已空!\n");
53         return NULL;
54     }else{
55         return &(q->data[q->head++]);
56     }
57 }
58 DATA *SeqQueuePeek(SeqQueue *q) //获取队头元素
59 {
60     if(SeqQueueIsEmpty(q))
61     {
62         printf("\n队列为空!\n");
63         return NULL; 
64     }else{
65         return &(q->data[q->head]);
66     }
67 } 

2-9 CycQueue.h

 1 //循环队列 
 2 #define QUEUEMAX 15
 3 typedef struct
 4 {
 5     DATA data[QUEUEMAX]; //队列数组 
 6     int head; //队头 
 7     int tail; //队尾 
 8 }CycQueue;
 9 CycQueue *CycQueueInit()
10 {
11     CycQueue *q;
12     if(q=(CycQueue *)malloc(sizeof(CycQueue))) //申请保存队列的内存 
13     {
14         q->head = 0;//设置队头 
15         q->tail = 0;//设置队尾 
16         return q;
17     }else
18         return NULL; //返回空 
19 }
20 void CycQueueFree(CycQueue *q) //释放队列
21 {
22     if (q!=NULL)
23         free(q);
24 } 
25 int CycQueueIsEmpty(CycQueue *q)  //队列是否为空 
26 {
27     return (q->head==q->tail);
28 }
29 int CycQueueIsFull(CycQueue *q)//队列是否已满 
30 {
31     return ((q->tail+1)%QUEUEMAX==q->head);
32 }
33 int CycQueueIn(CycQueue *q,DATA data)//入队函数
34 {
35     if((q->tail+1)%QUEUEMAX == q->head )
36     { 
37         printf("队列已满!\n");
38         return 0;
39     }else{
40         q->tail=(q->tail+1)%QUEUEMAX;//求列尾序号 
41         q->data[q->tail]=data;
42         return 1;
43     }
44 }
45 DATA *CycQueueOut(CycQueue *q)//循环队列的出队函数
46 {
47     if(q->head==q->tail) //队列为空 
48     {
49         printf("队列已空!\n");
50         return NULL;
51     }else{
52         q->head=(q->head+1)%QUEUEMAX;
53         return &(q->data[q->head]);
54     }
55 }
56 int CycQueueLen(CycQueue *q) //获取队列长度 
57 {
58     int n;
59     n=q->tail-q->head;
60     if(n<0)
61         n=QUEUEMAX+n;
62     return n;
63 }
64 DATA *CycQueuePeek(CycQueue *q) //获取队定中第1个位置的数据
65 {
66     if(q->head==q->tail)
67     {
68         printf("队列已经为空!\n");
69         return NULL; 
70     }else{
71         return &(q->data[(q->head+1)%QUEUEMAX]);
72     }
73 } 

2-10 BankQueue.c

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <time.h>
 4 typedef struct
 5 {
 6     int num; //顾客编号 
 7     long time;//进入队列时间 
 8 }DATA;
 9 #include "2-9 CycQueue.h"
10 int num;//顾客序号 
11 void add(CycQueue *q) //新增顾客排列 
12 {
13     DATA data;
14     if(!CycQueueIsFull(q)) //如果队列未满
15     {
16         data.num=++num;
17         data.time=time(NULL);
18         CycQueueIn(q,data);
19     }
20     else
21         printf("\n排队的人太多,请稍候再排队!\n"); 
22 }
23 void next(CycQueue *q) //通知下一顾客准备 
24 {
25     DATA *data;
26     if(!CycQueueIsEmpty(q)) //若队列不为空 
27     {
28         data=CycQueueOut(q); //取队列头部的数据 
29         printf("\n请编号为%d的顾客办理业务!\n",data->num);
30     }
31     if(!CycQueueIsEmpty(q)) //若队列不为空 
32     {
33         data=CycQueuePeek(q);//取队列中指定位置的数据 
34         printf("请编号为%d的顾客准备,马上将为您理业务!\n",data->num);      
35     }
36 }
37 int main()
38 {
39     CycQueue *queue1;
40     int i,n;
41     char select;
42     num=0;//顾客序号 
43     queue1=CycQueueInit(); //初始化队列 
44     if(queue1==NULL)
45     {
46         printf("创建队列时出错!\n");
47         getch();
48         return 0;
49     }
50     do{
51         printf("\n请选择具体操作:\n");
52         printf("1.新到顾客\n");
53         printf("2.下一个顾客\n");
54         printf("0.退出\n") ;
55         fflush(stdin);
56         select=getch();
57         switch(select)
58         {
59             case 1:
60                 add(queue1);
61                 printf("\n现在共有%d位顾客在等候!\n",CycQueueLen(queue1));
62                 break;
63             case 2:
64                 next(queue1);
65                 printf("\n现在共有%d位顾客在等候!\n",CycQueueLen(queue1));
66                 break;
67             case 0:
68                 break;
69         }        
70     }while(select!=0);
71     CycQueueFree(queue1); //释放队列
72     getch();
73     return 0;
74 }

 

2.2 先进先出结构:队列

标签:

原文地址:http://www.cnblogs.com/wozixiaoyao/p/5683119.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!