← Back to Pointers
Question 717
IntStack Backed by a Doubling Buffer
IntStack Backed by a Doubling Buffer
Write a classIntStackthat keeps its elements in a raw heap buffer it grows by itself. Novector, nostd::stack- you manage the memory. Rules the tests depend on: - A brand newIntStackis EMPTY but already owns a buffer of capacity 2, allocated withnew 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 readn, thennvalues, push them all, then pop them all. For the input5then1 2 3 4 5the 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
Sign in to use hintsTopics:
Pointers
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.