链表模板c++

关于链表的数据成员和一般的方法

template <class T>
class QueueTp{
private:
    struct Node{T item; struct Node * next;};
    enum {Q_SIZE=10};
    Node *front;
    Node *rear;
    int items;
    const int qsize;
    QueueTp():qsize(0){};
    QueueTp &operator=(const QueueTp &q) {return *this;}
public:
    QueueTp(int n=Q_SIZE);
    ~QueueTp ();
    bool isempty() const;
    bool isfull() const;
    int queuecount() const;
    bool enqueue(const T & s);
    bool dequeue(T & s);
};

template<class T>
QueueTp<T>::QueueTp(int n):qsize(n){
    front=rear=nullptr;
    items=0;
}

template<class T> QueueTp<T>::~QueueTp(){ Node *temp; while (front!=nullptr) { temp=front; front=front->next; delete temp; } } template<class T> bool QueueTp<T>::isempty() const{ return items==0; } template<class T> bool QueueTp<T>::isfull() const{ return items==qsize; }
template<class T> int QueueTp<T>::queuecount()const{ return items; } template<class T> bool QueueTp<T>:: enqueue(const T & s){ if(isfull()) return false; Node *temp(s,nullptr); if (front==nullptr) { front=temp; } items++; rear->next=temp; rear=temp; rear->next=nullptr; return true; } template<class T> bool QueueTp<T>:: dequeue(T & s){
   if (isfull())
        return false;

    Node *temp=new Node;

    temp->item=s;

    temp->next=nullptr;

    items++;

    if (front==nullptr) {

      front=temp;

    }

    else

        rear->next=temp;

    rear=temp;
return true;
 
   
}
 
  

转载于:https://www.cnblogs.com/KyleRuan/p/4176352.html

猜你喜欢

转载自blog.csdn.net/weixin_33674976/article/details/93435595
今日推荐