循环队列的链表表示

本文介绍了一种使用链表实现的队列数据结构——链式队列。详细讲解了链式队列的基本操作,包括入队、出队、获取队首元素及清空队列等,并提供了完整的C++模板实现。
 #include"LinkedList.h"
#include
"Queue.h"
template
<class T>
class LinkedQueue
{
public:
    LinkedQueue():rear(
NULL),front(NULL){}
    ~LinkedQueue(){makeEmpty();}
    bool EnQueue(
const T& x);
    bool DeQueue(T
& x);
    bool getFront(T
& x)const;
    void makeEmpty();
    bool 
IsEmpty()const{return(front==rear)?true:false;}
    
int getSize()const;
//    bool IsFull()const{return((rear+1)%maxSize==front)?true:false;}
    friend ostream
& operator<<(ostream& os,LinkedQueue<T>& Q);  
protected:
    LinkNode
<T> *front, *rear;
};

template
<class T>
void LinkedQueue
<T>::makeEmpty()
{
    LinkNode
<T> *p;
    
while(front!=NULL)
    {
        p
=front;
        front
=front->link;
        delete p;
    }
};

template
<class T>
bool LinkedQueue
<T>::EnQueue(const T& x)
{
    
if(front==NULL)
    {
        front
=rear=new LinkNode<T>(x);//空队列时,新结点成为队列的第一个结点,既是对头也是队尾
        
if(front==NULL) return false;
    }
    
else
    {
        rear
->link=new LinkNode<T>(x);//非空时在链尾追加新的结点并更新队尾指针
        
if(rear->link==NULL) return false;
        rear
=rear->link;
    }
    return 
true;
};

template
<class T>
bool LinkedQueue
<T>::DeQueue(T& x)
{
    
if(IsEmpty()==true) return false;
    LinkNode
<T> *p=front;
    x
=front->data;
    front
=front->link;
    delete p;
    return 
true;
};

template
<class T>
bool LinkedQueue
<T>::getFront(T& x)const
{
    
if(IsEmpty()==true) return false;
    x
=front->data;
    return 
true;
};

template
<class T>
int LinkedQueue<T>::getSize()const
{
    LinkNode
<T> *p=front;
    
int k=0;
    
while(p!=NULL)
    {
        p
=p->link;
        k
++;
    }
    return k;
};

template
<class T>
ostream
& operator<<(ostream& os,LinkedQueue<T>& Q)
{
    os
<<"队列中的元素个数有"<<Q.getSize()<<endl;
    LinkNode
<T> *p=Q.front;
    
int i=0;
    
while(p!=NULL)
    {
        os
<<++i<<":"<<p->data<<endl;
        p
=p->link;
    }
    return os;
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值