← Back to Data Structures & Algorithms
Question 556
Stack On A List
Stack On A List
Complete the Stack class built on a list, where the end of the list is the top: push(item) adds to the top with append(), pop() removes and returns the top item with the list's own pop(), peek() returns the top item without removing it (index -1), is_empty() returns whether the stack has no items, and size() returns how many it holds. Every operation is O(1). Do NOT write a main block or any top-level test code; the tests construct the class. Input: one line of space-separated integers; the tests push them all in order, print the size and the top item, pop every item printing each, and finally print whether the stack is empty.
Expected Output:
3 3 3 2 1 True
= newline. The output ends with a newline — print one after the last line.
Hints4 available
Sign in to use hintsTopics:
Data Structures & Algorithms
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Ctrl+Space to expand shortcuts (pr, inp, def)
Your Output
Run your code to see the output here...
Test Cases
Run your code to see test case results.