Like a Stack, 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 rear end and the end from which the old element gets removed is known as front end. The Queue data Stricture can be related with 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 relate to the Queue data structure.
Queue Principle
Queue works on FIFO principle which stands for “First in First Out”, sometimes it also referred to “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 Terminology
Front
The front is the initial block of the queue and the element in the front block known as the front element. In Queue.
Rear
The rear is the last point of the Queue from which new element gets pushed in the queue. The last element of the queue is known as the rear element.
Dequeue
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
Enqueue describes when we add a new element in the Queue from the rare end.
Queue Representation
- A queue is an Ordered Collection of Elements.
- New element adds from the back or rear part of the Queue.
- The Element removes from the front end of the queue.
- The Element inserted at the rear part of the queue make its way to the front part of the view.
- The recently added element in the queue has to wait long as compared to its front elements.
Queue Data Structure Advantages and Disadvantages
Advantage
The Addition and removal of elements take place on the opposite ends which makes it more efficient and less complex as compared to Stack .
Disadvantage
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.
- en_queue()
- de_queue()
- peek()
- is_empty()
en_queue()
en_queue operation describes adding and storing a new element in the queue from the rear end.
de_queue()
de_queue operation describes the removal of elements from the front end of the queue.
Peek()
peek operation is used to tell the current front element of the queue.
is_empty()
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()
Output:
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
People are also reading: