CJCoding With Joseph
15free / daySign inCreate account

IntStack Backed by a Doubling Buffer

Write 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 — print one after the last line.

Hints4 available
Topics:
Pointers
📥 Auto-Input:
5\n1 2 3 4 5\n
This input is automatically fed to your program's stdin
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (cout, fori)

Your Output

Run your code to see the output here...

Test Cases

Run your code to see test case results.