Loop queue:
1. The method to judge the empty team in the loop queue is to judge front==rear, and the method to judge front=(rear+1)%maxSize. (I once thought about why you don't use a length to represent the captain. When length==maxSize, the team is full) The reason is that in frequent queue operations, an extra variable will increase the execution time a lot, so it is better to waste an array space.
2. A chain queue represented by a single linked list is particularly suitable for situations where data elements change greatly 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){//Enter the team if(isFull()) return false; element[rear]=x; rear=(rear+1)%maxSize; return true; } bool DeQueue(T& x){//Get out of the team if(isEmpty()) return false; x=element[front]; front=(front+1)%maxSize; return true; } bool getFront(T& x){//Get the team leader element if(isEmpty()) return false; x=element[front]; return true; } void makeEmpty(){//The queue is empty front=rear=0; } bool isEmpty()const{//Judge whether the queue is empty return (rear==front)?true:false; } bool isFull()const{//Is the queue 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. Join the team"<<endl; cout<<"2. Obtain the team leader element"<<endl; cout<<"3. Departure"<<endl; cout<<"4. Queue empty"<<endl; cout<<"5. Obtain 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; }
Then there is a chain queue, 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. Join the team"<<endl; cout<<"2. Obtain the team leader element"<<endl; cout<<"3. Departure"<<endl; cout<<"4. Queue empty"<<endl; cout<<"5. Obtain 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; }
The above example of C++ implementing circular queues and chain queues is all the content I have shared with you. I hope you can give you a reference and I hope you can support me more.