← Back to Pointers
Question 471
Growable Integer Stack with realloc
Growable Integer Stack with realloc
Write a stack of ints backed by ONE dynamically allocated array that doubles withreallocwhen it runs out of room. The stack starts with capacity 2 and size 0. Read an integern, thennvalues, and push each one. Before a push, if the array is already full (size equals capacity), double the capacity withreallocand print a line: grow OLDCAPACITY -> NEWCAPACITY Then read an integerkand popkvalues off the top of the stack, printing each one as: pop VALUEkis 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
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 (p, fori)
Your Output
Run your code to see the output here...
Test Cases
Run your code to see test case results.