【数据结构】队列

1.【单链队列】

    利用链表实现队列,在尾部插入头部删除 。分别利用头尾指针指向单链表的头部和尾部,初始化时头尾指针均指向同一处,在进行插入时尾部指针指向新插入的节点,在进行删除时头部指针指向其前一个节点。

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 
 4 typedef struct Queue {
 5     int data;
 6     Queue * next ;
 7 }Queue , *queue;
 8 
 9 typedef struct {
10     queue front; // 队头指针 
11     queue rear; //  队尾指针 
12 }LinkQueue;
13 //构建一个空队列 
14 void InitQueue(LinkQueue &Q) {
15     Q.front = Q.rear = (queue)malloc(sizeof(Queue));
16     if(!Q.front)
17     exit(0);
18     Q.front->next = NULL;
19 }
20 
21 //销毁队列 
22 void DestroyQueue(LinkQueue &Q) {
23     while(Q.front) {
24         Q.rear = Q.front->next;
25         free(Q.front);
26         Q.front = Q.rear;
27     }
28 }
29 
30 //在队尾添加元素 
31 void EnQueue(LinkQueue &Q , int n) {
32     Q.rear->next = (queue)malloc(sizeof(Queue)) ;
33     if(!Q.rear)
34     exit(0);
35     Q.rear->data = n ;
36     Q.rear = Q.rear->next ;
37     Q.rear->next = NULL ;
38 }
39 
40 //取出队头元素 
41 int DeQueue(LinkQueue &Q) {
42     if(Q.front == Q.rear)
43     return -1 ;
44     Queue *q = Q.front->next;
45     int n = Q.front->data;
46     Q.front = q;
47     free(q);
48     return n;    
49 }
50 
51  //输出所有元素 
52  void PrintfQueue(LinkQueue &Q) {
53      for( ; ; ) 
54      {
55          if(Q.front == Q.rear)
56          break;
57          printf("%d" , DeQueue(Q));
58      }
59  }
60  
61 int main() {
62     LinkQueue Q ;
63     InitQueue(Q);
64     for(int i = 0 ; i < 3 ; i++)
65     {
66         int n = 0 ;
67         scanf("%d" , &n);
68         EnQueue(Q,n);
69     }
70     printf("%d\n" , DeQueue(Q));
71     PrintfQueue(Q);     
72 }

 

 

2.【循环队列】

     充分利用存储空间,防止假溢出 

    【假溢出】当用线性表的顺序结构实现队列时,不断的添加元素时,队尾指针指向最后一个元素,无法继续添加,队头元素出队列后,队头存储空间不能利用。存储区未满,但队列发生溢出

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 typedef struct {
 4     int *base ;
 5     int front ;
 6     int rear ;
 7 } SqQueue ;
 8 
 9 //构造空队列 
10 int InitQueue(SqQueue &Q) {
11     Q.base = (int *)malloc(6*sizeof(int));
12     if(!Q.base)
13     exit(0);
14     Q.front = Q.rear = 0;
15 }
16 
17 //在队尾添加元素 
18 int EnQueue(SqQueue &Q , int e) {
19     if((Q.rear+1)%6 == Q.front)
20     return -1 ;
21     Q.base[Q.rear] = e;
22     Q.rear = (Q.rear+1) % 6 ; //补码 
23     return 0 ;
24 } 
25 
26 //取出队头元素 
27 int DeQueue(SqQueue &Q ) {
28     if(Q.rear == Q.front)
29     return -1 ;
30     int n = Q.base[Q.front];
31     Q.front = (Q.front+1)%6;
32     return n ;
33 }
34 //判断循环链表的长度 
35 int QueueLength(SqQueue &Q) {
36     return ( Q.rear - Q.front + 6 ) / 6;
37 }
38 
39 int main() {
40     SqQueue Q ;
41     InitQueue(Q) ;
42     for(int i = 0 ; i <= 4 ; i++ )
43     EnQueue(Q , i);
44     while(Q.front != Q.rear)
45     printf("%d" , DeQueue(Q));
46 }

 

转载于:https://www.cnblogs.com/duolaAbao/p/9435735.html

假设某银行有n个窗口对外接待客户,从早晨银行9点开门起到5点关门,不断有客户进入银行,由于每个窗口在某个时刻只能接待一个客户。因此在客户人数众多时需要在每个窗口前顺次排队,对于刚进银行的客户。如果某个窗口的业务员正空闲,则可上前输业务。反之,若个窗口均有客户所占,他便会排在为数最少的队伍后面。编制一个程序模拟银行的这种业务活动并计算一天中客户在银行的平均逗留时间。 首先从题目分析:N个窗口排队,首先就要建立N个队列来存储排队的用户信息 ,然后算出那个队列最短就用户就到那个队伍排队,同时通过随机生成他办理业务的时间和到来的时间,通过计算用户的到来时间和离开时间就可以计算出某个用户在银行的逗留时间 ;话不多说直接上代码。 下面是主函数,由用户输入银行上下班时间,计算营业多长时间Total_time,如何当前时间小于关门的时间,就一直进入customer_into();函数,用户不断的进来 #define FALSE 0 #define TRUE 1 #define QUEUE_SUM 4 //窗口的数量 int rand_business_time=0, rand_wait_time=0;//定义办理时间,等待时间变量 int Total_time=0,now_tim=0;//总时间,当前时间 int go_time[4] = {0,0,0,0};//定义数组存储每个窗口最后一位办理完业务的时间 int sum_nan[4] = {0,0,0,0};//定义数组存储每个窗口排队的人数 int Sign=TRUE; //是否关门标志位 float Sum_Wait_Time=0.0; //等待的总时间 float Sun_Nan=0.0; //总人数 int open_time;//开门时间 int off_time; //关门时间 int main() { Prompted(); printf("输入银行的24小时制营业时间:如营业时间为9:00--17:00,则应输入:9,17\n"); scanf("%d,%d", &open;_time,&off;_time); Total_time = (off_time - open_time) * 60;//计算银行总营业多少分钟 for (int i = 0; i now_time) { customer_into(); //客户进入函数 } printf("银行关门时间到不再接收客人\n\n"); for (int i = 0; i < QUEUE_SUM; i++) { DisposeQueue(&queue;[i],i);//输入在银行关门前还没有办理完业务的客户信息 } printf("平均时间为%.2f分钟",Sum_Wait_Time/Sun_Nan); /*通过各个客户的总等待时间总和/总人数算出逗留平均时间*/ _getch(); return 0; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值