Queue & Stack
In this chapter, we will first introduce First-in-first-out
(FIFO) and how it works in a queue
.
The goal of this chapter is to help you:
Understand the
definition
of FIFO and queue;Be able to
implement
a queue by yourself;Be familiar with the
built-in queue structure
;Use queue to solve simple problems.
First-in-first-out Data Structure
In a FIFO data structure, the first element added to the queue will be processed first
.
As shown in the picture above, the queue is a typical FIFO data structure. The insert operation is also called enqueue and the new element is always added at the end of the queue
. The delete operation is called dequeue. You are only allowed to remove the first element.
Queue - Implementation
To implement a queue, we may use a dynamic array and an index pointing to the head of the queue.
As mentioned, a queue should support two operations: enqueue
and dequeue
. Enqueue appends a new element to the queue while dequeue removes the first element. So we need an index to indicate the starting point.
Here is an implementation for your reference:
Drawback
The implementation above is straightforward but is inefficient in some cases. With the movement of the start pointer, more and more space is wasted. And it will be unacceptable when we only have a space limitation.
Let's consider a situation when we are only able to allocate an array whose maximum length is 5. Our solution works well when we have only added less than 5 elements. For example, if we only called the enqueue function four times and we want to enqueue an element 10, we will succeed.
And it is reasonable that we can not accept more enqueue request because the queue is full now. But what if we dequeue an element?
Actually, we should be able to accept one more element in this case.
Circular Queue
Previously, we have provided a straightforward but inefficient implementation of queue.
A more efficient way is to use a circular queue. Specifically, we may use a fixed-size array
and two pointers
to indicate the starting position and the ending position. And the goal is to reuse the wasted storage
we mentioned previously.
Let's take a look at an example to see how a circular queue works. You should pay attention to the strategy we use to enqueue
or dequeue
an element.
Design
Design your implementation of the circular queue. The circular queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle and the last position is connected back to the first position to make a circle. It is also called "Ring Buffer".
One of the benefits of the circular queue is that we can make use of the spaces in front of the queue. In a normal queue, once the queue becomes full, we cannot insert the next element even if there is a space in front of the queue. But using the circular queue, we can use the space to store new values.
Your implementation should support following operations:
MyCircularQueue(k)
: Constructor, set the size of the queue to be k.Front
: Get the front item from the queue. If the queue is empty, return -1.Rear
: Get the last item from the queue. If the queue is empty, return -1.enQueue(value)
: Insert an element into the circular queue. Return true if the operation is successful.deQueue()
: Delete an element from the circular queue. Return true if the operation is successful.isEmpty()
: Checks whether the circular queue is empty or not.isFull()
: Checks whether the circular queue is full or not.
Example:
Note:
All values will be in the range of [0, 1000].
The number of operations will be in the range of [1, 1000].
Please do not use the built-in Queue library.
Last updated