Growable Integer Stack with realloc
hardQuestion 471Write 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, so print one after the last line.
Your output
Run your code to see the output here...