Queue Data Structure and Implementation in Python
Queue Data Structure and Implementation in Python
C/C++
A queue is a useful data structure in programming. It is similar to the ticket queue outside a cinema hall,
where the first person entering the queue is the first person who gets the ticket.
Queue follows the First In First Out (FIFO) rule - the item that goes in first is the item that comes out first.
In the above image, since 1 was kept in the queue before 2, it is the first to be removed from the queue as
well. It follows the FIFO rule.
In programming terms, putting items in the queue is called enqueue, and removing items from the queue is
called dequeue.
We can implement the queue in any programming language like C, C++, Java, Python or C#, but the
specification is pretty much the same.
A queue is an object (an abstract data structure - ADT) that allows the following operations:
Peek: Get the value of the front of the queue without removing it
Working of Queue
Enqueue Operation
Dequeue Operation
for the last element, reset the values of FRONT and REAR to -1
Enqueue and Dequeue Operations
We usually use arrays to implement queues in Java and C/++. In the case of Python, we use lists.
def printQueue(self):
if(self.head == -1):
print("No element in the queue")
else:
for i in range(self.head, self.tail + 1):
print(self.queue[i], end=" ")
print()
obj.dequeue()
print("After removing an element from the queue")
obj.printQueue()
Queue() {
front = -1;
rear = -1;
}
boolean isFull() {
if (rear == SIZE - 1) {
return true;
}
return false;
}