数据结构:队列的链式存储结构

队列的链式存储结构,其实就是线性表的单链表,只不过它只能尾进头出而已,我们把它简称为链队列。为了操作上的方便,我们将队头指针指向链队列的头节点,而队尾指针指向终端节点。空队列时,front和rear都指向头节点。


示例程序:(改变自《大话数据结构》)

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include<iostream>
using  namespace std;

typedef  int ElemType;

typedef  struct Node
{
    ElemType data;
     struct Node *next;
} Node, *NodePtr;

typedef  struct
{
    NodePtr front; /* 队头、队尾指针 */
    NodePtr rear;
} LinkQueue;
/* 构造一个空队列 */
bool InitQueue(LinkQueue *Lp)
{
    cout <<  "Init Queue ..." << endl;
    NodePtr p = (NodePtr)malloc( sizeof(Node));
    p->next =  NULL;
    Lp->front = Lp->rear = p;
     return  true;
}
/* 销毁队列,包括头节点 */
bool DestroyQueue(LinkQueue *Lp)
{
    cout <<  "Destroy Queue ..." << endl;
     while (Lp->front)
    {
        Lp->rear = Lp->front->next;
        free(Lp->front);
        Lp->front = Lp->rear;
    }

     return  true;
}
/* 清为空队列,保留头节点 */
bool ClearQueue(LinkQueue *Lp)
{
    cout <<  "Clear Queue ..." << endl;
    NodePtr p = Lp->front->next;
    Lp->front->next =  NULL;
    Lp->rear = Lp->front;
    NodePtr q;

     while (p)
    {
        q = p->next;
        free(p);
        p = q;
    }

     return  true;
}

bool QueueEmpty(LinkQueue LQ)
{
     return LQ.front == LQ.rear;
}

int QueueLength(LinkQueue LQ)
{
     int i =  0;
     if (LQ.front ==  NULL)
         return  0;
    NodePtr p = LQ.front->next;
     while (p)
    {
        ++i;
        p = p->next;
    }

     return i;
}

bool GetHead(LinkQueue LQ, ElemType *pe)
{
    NodePtr p;
     if (LQ.front == LQ.rear)
         return  false;
    p = LQ.front->next;
    *pe = p->data;
    cout <<  "Get Head Item : " << *pe << endl;
     return  true;
}

/* 插入元素Elem为队列的新的队尾元素 */
bool EnQueue(LinkQueue *Lp, ElemType Elem)
{
    cout <<  "EnQueue Item " << Elem << endl;
    NodePtr s = (NodePtr)malloc( sizeof(Node));
    s->data = Elem;
    s->next =  NULL;
    Lp->rear->next = s;
    Lp->rear = s;

     return  true;
}
/*删除队列的队头元素,用*pe返回其值 */
bool DeQueue(LinkQueue *Lp, ElemType *pe)
{
     if (Lp->front == Lp->rear)
         return  false;
    NodePtr p = Lp->front->next;
    *pe = p->data;
    cout <<  "DeQueue Item " << *pe << endl;
    Lp->front->next = p->next;
     if (Lp->rear == p) /* 若队头就是队尾,则删除后将rear指向头结点*/
        Lp->rear = Lp->front;
    free(p);

     return  true;
}
/* 从队头到队尾依次对队列中每个元素输出 */
bool QueueTraverse(LinkQueue LQ)
{
    cout <<  "Queue Traverse ..." << endl;
    NodePtr p = LQ.front->next;
     while (p)
    {
        cout << p->data <<  ' ';
        p = p->next;
    }

    cout << endl;
     return  true;
}

int main( void)
{
    LinkQueue LQ;
    InitQueue(&LQ);
     for ( int i =  0; i <  5; i++)
        EnQueue(&LQ, i);
    QueueTraverse(LQ);
     int result;
    GetHead(LQ, &result);
    DeQueue(&LQ, &result);
    QueueTraverse(LQ);
     if (!QueueEmpty(LQ))
        cout <<  "Queue Length : " << QueueLength(LQ) << endl;
     /*ClearQueue(&LQ);*/
    DestroyQueue(&LQ);
    cout <<  "Queue Length : " << QueueLength(LQ) << endl;

     return  0;
}
输出为:


总的来说,如果可以确定队列的最大值,建议用循环队列,如果不能预估队列的长度,则用链队列。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值