IntStack Backed by a Doubling Buffer
hardQuestion 717Write a class IntStack that keeps its elements in a raw heap buffer it grows by itself. No vector, no std::stack - you manage the memory.
Rules the tests depend on:
- A brand new IntStack is EMPTY but already owns a buffer of capacity 2, allocated with new int[2].
- void push(int value) - if the stack is full (size equals capacity), first allocate a new buffer of DOUBLE the capacity, copy the elements across, delete[] the old buffer, and keep the new one. Then store the value.
- int pop() - removes and returns the top element. Never called on an empty stack. Popping does not shrink the buffer.
- int size() const - elements stored; int capacity() const - what the current buffer can hold.
- The destructor frees the buffer.
The tests read n, then n values, push them all, then pop them all. For the input 5 then 1 2 3 4 5 the tests print exactly the output shown in Expected Output.
Free the memory you allocate - this question checks for memory leaks.
Do NOT write a main function - the tests provide one.
Expected output
start cap 2 size 5 cap 8 pop 5 4 3 2 1 size 0
= newline. The output ends with a newline, so print one after the last line.
Your output
Run your code to see the output here...