Queue in Data Structure YASH PAL, 27 May 202017 August 2025 A queue is an abstract data structure in which the insertion is performed on one end and deletion is performed on the other end.Example of Queue – In the above-given image, you see that the customers are standing in a line. and the customer who entered the line will go first means First in First out. and we can add the new customers in line at the end of the line and will go out from the beginning of the line.So the queue follows the FIFO ( First in First out ) process. and we can insert a new value from one end and delete the first inserted value from the other end.Enqueue operationTo insert a new value in the queue is called the Enqueue operation in the queueDequeue operationTo delete the first entered value in the queue is called the Dequeue operation in the queue.Front end The end from which we delete the value in the queue is called the Front end of the queue.Rear-endThe end from which we insert a new value in the queue is called the rear end of the queue.If we want to enter a new value then we use the rear end and if we want to delete the value then we use the front end.Implementation of QueueWe can implement a queue using two ways or using two data types. first is using the array and second is using linked list. so first we learn about the how to implement queue using array and then we will learn about implementation of queue using linked list.Implementation of the queue using the arrayTo implement a queue using an array first we need to choose the position of the front end and rear end. we use the first index of the array as the front end and the last index as the rear end.In the above example first, we take an empty array and then we enqueue the values 20, 30, 15, and 40 after that, we dequeue the values 20, and 30 from the front end, and then we again enqueue the value 56, and then dequeue the value 15.Program to implement queue using array in Pythonclass EmptyQueueError(Exception): pass class Queue: def __init__(self): self.items = [] def is_empty(self): return self.items == [] def size(self): return len(self.items) def enqueue(self,item): self.items.append(item) def dequeue(self): if self.is_empty(): raise EmptyQueueError("Queue is Empty") return self.items.pop(0) def peek(self): if self.is_empty(): raise EmptyQueueError("Queue is Empty") return self.items[0] def display(self): print(self.items) ############## if __name__ == '__main__': qu = Queue() while True: print("1. Enqueue") print("2. Dequeue") print("3. Peek") print("4. Size") print("5. Display") print("6. Quit") choice = int(input("Enter your choice : ")) if choice == 1: x = int(input("Enter the element : ")) qu.enqueue(x) elif choice == 2: x=qu.dequeue() print("Element deleted fro the queue is : ", x) elif choice == 3: print("Element at the front end is ", qu.peek()) elif choice == 4: print("Size of queue ", qu.size()) elif choice == 5: qu.display() elif choice == 6: break else: print("Wrong choice ") print()class EmptyQueueError(Exception): pass class Queue: def __init__(self): self.items = [] def is_empty(self): return self.items == [] def size(self): return len(self.items) def enqueue(self,item): self.items.append(item) def dequeue(self): if self.is_empty(): raise EmptyQueueError("Queue is Empty") return self.items.pop(0) def peek(self): if self.is_empty(): raise EmptyQueueError("Queue is Empty") return self.items[0] def display(self): print(self.items) ############## if __name__ == '__main__': qu = Queue() while True: print("1. Enqueue") print("2. Dequeue") print("3. Peek") print("4. Size") print("5. Display") print("6. Quit") choice = int(input("Enter your choice : ")) if choice == 1: x = int(input("Enter the element : ")) qu.enqueue(x) elif choice == 2: x=qu.dequeue() print("Element deleted fro the queue is : ", x) elif choice == 3: print("Element at the front end is ", qu.peek()) elif choice == 4: print("Size of queue ", qu.size()) elif choice == 5: qu.display() elif choice == 6: break else: print("Wrong choice ") print()Implementation of the queue using the linked listTo implement a queue using the linked list we first store the first node and last node’s reference into the front and rear variables. because using the front variable we perform deletion in a queue and using the rear variable we perform insertion in a queue.so the beginning of the list is called the front end of the queue and the end of the list is called the rear end of the queue.Dequeue operationTo remove a node from the queue is known as Dequeue. we already know how to delete the first node of the linked list and delete the node in the only linked list. and if you don’t know then please go through this tutorial – Deletion in the linked listEnqueue operationTo add or insert a new node in the queue is known as Enqueue operation. We already know how to insert a new node at the end of the linked list. and if you don’t know then please go through this tutorial – Insertion in the linked listProgram to implement Queue using linked list in Pythonclass EmptyQueueError(Exception): pass class Node: def __init__(self, value): self.info = value self.link = None class Queue: def __init__(self): self.rear = None def is_empty(self): return self.rear is None def size(self): if self.is_empty(): return 0 n = 0 p = self.rear.link while True: n += 1 p = p.link if p == self.rear.link: break return n def enqueue(self, data): temp = Node(data) if self.is_empty(): self.rear = temp self.rear.link = self.rear else: temp.link = self.rear.link self.rear.link = temp self.rear = temp def dequeue(self): if self.is_empty(): raise EmptyQueueError("Queue is Empty ") if self.rear.link == self.rear: temp = self.rear self.rear = None else: temp = self.rear.link self.rear.link = self.rear.link.link return temp.info def peek(self): if self.is_empty(): raise EmptyQueueError("Queue is Empty") return self.rear.link.info def display(self): if self.is_empty(): print("List is empty") return p = self.rear.link while True: print(p.info, " ", end='') p = p.link if p == self.rear.link: break print() ##################### if __name__ == '__main__': qu = Queue() while True: print("1. Enqueue") print("2. Dequeue") print("3. Peek") print("4. Size") print("5. Display") print("6. Quit") choice = int(input("Enter your choice : ")) if choice == 1: x = int(input("Enter the element : ")) qu.enqueue(x) elif choice == 2: x = qu.dequeue() print("Element deleted from the queue is : ", x) elif choice == 3: print("Element at the front end is ", qu.peek()) elif choice == 4: print("Size of queue ", qu.size()) elif choice == 5: qu.display() elif choice == 6: break else: print("Wrong choice") print()class EmptyQueueError(Exception): pass class Node: def __init__(self, value): self.info = value self.link = None class Queue: def __init__(self): self.rear = None def is_empty(self): return self.rear is None def size(self): if self.is_empty(): return 0 n = 0 p = self.rear.link while True: n += 1 p = p.link if p == self.rear.link: break return n def enqueue(self, data): temp = Node(data) if self.is_empty(): self.rear = temp self.rear.link = self.rear else: temp.link = self.rear.link self.rear.link = temp self.rear = temp def dequeue(self): if self.is_empty(): raise EmptyQueueError("Queue is Empty ") if self.rear.link == self.rear: temp = self.rear self.rear = None else: temp = self.rear.link self.rear.link = self.rear.link.link return temp.info def peek(self): if self.is_empty(): raise EmptyQueueError("Queue is Empty") return self.rear.link.info def display(self): if self.is_empty(): print("List is empty") return p = self.rear.link while True: print(p.info, " ", end='') p = p.link if p == self.rear.link: break print() ##################### if __name__ == '__main__': qu = Queue() while True: print("1. Enqueue") print("2. Dequeue") print("3. Peek") print("4. Size") print("5. Display") print("6. Quit") choice = int(input("Enter your choice : ")) if choice == 1: x = int(input("Enter the element : ")) qu.enqueue(x) elif choice == 2: x = qu.dequeue() print("Element deleted from the queue is : ", x) elif choice == 3: print("Element at the front end is ", qu.peek()) elif choice == 4: print("Size of queue ", qu.size()) elif choice == 5: qu.display() elif choice == 6: break else: print("Wrong choice") print() Computer Science Tutorials Data Structures Tutorials computer scienceData Structure