CJCoding With Joseph
15free / daySign inCreate account

Growable Integer Stack with realloc

Write a stack of ints backed by ONE dynamically allocated array that doubles with realloc when it runs out of room. The stack starts with capacity 2 and size 0.

Read an integer n, then n values, and push each one. Before a push, if the array is already full (size equals capacity), double the capacity with realloc and print a line:

grow OLDCAPACITY -> NEWCAPACITY

Then read an integer k and pop k values off the top of the stack, printing each one as:

pop VALUE

k is never larger than the number of values pushed. Last, print how many values are still on the stack:

left COUNT

The array never shrinks — popping only moves the size down. For the input:

5
10 20 30 40 50
2

the program prints exactly the output shown in Expected Output.

Free the memory you allocate before the program ends — this question checks for memory leaks.

Expected Output:

grow 2 -> 4
grow 4 -> 8
pop 50
pop 40
left 3

= newline. The output ends with a newline — print one after the last line.

Hints4 available
Topics:
Pointers
📥 Auto-Input:
5\n10 20 30 40 50\n2\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 (p, fori)

Your Output

Run your code to see the output here...

Test Cases

Run your code to see test case results.