A Queue is a lot like the Stack data structure, but the order in which you pull elements out is different. In a Queue you enqueue to put something in the Queue and you dequeue to pull something out. The first thing in to the data structure is the first thing that will be dequeued. In other words, this is a first-in, first-out data structure, aka FIFO.
To recap:
- Stacks — The last element added to the list is the first to be removed with
pop, so a stack if a LIFO data structure - Queues - The first element added to the list is the first to be removed with
dequeue, so a Queue is a FIFO data structure.
Queues are the natural complement to Stacks. Some algorithms work with Queues or Stacks to help maintain state. The same algorithm using a Queue vs a Stack can produce wildly different results.
Write and test a Queue class that conforms to the following interface:
Queue#new(): Instantiate a newQueueQueue#enqueue(element): Add a new element to the queueQueue#dequeue: Remove and return the first element in the queueQueue#peel: Return (but do not remove) the first element in the queueQueue#empty?: Answer whether or not the queue is empty
Do not use any Ruby data structures in your implementation. Instead, pick one of your own to use in your implementation:
- FixedArray
- ArrayList
- LinkedList