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

队列的链式存储结构,其实就是线性表的单链表,只不过它只能尾进头出而已,我们把它简称为链队列。为了操作上的方便,我们将队头指针指向链队列的头节点,而队尾指针指向终端节点。空队列时,front和rear都指向头节点。
这里写图片描述
示例程序:(改变自《大话数据结构》)

#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;
}

输出为;
这里写图片描述
总的来说,如果可以确定队列的最大值,建议用循环队列,如果不能预估队列的长度,则用链队列。

转载自:http://blog.csdn.net/jnu_simba/article/details/8842836

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值