unsigned mySize; // number of items I contain
unsigned myCapacity; // how many items I can store
unsigned myFirst; // index of oldest item (if any)
unsigned myLast; // index of next available spot for append (if any)
Item* myArray; // dynamic array of items
I am creating a dynamic array-based queue class and need to make a method that changes the number of items that a queue can hold. I need "myLast" to remain accurate after the capacity has changed, especially on a queue that has already had items dequeued from the front.
void ArrayQueue<Item>::setCapacity(unsigned cap) {
if (cap < getSize() || cap == 0){
throw QueueException("setCapacity()", "New capacity must be greater than size");
} else {
Item * nq = new Item[cap];
for (unsigned i = 0; i < cap; i++){
nq[i] = myArray[i];
}
delete [] myArray;
myArray = nq;
myCapacity = cap;
//what do I put here to make myFirst and myLast be correct for the new capcacity?
}
}
Can anyone explain how to do this?