STACK abstract data type :Class Implementation Assignment
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
Question 3.a Emulate the stack behaviour using the Python list data structure rather than the Array ADT, then list the pros and cons of this approach.
Question 3.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]