What are the contents of the stack after running the following algorithm? Given
ID: 3669430 • Letter: W
Question
What are the contents of the stack after running the following algorithm? Given an array and an empty stack: Push the values of the array onto the stack until a - 1 value is found. Do not push the -1. Pop the values in the stack until an even value is found or the stack is empty. The even value shouldn't be popped. Repeat steps 1 and 2 until the stack is full or the array is read completely. Type the contents of the stack from top to bottom, separated by a space, in the Answer box. If the stack is empty, enter a 0. For this problem, use an array = {10, 1, 3, 2, 3, -1, 4, 5, 7}, and a stack size of 6.Explanation / Answer
Assumption: If the stack is full while reading from the array, then the program quits
a = {10,1,3,2,3,-1,4,5,7}
1st Iteration:
1. stack = 10,1,3,2,3
2. stack = 10,1,3,2
2nd Iteration:
1. stack = 10,1,3,2,4,5 now the stack is full
Hence answer = 5 4 2 3 1 10
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.