数据结构——链式队列(c++)

Link.h

//Link.h
template <class T> class Link { 
public:  
    T       data;                       // 用于保存结点元素的内容 
    Link    * next;                     // 指向后继结点的指针 

    Link(const T info, Link* nextValue = NULL)  {   // 具有两个参数的Link构造函数 
        data = info; 
        next = nextValue; 
    } 
    Link(Link* nextValue = NULL)  {     // 具有一个参数的Link构造函数 
        next = nextValue; 
    } 
}; 

myQueue.h

//myQueue.h
template <class T>  
class Queue  {  
public:                             // 队列的运算集 
    void clear();                   // 变为空队列 
    bool enQueue(const T item);     // item入队,插入队尾,成功则返回真否则返回假 
    bool deQueue(T item);           // 返回队头元素并从队列中删除,成功则返回真 
    bool front(T* item);            // 返回队头元素,但不删除,成功则返回真 
    bool isEmpty();                 // 返回真,若队列已空 
    bool isFull();                  // 返回真,若队列已满 
    void print(); 
}; 

lnkQueue.h

//lnkQueue.h
#include <cstdlib> 
#include <iostream> 
#include "Link.h" 
#include "myQueue.h" 

using namespace std; 

template <class T>  
class lnkQueue: public Queue <T> {  
private:     
    int         size;                   // 队列中当前元素的个数 
    Link<T>* front;                     // 表示队头的指针 
    Link<T>* rear;                      // 表示队尾的指针 
public:                                 // 队列的运算集 
    lnkQueue()  {                   // 创建队列的实例 
        size = 0; 
        front = rear = NULL; 
    } 
    ~lnkQueue()  {                     // 消除该实例,并释放其空间 
        clear(); 
    } 
    void print() {                      // 打印队列 
        if (front == NULL)  { 
            cout << "队列为空" << endl; 
        } 
        Link<T>*p = front; 
        while(p != NULL) { 
              cout << p->data << " "; 
              p = p->next;   
        }  
        cout << endl; 
    } 
    void clear();                       // 清空队列 
    bool enQueue(const T item);         //  item入队,插入队尾 
    bool deQueue(T* item);              // 返回队头元素并从队列中删除。注意:队列为空,没有元素可出队 
    bool getFront(T* item);             // 返回队头元素,但不删除。注意:队列为空时,没有元素可读。item为引用亦可
}; 


template<class T>
void lnkQueue<T>::clear()
{
    while(front!=NULL)
    {
        rear=front;
        front=front->next;
        delete rear;
    }
    rear=NULL;
    size=0;
}


template<class T>
bool lnkQueue<T>::enQueue(const T item)
{
    if(rear==NULL)  //空队列
    {
        front=rear=new Link<T>(item,NULL);
    }
    else
    {
        rear->next=new Link<T>(item,NULL);
        rear=rear->next;
    }
    size++;
    return true;

}


template<class T>
bool lnkQueue<T>::deQueue(T * item)
{
    Link<T> * tmp;
    if(size==0)
    {
        cout<<"队列为空"<<endl;
        return false;
    }
    *item=front->data;
    tmp=front;
    front=front->next;
    delete tmp;
    if(front==NULL)
    {
        rear=NULL;
    }
    size--;
    return true;
}


template<class T>
bool lnkQueue<T>::getFront(T * item)
{
    if(size==0)
    {
        cout<<"队列为空"<<endl;
        return false;
    }
    *item=front->data;
    return true;
}

main.cpp

//main.cpp
#include <cstdlib>    
#include <iostream>    

#include "lnkQueue.h"    

using namespace std;   

int main(int argc, char *argv[])   
{   
    lnkQueue<int> Qu;
    for(int i=1;i<=5;i++)
    {
        Qu.enQueue(i);
    }
    cout<<"输出元素:";
    Qu.print();
    cout<<"入队操作:"<<endl;
    Qu.enQueue(8);
    cout<<"输出元素:";
    Qu.print();
    cout<<"出队操作";
    int del;
    Qu.deQueue(&del);
    cout<<"出队元素为:"<<del<<endl;
    cout<<"输出元素:";
    Qu.print();
}   

这里写图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值