Question
For Python: A last-in-first-out (LIFO) structure, also called a stack, is a list that gets new elements added at the beginning, while elements from the
For Python: A last-in-first-out (LIFO) structure, also called a "stack," is a list that gets new elements added at the beginning, while elements from the front are removed and processed. Write a program that implements a stack. In a loop, ask the user for input. If the user just presses the Enter key, the program ends. If the user enters anything else, except for a single question mark (?), the program considers what the user entered a new element and appends it to the queue. If the user enters a single question mark, the program pops the first element from the queue and displays it. You have to take into account that the user might type a question mark even if the queue is empty.
examples
% python3 stack.py enter stuff (empty to end, '?' to pop first in stack): a adding a to stack ['a'] enter stuff (empty to end, '?' to pop first in stack): b adding b to stack ['b', 'a'] enter stuff (empty to end, '?' to pop first in stack): c adding c to stack ['c', 'b', 'a'] enter stuff (empty to end, '?' to pop first in stack): d adding d to stack ['d', 'c', 'b', 'a'] enter stuff (empty to end, '?' to pop first in stack): 1 adding 1 to stack ['1', 'd', 'c', 'b', 'a'] enter stuff (empty to end, '?' to pop first in stack): ? popping item: 1 from stack ['d', 'c', 'b', 'a'] processing item: 1 enter stuff (empty to end, '?' to pop first in stack): ? popping item: d from stack ['c', 'b', 'a'] processing item: d enter stuff (empty to end, '?' to pop first in stack): ? popping item: c from stack ['b', 'a'] processing item: c enter stuff (empty to end, '?' to pop first in stack): 2adding 2 to stack ['2', 'b', 'a'] enter stuff (empty to end, '?' to pop first in stack): 3 adding 3 to stack ['3', '2', 'b', 'a'] enter stuff (empty to end, '?' to pop first in stack): 4 adding 4 to stack ['4', '3', '2', 'b', 'a'] enter stuff (empty to end, '?' to pop first in stack): ? popping item: 4 from stack ['3', '2', 'b', 'a'] processing item: 4 enter stuff (empty to end, '?' to pop first in stack): ? popping item: 3 from stack ['2', 'b', 'a'] processing item: 3 enter stuff (empty to end, '?' to pop first in stack): ? popping item: 2 from stack ['b', 'a'] processing item: 2 enter stuff (empty to end, '?' to pop first in stack): ? popping item: b from stack ['a'] processing item: b enter stuff (empty to end, '?' to pop first in stack): ? popping item: a from stack [] processing item: a enter stuff (empty to end, '?' to pop first in stack): ? stack is empty processing item: None enter stuff (empty to end, '?' to pop first in stack): ? stack is empty processing item: None enter stuff (empty to end, '?' to pop first in stack):
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started