队列

//顺序存储:
//头文件:
template<class T>
class AQueue
{
 private:
  int front;
  int rear;
  int count;
  T *QArray;
  int size;
 public:
  AQueue(int MaxQueueSize  = 10);
  ~AQueue(void) {delete [] QArray;}
  bool QInsert(const T& item);
  bool QDelete(T &item);
  void QClear(void) {front = rear = count = 0;}
  T QFront(void) const;
  bool isEmpty(void) const {return count == 0;}
  bool isFull(void) const {return count == size;}
  bool QFront(T &item);
};
//源文件:
#include "AQueue.h"
template <class T>
AQueue<T>::AQueue(int MaxQueueSize)
{
 size = MaxQueueSize;
 QArray = new T[MaxQueueSize];
 front = 0;
 rear = 0;
 count = 0;
}
template <class T>
bool AQueue<T>::QInsert(const T& item)
{
 if (isFull())
 {
  cout<<"inserting into a full queue"<<endl;
  return false;
 }
 QArray[rear] = item;
 rear = (rear+1)%size;
 cout ++;
 return true;
}
template <class T>
bool AQueue<T>::QDelete(T &item)
{
 if (isEmpty())
 {
  cout<<"deleting from an empty queue"<<endl;
  return false;
 }
 item = QArray[front];
 front = (front+1)%size;
 count --;
 return true;
}
template <class T>
bool AQueue<T>::QFront(T &item)
{
 if (isEmpty())
 {
  count<<"reading from an empty queue"<<endl;
  return false;
 }
 item = QArray[front];
 return true;
}
//链式存储:
//头文件:
#include "SLList.h"
template <class T>
class LQueue
{
 private:
  int size;
  SLNode <T> *front; 
  SLNode <T> *rear;
 public:
  LQueue(void) {front = rear = NULL; size = 0; }
  ~LQueue(void){QClear();}
  void QInsert(const T&item);
  bool QDelete(T &item);
  bool QFront(T &item) const;
  int IsEmpty(void) const {return front == NULL;}
  void QClear();  
};
//源文件:
#include "LQueue.h"
template<class T>
void LQueue<T>::QInsert(const T&item)
{
 if (IsEmpty())
 {
  front = rear = new SLNode<T>(item, NULL);
  size = 1;    
 }
 else
 {
  rear->next = new SLNode<T>(item, NULL);
  rear = rear->next;
  size ++;
 }
};
template<class T>
bool LQueue<T>::QDelete(T &item)
{
 if (IsEmpty())
 {
  cout<<"delete from an empty queue"<<endl;
  return false;
 }
 SLNode<T> *temp = front;
 item = temp->data;
 front = front->next;
 cout --;
 delete temp;
 if (size == 0) rear = NULL;
 return true; 
};
template<class T>
bool LQueue<T>::QFront(T &item) const
{
 if (IsEmpty())
 {
  cout<<"reading from an empty queue"<<endl;
  return false;
 }
 item = front->data;
 return true;
};
template<class T>
void LQueue<T>::QClear()
{
 while (!IsEmpty())
 {
  rear = front;
  front = front->next;
  delete rear;
  size --;
 }
 rear = NULL;
}; 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值