关于链表的数据成员和一般的方法
下面是头文件:
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); };
方法的定义:
#include <stdio.h> #include "QueueTp.h" 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 (isempty()) { return false; } s=front->item; items--; Node *temp; temp=front; front=front->next; delete temp; if (items==0) { rear=nullptr; } return true; }
时间: 2024-10-24 11:15:57