C++ implements examples of circular and chained queues

  • 2020-06-01 10:29:33
  • OfStack

Circular queue:

1. In a circular queue, front==rear; in a full queue, front= (rear+1) %maxSize. (I once wondered why I didn't use 1 length for captain, when length==maxSize the queue was full.) the reason was that in frequent queue operations, adding 1 more variable would significantly increase the execution time, so it would be more cost-effective to waste 1 array space.

2. The chain-linked queue represented by single linked list is particularly suitable for the situation with large variation of data elements, and there is no overflow.


template<class T>
class SeqQueue{
 protected:
  T *element;
  int front,rear;
  int maxSize;
 public:
  SeqQueue(int sz=10){
   front=rear=0;
   maxSize=sz;
   element=new T[maxSize];
  }
  ~SeqQueue(){
   delete[] element;
  }
  bool EnQueue(const T& x){// The team  
   if(isFull()) return false;
   element[rear]=x;
   rear=(rear+1)%maxSize;
   return true;
  }
  bool DeQueue(T& x){// Out of the team  
   if(isEmpty()) return false;
   x=element[front];
   front=(front+1)%maxSize;
   return true;
  }
  bool getFront(T& x){// Gets the team leader element  
   if(isEmpty()) return false;
   x=element[front];
   return true;
  }
  void makeEmpty(){// The queue empty  
   front=rear=0;
  }
  bool isEmpty()const{// Determine if the queue is empty  
   return (rear==front)?true:false;
  }
  bool isFull()const{// Whether the queue is full 
    return ((rear+1)%maxSize==front)?true:false;
  }
  int getSize()const{
   return (rear-front+maxSize)%maxSize;
  }
};

The test code is as follows:


void menu(){
 cout<<"1. The team "<<endl;
 cout<<"2. Gets the team leader element "<<endl;
 cout<<"3. Out of the team "<<endl;
 cout<<"4. The queue empty "<<endl;
 cout<<"5. Gets the number of elements in the team "<<endl;
 cout<<"6. exit "<<endl;
} 
void function(int num,SeqQueue<int> *sq){
 switch(num){
  int x;
  case 1:
   cin>>x;
   sq->EnQueue(x);
   break;
  case 2:
   sq->getFront(x);
   cout<<x<<endl;
   break;
  case 3:
   sq->DeQueue(x);
   break;
  case 4:
   sq->makeEmpty();
   break;
  case 5:
   x=sq->getSize();
   cout<<x<<endl;
   break; 
  default:
   exit(1);
 }
}
int main(int argc, char** argv) {
 SeqQueue<int> *sq=new SeqQueue<int>;
 int num;
 while(true){
  menu();
  cin>>num;
  function(num,sq);
 } 
 delete sq;
 return 0; 
}

The implementation class code and test code are as follows:


#include <iostream>
using namespace std;
template<class T> 
struct LinkNode{
 T data;
 LinkNode<T> *link;
 LinkNode(T& x,LinkNode<T> *l=NULL){
  data=x;
  link=l;
 }
};
template<class T>
class LinkedQueue{
 protected:
  LinkNode<T> *front,*rear;
 public:
  LinkedQueue(){
   front=rear=NULL;
  }
  ~LinkedQueue(){
   makeEmpty();
  }
  bool enQueue(T& x){
   if(front==NULL)
    front=rear=new LinkNode<T>(x);
   else{
    rear=rear->link=new LinkNode<T>(x);
   }
   return true;
  }
  bool deQueue(T& x){
   if(isEmpty()) return false;
   LinkNode<T> *p=front;
   x=front->data;
   front=front->link;
   delete p;
   return true;
  }
  bool getFront(T& x)const{
   if(isEmpty()) return false;
   x=front->data;
   return true;
  }
  void makeEmpty(){
   LinkNode<T> *p;
   while(front!=NULL){
    p=front;
    front=front->link;
    delete p;
   }
  }
  bool isEmpty()const{
   return (front==NULL)?true:false;
  }
  int getSize()const{
   LinkNode<T> *p;
   int count=0;
   p=front;
   while(p!=NULL){
    count++;
    p=p->link;
   } 
  return count;
  }
}; 
void menu(){
 cout<<"1. The team "<<endl;
 cout<<"2. Gets the team leader element "<<endl;
 cout<<"3. Out of the team "<<endl;
 cout<<"4. The queue empty "<<endl;
 cout<<"5. Gets the number of elements in the team "<<endl;
 cout<<"6. exit "<<endl;
} 
void function(int num,LinkedQueue<int> *lq){
 switch(num){
  int x;
  case 1:
   cin>>x;
   lq->enQueue(x);
   break;
  case 2:
   lq->getFront(x);
   cout<<x<<endl;
   break;
  case 3:
   lq->deQueue(x);
   break;
  case 4:
   lq->makeEmpty();
   break;
  case 5:
   x=lq->getSize();
   cout<<x<<endl;
   break; 
  default:
   exit(1);
 }
}
int main(int argc, char** argv) {
 LinkedQueue<int> *lq=new LinkedQueue<int>;
 int num;
 while(true){
  menu();
  cin>>num;
  function(num,lq);
 } 
 delete lq;
 return 0; 
}

Related articles: