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 withvalue
to thetop
of the stack)
Input: avalue
object to add to the stack’stop
Output: none
.pop()
(remove the node at thetop
of the stack and return itsvalue
)
Input: none
Output: thevalue
of the node removed from thetop
of the stack
Edge Case(s):
- Attempting to
pop()
on an empty stack throws an exception
.peek()
(returns thevalue
of the node at thetop
of the stack)
Input: none
Output: thevalue
of the node at thetop
of 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 withvalue
to theend
of the queue)
Input: avalue
object to add to the queue’send
Output: none
.dequeue()
(remove the node at thefront
of the queue and return itsvalue
)
Input: none
Output: thevalue
of the node removed from thefront
of the queue
Edge Case(s):
- Attempting to
dequeue()
on an empty queue throws an exception
.peek()
(returns thevalue
of the node at thefront
of the queue)
Input: none
Output: thevalue
of the node at thefront
of 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 withvalue
to theend
of the pseudoqueue)
Input: avalue
object to add to the pseudoqueue’send
Output: none
.dequeue()
(remove the node at thetop
of the pseudoqueue and return itsvalue
)
Input: none
Output: thevalue
of the node removed from thetop
of the pseudoqueue
Edge Case(s):
- Attempting to
dequeue()
on an empty pseudoqueue throws an exception
.peek()
(returns thevalue
of the node at thetop
of the pseudoqueue)
Input: none
Output: thevalue
of the node at thetop
of 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: