队列

队列,与栈相反,它是先进先出的一种线性表。(就像一根管道)

 

因为是一种线性表,所以跟线性表类似,队列也有两种存储表示。

用链表表示的队列简称为 链队列。一个链队列显然需要两个分别指示队头和队尾的指针(分别称为头指针和尾指针)才能唯一确定。

上代码:

复制代码
复制代码
 1 #include <stdlib.h>
 2 #include <stdio.h>
 3 
 4 /****** 宏定义 ******/
 5 #define ERROR 0
 6 #define OVERFLOW -2
 7 #define OK 1
 8 
 9 /****** 取别名 ******/
10 typedef int QElemType;
11 typedef int Status;
12 
13 //-------- 单链队列 ——队列的链式存储结构
14 typedef struct QNode{
15     QElemType data;
16     struct QNode *next;
17 }QNode, *QueuePTr;
18 
19 typedef struct{
20     QueuePTr front;    //队头指针
21     QueuePTr rear;    //队尾5指针
22 }LinkQueue;
23 
24 /********* 构造一个空的队列Q *******/
25 Status InitQueue(LinkQueue &Q){
26     Q.front = Q.rear = (QueuePTr)malloc(sizeof(QNode));
27     if(!Q.front)
28         exit(OVERFLOW);
29     Q.front->next =  NULL;
30     return OK;
31 }
32 
33 /********* 销毁队列Q **********/
34 Status DestroyQueue(LinkQueue &Q){
35     while (Q.front){
36         Q.rear = Q.front->next;
37         free(Q.front);
38         Q.front = Q.rear;
39     }
40     return OK;
41 }
42 
43 /******** 插入元素e为新的队尾元素 ********/
44 Status EnQueue(LinkQueue &Q, QElemType e){
45     QueuePTr p;
46     p = (QueuePTr)malloc(sizeof(QNode));
47     if (!p)
48         exit(OVERFLOW);
49     p->data = e;
50     p->next = NULL;
51     Q.rear->next = p;
52     Q.rear = p;
53     return OK;
54 }
55 
56 
57 /********* 删除对头的元素,并用e返回其值 *******/
58 Status DeQueue(LinkQueue &Q, QElemType &e){
59     if (Q.front == Q.rear)
60         return ERROR;
61     QueuePTr p;
62     p = Q.front->next;
63     e = p->data;
64     Q.front->next = p->next;
65     if (Q.rear == p)
66         Q.rear = Q.front;
67     free(p);
68     return OK;
69 }
70 
71 int main(){
72     //
73     return 0;
74 }
复制代码

 

posted @   ouyang_wsgwz  阅读(156)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
展开