Using a Linked List as the underlying data storage mechanism, implement both a Stack and a Queue.
- Create a
Nodeclass withvalueandnextproperties. - Create a
Stackclass with atopproperty, and write class instance methods forpush,pop,peek, andisEmpty. - Create a
Queueclass withfrontandbackproperties, and write class instance methods forenqueue,dequeue,peek, andisEmpty. - Create tests for all methods and pass all tests.
push and enqueue
- Time and Space Complexity: O(1).
pop and dequeue
- Time and Space Complexity: O(1).
peek
- Time and Space Complexity: O(1).
isEmpty
- Time and Space Complexity: O(1).
push and enqueue
- Argument: a value.
- Returns: nothing.
- Side effects: adds a node with a value to the top/back of the stack/queue, respectively.
pop and dequeue
- Argument: nothing.
- Returns: the value at the top/front of the stack/queue, respectively.
- Side effects: Removes the top/front node from the stack/queue, respectively.
peek
- Argument: nothing.
- Returns: the value at the top/front of the stack/queue, respectively.
- Side effects: none.
isEmpty
- Argument: nothing.
- Returns: a boolean, true if the stack/queue is empty, or false otherwise.
- Side effects: none.