Write three classes, Stack, Queue and Node. The Stack class will have a top property, Queue will have a front property(1). The following methods can be called on the Stack and Queue classes.
Stack should have these four different methods:
.push(value)(adds a node withvalueto thetopof the stack)
Input: avalueobject to add to the stack’stop
Output: none
.pop()(remove the node at thetopof the stack and return itsvalue)
Input: none
Output: thevalueof the node removed from thetopof the stack
Edge Case(s):
- Attempting to
pop()on an empty stack throws an exception
.peek()(returns thevalueof the node at thetopof the stack)
Input: none
Output: thevalueof the node at thetopof the stack
Edge case(s):
- Attempting to
peek()on an empty stack throws an exception
.isEmpty()(returns a boolean representing whether the stack is empty)
Input: none
Output: a boolean representing whether the stack is empty
Queue should have these four different methods:
.enqueue(value)(adds a node withvalueto theendof the queue)
Input: avalueobject to add to the queue’send
Output: none
.dequeue()(remove the node at thefrontof the queue and return itsvalue)
Input: none
Output: thevalueof the node removed from thefrontof the queue
Edge Case(s):
- Attempting to
dequeue()on an empty queue throws an exception
.peek()(returns thevalueof the node at thefrontof the queue)
Input: none
Output: thevalueof the node at thefrontof the queue
Edge case(s):
- Attempting to
peek()on an empty queue throws an exception
.isEmpty()(returns a boolean representing whether the queue is empty)
Input: none
Output: a boolean representing whether the queue is empty
PseudoQueue should have these three different methods:
.enqueue(value)(adds a node withvalueto theendof the pseudoqueue)
Input: avalueobject to add to the pseudoqueue’send
Output: none
.dequeue()(remove the node at thetopof the pseudoqueue and return itsvalue)
Input: none
Output: thevalueof the node removed from thetopof the pseudoqueue
Edge Case(s):
- Attempting to
dequeue()on an empty pseudoqueue throws an exception
.peek()(returns thevalueof the node at thetopof the pseudoqueue)
Input: none
Output: thevalueof the node at thetopof the pseudoqueue
Edge case(s):
- Attempting to
peek()on an empty pseudoqueue throws an exception
(1) I found that it was necessary to also add an end property to the Queue and PseudoQueue class to maintain O(1) time performance.
Write tests to prove the following functionality:
