SeQueue:
- <pre name="code" class="cpp">/*=============================================================================
- #
- # Author: liangshu - cbam
- #
- # QQ : 756029571
- #
- # School : 哈尔滨理工大学
- #
- # Last modified: 2015-10-26 14:37
- #
- # Filename: 顺序循环队列一 副本 (3).cpp
- #
- # Description:
- # The people who are crazy enough to think they can change the world, are the ones who do !
- =============================================================================*/
- #
- using namespace std;
- template<class T>
- class SeqQueue
- {
- public:
- SeqQueue(int sz):rear_(0),front_(0),num(0),Max_size(sz){
- Queue = new T[sz];
- if(Queue == NULL){
- cout<<"runtime error!"<<endl;return ;
- }
- }
- ~SeqQueue(){
- delete[] Queue;
- }
- void Clear_queue();
- bool Is_empty()const;
- bool Is_full()const;
- bool Append(const T &item);
- T Delete();
- T Get_front()const;
- void Print()const;
- private:
- <pre name="code" class="cpp"> int rear_;
- int front_;
- int num;
- int Max_size;
- T * Queue;
- };
- template<class T>
- void SeqQueue<T>::Clear_queue(){
- this -> num = 0;
- this -> rear_ = 0;
- this -> front_ = 0;
- }
- template<class T>
- bool SeqQueue<T>::Is_empty()const{
- return num == 0;
- }
- template<class T>
- bool SeqQueue<T>::Is_full()const{
- return num == Max_size;
- }
- template<class T>
- bool SeqQueue<T>::Append(const T &item){
- if(Is_full()){
- cout<<"The queue is full!"<<endl;return false;
- }
- Queue[rear_] = item;
- rear_ = (rear_ + 1== Max_size ? 0 : rear_ + 1);
- num++;
- return true;
- }
- template<class T>
- T SeqQueue<T>::Get_front()const{
- if(Is_empty()){
- cout<<"There is no element available!"<<endl;return false;
- }
- return Queue[front_];
- }
- template<class T>
- T SeqQueue<T>::Delete(){
- if(Is_empty()){
- cout<<"There is no element available!"<<endl;return false;
- }
- T tmp = Queue[front_];
- front_ = (front_ ++) % Max_size;
- num --;
- return tmp;
- }
- template<class T>
- void SeqQueue<T>:: Print()const{
- cout<<"front -> ";
- for(int i = 0; i < num; i++){
- cout<<Queue[(front_ + i) % Max_size]<<" -> ";
- }
- cout<<"rear"<<endl;
- }
Test.cpp
- #include<iostream>
- #include"SeQueue.h"
- using namespace std;
- int main()
- {
- SeqQueue<int>queue_(6);
- for(int i = 1; i <= 5; i++){
- queue_.Append(i);
- }
- queue_.Print();
- cout<<queue_.Get_front()<<endl;
- queue_.Delete();
- cout<<queue_.Get_front()<<endl;
- queue_.Append(6);
- queue_.Append(7);
- queue_.Append(8);
- queue_.Print();
- queue_.Clear_queue();
- cout<<queue_.Get_front();
- return 0;
- }