Question: Using Python n this exercise you'll be completing an array-based and a link-based stack collection type discussed in this chapter. Both have similar implementations, but
Using Python
n this exercise you'll be completing an array-based and a link-based stack collection type discussed in this chapter. Both have similar implementations, but a link-based stack uses nodes and an array-based stack can resize the array if necessary.
In the LinkedStack class of the linkedstack.py file, complete the following:
- Complete the implementation of the constructor method.
- __init__, sets the initial state of self, which includes the contents of sourceCollection, if it's present.
- Complete the implementation of the accessor methods.
- __iter__, supports iteration over a view of self and visits items from bottom to top of stack.
- peek(), returns the item at the top of the stack.
- Precondition: the stack is not empty.
- Raises: KeyError if the stack is empty.
- Complete the implementation of the mutator methods.
- clear(), makes self become empty.
- push(), adds item to the top of the stack.
- pop(), removes and returns the item at the top of the stack.
- Precondition: the stack is not empty.
- Raises: KeyError if the stack is empty.
- Postcondition: the top item is removed from the stack.
In the ArrayStack class of the arraystack.py file, complete the following:
- Complete the implementation of the constructor method.
- __init__, sets the initial state of self, which includes the contents of sourceCollection, if it's present.
- Complete the implementation of the accessor methods.
- __iter__, supports iteration over a view of self and visits items from bottom to top of stack.
- peek(), returns the item at the top of the stack.
- Precondition: the stack is not empty.
- Raises: KeyError if the stack is empty.
- Complete the implementation of the mutator methods.
- clear(), makes self become empty.
- push(), adds item to the top of the stack.
- pop(), removes and returns the item at the top of the stack.
- Precondition: the stack is not empty.
- Raises: KeyError if the stack is empty.
- Postcondition: the top item is removed from the stack.
Verify that exceptions are raised when preconditions are violated and that the array-based implementation adds or removes storage as needed.
To test your program run the test() method in the teststack.py file.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
