Last active
November 12, 2021 04:47
-
-
Save Nydhal/ea5c37cabcac923eac45fa49686b4ca6 to your computer and use it in GitHub Desktop.
Queue Class example with explanations in C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
class Queue { // Class definition | |
private: //private members can only be accessed in the class | |
int queue_size; | |
protected: // protected members can ba accessed in derived classes | |
int* buffer; // pointer to first element of array | |
int front; // used for removing an element of array | |
int rear; // user for adding an element into queue | |
static int counter; // counter is a STATIC class member -> must be initialized outside the class (using :: operator) | |
// counter could have been global, but using static allows to : | |
// declare it in the class where it's used (readability) & control it's access (encapsulation) | |
public: // public members can be accessed by all functions | |
Queue(void){ //constructor with no parameters | |
front = 0; | |
rear = 0; | |
queue_size = 10; | |
buffer = new int[queue_size]; // create an array in the heap | |
if (buffer != NULL) counter++; //counting objects | |
} | |
Queue(int n) { // overloaded constructor with one parameter | |
front = 0; | |
rear = 0; | |
queue_size = n; | |
buffer = new int[queue_size]; // same as above, create heap object of type : array of int | |
if (buffer != NULL) counter++; | |
} | |
virtual ~Queue(void) { //destructor | |
delete buffer; // must use "delete [] buffer;" if buffer points to an array of objects, (instead of array of int) | |
counter--; //decrement the # of objects | |
} | |
void enqueue(int v) { // add element at the end of queue | |
if (rear < queue_size) buffer[rear++] = v; | |
else if (compact()) buffer[rear++] = v; | |
} | |
int dequeue(void) { // return and remove 1st element from queue | |
if (front < rear) return buffer[front++]; | |
else { cout << "Error:Queue empty" << endl; return -1; } | |
} | |
private: | |
bool compact(void); // implementation is outside class (allowed in C++ using ::, not allowed in Java) | |
}; | |
//counter initialized to zero, it keeps track of # of Queues created | |
int Queue::counter = 0; | |
//End of class definition | |
class PriQueue: public Queue {//PriQueue derived from Queue | |
public: | |
int getMax(void); // return and remove the max value from priority queue | |
PriQueue(int n) : Queue(n) {}; // <- here PriQueue constructor simply calls Queue constructor | |
// this is because base class constructor may not be inherited. Has to be explicitly called. | |
~PriQueue() { // base class destructor may not be called or inherited. | |
delete buffer; //Must explicitly use delete | |
buffer = NULL; | |
counter--; // decrement counter of objects | |
} | |
}; | |
bool Queue::compact(void) { //compact function implementation, separate from specification () | |
if (front == 0) { | |
cout << "Error: Queue overflow" << endl; | |
return false; | |
} | |
else { | |
for (int i = 0;i < rear - front;i++) | |
{ | |
buffer[i] = buffer[i + front]; | |
} | |
rear = rear - front; | |
front = 0; | |
return true; | |
} | |
} | |
int PriQueue::getMax(void){// get & remove max value from priority queue | |
int i, max, imax; | |
if (front < rear) { | |
max = buffer[front]; | |
imax = front; // imax is index of current max value | |
for (i = front;i < rear;i++) { | |
if (max < buffer[i]) { | |
max = buffer[i]; | |
imax = i; | |
} | |
} | |
for (i = imax;i < rear - 1;i++) | |
buffer[i] = buffer[i++]; // remove max value | |
rear = rear - 1; | |
return max; | |
} | |
else { | |
cout << "Error: Queue empty" << endl; | |
return -1; | |
} | |
} | |
//main outside all classes | |
void main() { | |
Queue Q1(5); // variable/object created using constructor in stack memory (note that buffer will create an array on the heap) | |
Q1.enqueue(2); | |
Q1.enqueue(8); | |
int x = Q1.dequeue(); | |
int y = Q1.dequeue(); | |
cout << "x = " << x << endl << "y = " << y << endl; | |
Queue* Q2; // create pointer (in the stack) to a Queue variable/object | |
Q2 = new Queue(4); // call constructor Queue(int) and dynamically allocate memory from heap | |
Q2->enqueue(12); // insert 12 | |
Q2->enqueue(18); // insert 18 | |
x = Q2->dequeue(); | |
y = Q2->dequeue(); | |
cout << "x = " << x << endl << "y = " << y << endl; | |
PriQueue* Q3 = new PriQueue(4); | |
Q3->enqueue(12); | |
Q3->enqueue(18); | |
Q3->enqueue(14); | |
x = Q3->getMax(); | |
y = Q3->getMax(); | |
cout << "x = " << x << endl << "y = " << y << endl; | |
//Since Q2 & Q3 are in the heap | |
delete Q2; Q2 = NULL; | |
delete Q3; Q3 = NULL; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://ideone.com/jzqHUI
Hey Nydhal , Could you give me an explanation for "Queue is empty"?