- Create a Stack class that has top and storage properties. It creates an empty Stack when instantiated.
- This object should be aware of a default empty value assigned to top when the stack is created.
- Adds a new item with that value to the top of the stack with an O(1) Time performance.
- Returns the value from the top item of the stack.
- Removes the item from the top of the stack.
- Should raise an exception when called on an empty tack.
- Returns Value of the item located at the top of the stack.
- Should raise exception when called on empty stack.
- Returns a boolean indicating whether or not the stack is empty.
- Create a Queue class that has a storage property. It creates an empty Queue when instantiated.
- Adds a new node with that value to the back of the queue with an O(1) Time performance.
- Returns the value from node from the front of the queue
- Removes the node from the front of the queue
- Should raise exception when called on empty queue
- Returns the value of the item located at the front of the queue
- Should raise exception when called on empty stack
- Returns a boolean indicating whether or not the queue is empty
Method | Time Complexity | Space Complexity |
---|---|---|
push | O(1) | O(1) |
pop | O(1) | O(1) |
peek | O(1) | O(1) |
isEmpty | O(1) | O(1) |
Method | Time Complexity | Space Complexity |
---|---|---|
enqueue | O(1) | O(1) |
dequeue | O(n) | O(1) |
peek | O(1) | O(1) |
isEmpty | O(1) | O(1) |
# run all related tests
npm test stack.test.js queue.test.js