We've just finished learning about queues in my data structures class; I want to make sure I really understand it. I'm still feeling a bit lost on how to optimize the data use of a queue. I've created a little program here:
#include <iostream>
class Queue{
public:
Queue(int size = 10){
ptr = new int[size];
head = counter = 0;
qSize = size;
}
void push(int value){
if(counter == qSize)
doubleSize();
ptr[counter++] = value;
}
int pop(){
if(counter == 0){
std::cout << "The queue is empty!!! \n";
return -1;
}
if(head >= counter - 1){
int temp = ptr[head];
std::cout << "This is the last item in the queue. \n";
delete[] ptr;
ptr = new int[qSize];
head = counter = 0;
return temp;
}
return ptr[head++];
}
private:
int *ptr;
int head;
int counter;
int qSize;
void doubleSize(){
int prevSize = qSize;
qSize *= 2;
int *temp = new int[qSize];
for(int i = 0; i < prevSize; i++){
temp[i] = ptr[i];
}
delete[] ptr;
ptr = temp;
}
};
So, as you can test, the program runs fine; but what I'm looking for is how I can optimize my queues to be as efficient as possible. I.E. Is there a specific way that I should be programming these queues that uses the minimum amount of data, and has the smallest Order complexity?
P.S. I am aware that there is a queue library. I am more seeking to optimize how I program out queues themselves.