链队列介绍及其C++实现

介绍

队列的链接存储结构及实现:
改造单链表实现队列的链接存储。

队头指针即为链表的头指针

类声明如下
这里写图片描述

———————————————————————————————————————————————————————————

实现代码

头文件LinkQueue.h

#ifndef LinkQueue_byNim
#define LinkQueue_byNim

#include<iostream>
using namespace std;

template<class T>
struct Node{
    T data; //数据域
    Node *next; //指针域 
};

template<class T>
class LinkQueue
{
    private:
        Node<T> *front,*rear;
    public:
        LinkQueue();
        ~LinkQueue();

        void EnQueue(T e);
        T DeQueue();
        T GetQueue();

        bool empty();
};

template<class T>
LinkQueue<T>::LinkQueue()   //构造函数 
{
    front=new Node<T>;
    front->next=NULL;
    rear=front;
}

template<class T>
LinkQueue<T>::~LinkQueue()
{
    delete front;
}

template<class T>
void LinkQueue<T>::EnQueue(T e)
{
    Node<T> *aNode;
    aNode=new Node<T>;
    aNode->data=e;
    aNode->next=NULL;
    rear->next=aNode;
    rear=aNode;
}

template<class T>
T LinkQueue<T>::DeQueue()
{
    if(rear==front) throw "下溢";
    Node<T> *p;
    p=front->next;
    T x=p->data;
    front->next=p->next;
    if(p->next==NULL) rear=front;   //边界情况队列中只有一个元素 
    delete p;   //及时释放内存,避免内存泄漏 
    return x;
}

template<class T>
T LinkQueue<T>::GetQueue()
{
    if(rear==front) throw "下溢";
    return front->next->data;
}

template<class T>
bool LinkQueue<T>::empty()
{
    if(rear==front)
        return true;
    return false;
}

#endif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值