C++ implements circular queues

  • 2020-06-23 01:34:44
  • OfStack

This article is an example of C++ to share the implementation of circular queue specific code, for your reference, the specific content is as follows

circularQueue.h


#pragma once
#pragma once
#ifndef CIRCULARQUEUE_H
#define CIRCULARQUEUE_H
 
#include<iostream>
#include<ostream>
using std::cout;
using std::cin;
using std::endl;
using std::ostream;
template<class T> class cirQueue;
 
template<typename T>
class cirQueue
{
public:
 cirQueue(int sz);
 ~cirQueue();
 void push(const T& elem);// Into the team 
 void pop(T& elem);// Out of the team 
 bool empty();// See if the queue is empty 
 int getSize();// Returns the number of elements in the queue 
 void clearQueue();// Clears the elements in the queue 
 void print();// Print the elements in the queue 
 int getfront() { return front; }
 int getrear() { return rear; }
 bool getTop(T& elem);// Read the first element of the queue 
 
 template<typename T>
 friend ostream& operator<<(ostream& os, cirQueue<T>& queue);
 
private:
 bool _full()const;// Determine if the queue is full 
 int maxsize;// The maximum space in the queue 
 T* element;// An array of elements stored in a queue 
 int front;// Simulate team head pointer 
 int rear;// Simulate the tail pointer 
};
 
 
template<typename T>
cirQueue<T>::cirQueue(int sz) {
 maxsize = sz;
 element = new T[maxsize];
 if (element == nullptr)
 cout << " Memory allocation failure " << endl;
 front = 0;
 rear = 0;
}
 
 
template<typename T>
cirQueue<T>::~cirQueue() {
 if (element != nullptr)
 delete element;
}
 
// Into the team 
template<typename T>
void cirQueue<T>::push(const T& elem) {// You need to make sure that the pointer at the end of the line is different from the first element 1 A position 
 if (rear > (maxsize - 1))
 rear -= maxsize ;
 if (front > (maxsize - 1))
 front -= maxsize ;
 if (!_full()) {// Queue under condition  
 element[rear++] = elem;// The back of the line moved back 1 position 
 //++rear;
 }
 else {
 cout << " The queue is full, cannot be inserted! " << endl;
 return;
 }
}
 
// Out of the team 
template<typename T>
void cirQueue<T>::pop(T& elem) {
 if (rear > (maxsize - 1))
 rear -= (maxsize - 1);
 if (front > (maxsize - 1))
 front -= (maxsize - 1);
 if (!empty()) {// The queue is not empty 
 elem = element[front++];// The head of the queue moves backwards 1 position 
 element[front - 1] = 0;// zero 
 }
 else {
 cout << " The queue is empty! " << endl;
 return;
 }
}
 
// See if the queue is empty 
template<typename T>
bool cirQueue<T>::empty() {
 if (front == rear)// To be determined 
 return true;
 return false;
}
 
// Returns the number of elements in the queue 
template<typename T>
int cirQueue<T>::getSize() {
 int num = 0;
 if (front <= rear)
 return rear - front;
 else
 return maxsize - front + rear + 1;
}
 
// Clears the elements in the queue 
template<typename T>
void cirQueue<T>::clearQueue() {
 if (!empty())
 {
 int Index = 0;
 while (front < rear) {//front Close to rear
  element[front++] = 0;
  if (front == rear)
  return;
 }
 if (rear < front) {
  while (front <= maxsize - 1)// delete front To the end of the array 
  element[front++] = 0;
  front -= maxsize;
  while (front < rear) {// delete front to rear The data of 
  element[front++] = 0;
  if (front == rear)
   return;
  }
 }
 }
}
 
// Print the elements in the queue 
template<typename T>
void cirQueue<T>::print() {// with clearQueue The function principle 1 Will, front Replace with Index
 if (!empty())
 {
 int Index = front;
 while (Index < rear) {
  cout << element[Index++] << " ";
  if (Index == rear) {
  cout << endl;
  return;
  }
 }
 if (rear < Index) {
  while (Index <= maxsize - 1)
  cout << element[Index++] << " ";
  Index -= maxsize;
  while (Index < rear) {
  cout << element[Index++] << " ";
  if (Index == rear) {
   cout << endl;
   return;
  }
  }
 }
 }
}
 
// Read the first element of the queue 
template<typename T>
bool cirQueue<T>::getTop(T& elem) {
 if (!empty()) {
 elem = element[front];
 return true;
 }
 return false;
}
 
template<typename T>
ostream& operator<<(ostream& os, cirQueue<T>& queue) {
 os << " The number of elements in the queue is: " << queue.getSize() << endl;
 return os;
}
 
// Determine if the queue is full 
template<typename T>
bool cirQueue<T>::_full()const {
 if (front - rear == 1 || front - rear == -maxsize + 1)
 return true;
 return false;
}
 
#endif // !CIRCULARQUEUE_H

main.cpp


#include"CircularQueue.h"
 
 
int main()
{
 cirQueue<int> cq(20);
 int a = 0;
 for (int i = 0; i < 19; i++)
 {
 cq.push(i);
 }
 cq.print();
 cout << cq;
 for (int i = 0; i < 20; i++)
 {
 cq.pop(a);
 }
 cout << cq;// At this time front=rear=19
 cout << cq.getfront() << "  " << cq.getrear() << endl;
 //for (int i = 19; i < 25; i++)
 //{
 // cq.push(i);
 //}
 cq.push(19);
 cq.print();
 cout << cq.getfront() << "  " << cq.getrear() << endl;
 cout << endl << endl;
 cq.push(20); 
 cq.getTop(a);
 cout << a << endl;
 cq.print();
 cout << cq.getfront() << "  " << cq.getrear() << endl;
 return 1;
}

Related articles: