Queue
● Queue is a linear data structure that follows FIFO (First In First Out) Principle, so the
first element inserted is the first to be popped out.
● FIFO Principle states that the first element added to the Queue will be the first one to be
removed or processed. So, Queue is like a line of people waiting to purchase tickets,
where the first person in line is the first person served.
Basic Terminologies of Queue
● Front: Position of the entry in a queue ready to be served, that is, the first entry that will be
removed from the queue, is called the front of the queue. It is also referred as the head of the
queue.
● Rear: Position of the last entry in the queue, that is, the one most recently added, is called
the rear of the queue. It is also referred as the tail of the queue.
● Size: Size refers to the current number of elements in the queue.
● Capacity: Capacity refers to the maximum number of elements the queue can hold.
, Representation of Queue
Operations on Queue
1. Enqueue:
Enqueue operation adds (or stores) an element to the end of the queue.
Steps:
1. Check if the queue is full. If so, return an overflow error and exit.
2. If the queue is not full, increment the rear pointer to the next available position.
3. Insert the element at the rear.
2. Dequeue:
Dequeue operation removes the element at the front of the queue. The following steps are taken
to perform the dequeue operation:
1. Check if the queue is empty. If so, return an underflow error.
2. Remove the element at the front.
3. Increment the front pointer to the next element.
3. Peek or Front Operation:
This operation returns the element at the front end without removing it.
● Queue is a linear data structure that follows FIFO (First In First Out) Principle, so the
first element inserted is the first to be popped out.
● FIFO Principle states that the first element added to the Queue will be the first one to be
removed or processed. So, Queue is like a line of people waiting to purchase tickets,
where the first person in line is the first person served.
Basic Terminologies of Queue
● Front: Position of the entry in a queue ready to be served, that is, the first entry that will be
removed from the queue, is called the front of the queue. It is also referred as the head of the
queue.
● Rear: Position of the last entry in the queue, that is, the one most recently added, is called
the rear of the queue. It is also referred as the tail of the queue.
● Size: Size refers to the current number of elements in the queue.
● Capacity: Capacity refers to the maximum number of elements the queue can hold.
, Representation of Queue
Operations on Queue
1. Enqueue:
Enqueue operation adds (or stores) an element to the end of the queue.
Steps:
1. Check if the queue is full. If so, return an overflow error and exit.
2. If the queue is not full, increment the rear pointer to the next available position.
3. Insert the element at the rear.
2. Dequeue:
Dequeue operation removes the element at the front of the queue. The following steps are taken
to perform the dequeue operation:
1. Check if the queue is empty. If so, return an underflow error.
2. Remove the element at the front.
3. Increment the front pointer to the next element.
3. Peek or Front Operation:
This operation returns the element at the front end without removing it.