Question: Programming Language Python: A Stack is an abstract data type for storing a collection of items, where items are both added and removed from one
Programming Language Python:
A "Stack" is an abstract data type for storing a collection of items, where items are both added and removed from one end of the collection. The methods for adding and removing items from a stack are traditionally called "push()" and "pop()" respectively. A method called "peek()" returns the value on the top of the stack, but does not remove it from the stack. A "size()" method returns the number of items on the stack, and a method "is_empty()" returns a boolean indicating if the stack is empty.
For this exercise, you will provide a complete implementation of the Stack ADT. You can implement the Stack ADT in any way that you like, but using a Python list to store the items is certainly the simplest way.
The method names are given below as a guide to help you:
class Stack: def __init__(self): def is_empty(self): def push(self, item): def pop(self): def peek(self): def size(self):
For example:
| Test | Result |
|---|---|
s = Stack() print(s.is_empty()) | True |
s = Stack() s.push(1) s.push(2) s.push(3) s.push(4) while not s.is_empty(): print(s.pop()) | 4 3 2 1 |
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
