Question: Assume the following class implements the STACK abstract data type (ADT) using the array ADT. class aStack(iArray): def __init__(self, capacity = 5): self._items = iArray(capacity)
Assume the following class implements the STACK abstract data type (ADT) using the array ADT.
class aStack(iArray):
def __init__(self, capacity = 5):
self._items = iArray(capacity)
self._top = -1
self._size = 0
def push(self, newItem):
self._top += 1
self._size += 1
self._items[self._top] = newItem
def pop(self):
oldItem = self._items[self._top]
self._items[self._top] = None
self._top -= 1
self._size -= 1
return oldItem
def peek(self):
return self._items[self._top]
def __len__(self):
return self._size
def __str__(self):
result = ' '
for i in range(len(self)):
result += str(self._items[i]) + ' '
return result
A- Emulate the stack behavior using the Python list data structure rather than the Arrary ADT.
B- Redefine the Stack class methods to push and pop two items rather than one item at a time.
For example, if the stack includes numbers from one to ten: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], then invoking the pop() method twice will remove the last four elements and modify the stack elements to be: [1, 2, 3, 4, 5, 6].
C- Define and implement a function that reverses the items of a given stack using only the methods defined in the Stack class.
For example, if the stack includes the elements from one to ten: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], then calling the new function will modify the stack elements to be from ten to one: [10, 9, 8, 7, 6, 5, 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
