Here's some input on your current solution:
T *q = new T[num]; // Happens on the heap
What this line does, in detail, is allocate enough memory for »num« elements of T and return the pointer to the first.
So when you call
p = new T[size + 1]
you're NOT overwriting p with an array that is bigger by one. p is just a pointer to the first element of the newly created array. By assigning something new to p, you leave the old values untouched, or in other words: You're creating a completely new array somewhere else on the heap and overwrite your pointer to the old one, leaving its data completely inaccessible.
I will give you some solutions at the end of this post, but you should try to figure it out yourself first. Here's one tip though:
The enque() function should only contain one single for-loop.
Bear in mind that malloc/new and free/delete are »extremely« slow. What happens once you reach the limit of your queue, is that you allocate a new array every single time you add something. That's not how you're supposed to do that.
In C/C++ variables have no default value.
// Your headerSomeclass *ptr; // Is NOT NULL! (or nullptr, if you're using C++11 or higher)int num; // is NOT zero!
Variables are just pieces of memory and what they contain »before« you assigned something to them for the first time or »after« you freed the resource, is completely unpredictable. It's can change every time you run your application.
———————
———————
———————WARNING SOLUTIONS AHEAD
enque(): After creating a copy of p, you have to delete[] the old one before creating a new array with »size + 1« elements.
deque(): Same story, before assigning a new value to p, you need to delete[] the old data.
What you do:
Why not:
Same thing, just one for-loop.
As said earlier, allocating (and freeing) resources on the heap is slow as fuck. The way every standard library (C, C++, C#, Java, you name it) handles it is by doubling the size of the array every time it grows. It's the space-time trade-off. Your solution doesn't waste any memory but is extremely CPU intensive. Nowadays memory is worth far less than CPU cycles in most cases.
That was the first thing. The other is that you don't address the array physically, but logically. What this means is that rather than saying "The next element in the queue is always at arr[0]", you define the beginning and end of your queue yourself. After returning the element at arr[0], the next element is at arr[1]. This of course means that once you have written an element at arr[last], you don't need to increase the size of the array just yet, because at arr[0] a slot is now free.You make your array cyclic.
In case you haven't done that before: You don't need any ifs/elses for that, just the modulo operator. See if you can figure it out by yourself.
You need to keep track of three values:
Once you do need to increase the capacity of your queue, it makes sense to reset everything. Copy your arrays in a way that readHead is arr[0]. Makes it easier to debug.
Hope this helps. If you have any more questions, ask away.