NashZhou

广告算法工程师,目前致力于关键词广告的效果自动优化

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 }

posted on 2011-05-16 17:45  NashZhou  阅读(171)  评论(0编辑  收藏  举报

导航