队列的链式表示和实现

 1 //队列的链式表示和实现
 2 //链队类型定义
 3 #define MAXQIZE 100        //最大队列长度 
 4 typedef struct Qnode{
 5     QElemType data;
 6     struct Qnode *next;
 7 }Qnode,*QuenePtr;
 8 typedef struct{
 9     QuenePtr front;        //队头指针 
10     QuenePtr rear;        //队尾指针 
11 }LinkQuene; 
12 //链队的初始化
13 Status InitQuene(LinkQuene &Q){
14     Q.front=Q.rear=(QuenePtr)malloc(Sizeof(Qnode));
15     if(!Q.front)    exit(OVERFLOW);
16     Q.front->next=NULL;
17 } 
18 //链队的销毁
19 Status DestroyQuene(LinkQuene &Q){
20     while(Q.front){
21         p=Q.front->next;
22         free(Q.front);
23         Q.front=p;
24     }
25     return OK;
26 } 
27 //链队的入队
28 Status EnQuene(LinkQuene &Q,QElemType e){
29     p=(QuenePtr)malloc(sizeof(Qnode));
30     if(!p) exit(OVERFLOW);
31     p->data=e;
32     p->next =NULL;
33     Q.rear->next=p;
34     Q.rear=p;
35     return OK;
36 } 
37 //链队的出队
38 Status DeQuene(LinkQuene &Q,QElemType &e){
39     if(Q.rear==Q.front)    return ERROR;
40     p=Q.front->next;
41     e=p->data;
42     Q.front->data=p->next;
43     if(Q.rear==p) Q.rear=Q.front;
44     free(p);
45     return OK;
46 }
47 //求链队的队头元素
48 Stauts Getahead(LinkQuene Q,QElemType &e){
49     if(Q.front==Q.rear)    return ERROR;
50     e=Q.front->next->data;
51     return OK;
52 } 

猜你喜欢

转载自www.cnblogs.com/suqf/p/9853403.html