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

来源:互联网 发布:魔力宝贝热砂数据 编辑:程序博客网 时间:2024/05/19 18:15

Link.h

//Link.htemplate <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.htemplate <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 0
原创粉丝点击