Like a stack, a queue is also an ordered collection of elements or items. In a queue, the addition of new elements and removal of old ones occur on opposite ends. The end at which the new element gets inserted is known as the rear end, and the end from which the old element gets removed is known as the front end.
This data structure can be related to the queue of humans standing for a movie ticket; if a new person wants to buy a movie ticket, he has to stand at the last or the rear point of the queue, and the person who is at the front of the queue would get the movie ticket first and move. In the real world, there are many examples related to the queue data structure.
Queue works on the FIFO principle, which stands for “First in First Out” sometimes; it also refers to the “First Come First Served” principle. According to the FIFO principle, the element which comes first would be served first, and the element inserted last would be treated last.
Basic Queue Data Structure Terminologies
The front is the initial block of the queue, and the element in the front block is known as the front element.
The rear is the last point of the queue from which a new element gets pushed in the queue. The last element of the queue is known as the rear element.
Dequeue is an operation that helps to remove the first element of the queue, and it operates on the front element of the queue.
Enqueue describes when we add a new element in the queue from the rare end.
- A queue is an ordered collection of elements.
- New elements are added from the back or rear part of the queue.
- The elements are removed from the front end of the queue.
- The element inserted at the rear part of the queue makes its way to the front part of the view.
- The recently added element in the queue has to wait longer compared to its front elements.
Queue Data Structure Advantages and Disadvantages
The addition and removal of elements take place on the opposite ends, which makes it more efficient and less complex as compared to stack .
The algorithm implementation of removing and adding elements in a queue could be complex.
Queue Basic Operations
There are some basic operations that are related to the queue data structure, as stated below:
en_queue operation describes adding and storing a new element in the queue from the rear end.
de_queue operation describes the removal of elements from the front end of the queue.
peek operation is used to tell the current front element of the queue.
is_empty operation tells whether the queue has any element or not.
Queue Implementation Using Python Programming Language
class Queue(): def __init__(self): self.items =  def is_empty(self): if self.items == : print("Queue is Empty") else: print("There are some elemetns in the Queue") def en_queue(self,data): self.items.insert(0,data) print(data,"has been added to the rear part of the Queue") def de_queue(self): self.items.pop() print("Front element of the Queue has been deleted") def size(self): print("The size of the Queue is:",len(self.items)) def peek(self): print("The current Front element is:", self.items[len(self.items)-1]) def show_queue(self): print("rear-->|",end="") for i in self.items: print(str(i)+"|",end="") print("<--Front") q = Queue() q.is_empty() q.en_queue(4) q.en_queue(10) q.de_queue() q.en_queue(12) q.en_queue(13) q.en_queue(14) q.en_queue(15) q.peek() q.show_queue()
Queue is Empty 4 has been added to the rear part of the Queue 10 has been added to the rear part of the Queue Front element of the Queue has been deleted 12 has been added to the rear part of the Queue 13 has been added to the rear part of the Queue 14 has been added to the rear part of the Queue 15 has been added to the rear part of the Queue The current Front element is: 10 rear-->|15|14|13|12|10|<--Front
In conclusion, the queue follows the First-In, First-Out (FIFO) principle. It manages data, ensuring that the first element to arrive is the first to be served, which you can easily co-relate to real-world scenarios like waiting lines in everyday life. In this tutorial, we provided the basic information regarding queues and its implementation.
We hope that the tutorial proves helpful to you!
People are also reading: