Queue_L.h

1 #include <cstdio>
2 #include <cstdlib>
3 typedef int QueueElemType;
4 typedef struct node
5 {
6 QueueElemType data;
7 struct node *next;
8 }SNode;
9 typedef struct
10 {
11 SNode *front,*rear;
12 }LQueue;
13 void initQueue(LQueue *Q);//置空队列
14 void enQueue(LQueue *Q,const QueueElemType &x);//入队
15 QueueElemType deQueue(LQueue *Q);//出队
16 bool lqueueIsEmpty(const LQueue *Q);
17
18 void initQueue(LQueue *Q)//置空队列
19 {
20 Q->front=Q->rear=(SNode *)malloc(sizeof(SNode));//头结点存储队列长度,初始为0
21 Q->front->data=0;
22 Q->front->next=NULL;
23 }
24 void enQueue(LQueue *Q,const QueueElemType &x)//入队
25 {
26 SNode *s=(SNode *)malloc(sizeof(SNode));
27 s->data=x;
28 s->next=NULL;
29 Q->rear->next=s;
30 Q->rear=s;
31 Q->front->data+=1;
32 }
33 QueueElemType deQueue(LQueue *Q)//出队
34 {
35 QueueElemType x;
36 if(Q->front->data==0)
37 {
38 printf("deQueue error 队空\n");
39 exit(1);
40 }
41 else
42 {
43 SNode *p=Q->front->next;//定位第一个结点
44 x=p->data;
45 Q->front->next=p->next;
46 Q->front->data-=1;
47 if (Q->front->data==0)
48 {
49 Q->rear=Q->front;
50 }
51 free(p);
52 }
53 return x;
54 }
55
56 bool lqueueIsEmpty(const LQueue *Q)
57 {
58 if (Q->front->data==0)
59 {
60 return true;
61 }
62 else return false;
63 }

转载于:https://www.cnblogs.com/2010Freeze/articles/2047916.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值