Queue Using Linked List in Python
Implementation of Queue Using Linked List in Python
Discover on how implementation of Queue linked list in Python. Queues are essential for managing data in a first-in, first-out (FIFO) manner. This Python implementation provides a solid foundation for understanding queues and their practical use in your programming projects.
In this page, we will delve into the implementation of a queue using a linked list in python, exploring the concepts, benefits, and potential use cases.
Basic Operations on Linked Lists
There are two basic Operations for implementing queue in the linked list. The operations are Insertion and Deletion.
Syntax of Insertion in Linked List
class Node: def __init__(self, data): self.data = data self.next = None class LinkedList: def __init__(self): self.head = None def insert_at_position(self, data, position): new_node = Node(data) # Insertion logic here # Example usage linked_list = LinkedList() linked_list.insert_at_position(1, 0)
Syntax of Deletion in Linked List
class Node: def __init__(self, data): self.data = data self.next = None class LinkedList: def __init__(self): self.head = None def insert_at_end(self, data): new_node = Node(data) if not self.head: self.head = new_node return current = self.head while current.next: current = current.next current.next = new_node def delete_by_value(self, value): if not self.head: return if self.head.data == value: self.head = self.head.next return current = self.head prev = None while current: if current.data == value: prev.next = current.next return prev = current current = current.next
You can perform deletion using the following methods:
- Delete Value
linked_list = LinkedList() linked_list.insert_at_end(1) linked_list.insert_at_end(2) linked_list.insert_at_end(3) value_to_delete = 2 linked_list.delete_by_value(value_to_delete)
Implementation of Queue Using Linked List
Implementing a queue using a linked list allows to grow queue according to the requirements.
A queue implemented using a linked list not changes its behavior and will continue to work according to FIFO.
Code for implementing queue in linked list
class Node: def __init__(self, data): self.data = data self.next = None class Queue: def __init__(self): self.front = None self.rear = None def is_empty(self): return self.front is None def enqueue(self, data): new_node = Node(data) if self.rear is None: self.front = self.rear = new_node return self.rear.next = new_node self.rear = new_node def dequeue(self): if self.is_empty(): return None temp = self.front self.front = temp.next if self.front is None: self.rear = None return temp.data def peek(self): if self.is_empty(): return None return self.front.data def display(self): current = self.front while current: print(current.data, end=" ") current = current.next print() queue = Queue() queue.enqueue(10) queue.enqueue(20) queue.enqueue(30) print("Queue elements:") queue.display() print("Dequeue:", queue.dequeue()) print("Dequeue:", queue.dequeue()) print("Front of the queue:", queue.peek()) print("Is the queue empty?", queue.is_empty())
Explanation
In this implementation, we have a ‘Node’ class to create individual nodes for the linked list and a ‘Queue’ class to implement the queue using a linked list. The ‘Queue’ class has methods for enqueuing, dequeuing, peeking at the front element, checking if the queue is empty, and displaying its contents. The example usage section demonstrates how to use these methods to manipulate the queue.
Use Cases and Applications
Linked lists find application in various domains, such as:
- Memory Management: Allocating and deallocating memory blocks efficiently.
- Graphs: Representing graphs and networks with dynamic connectivity.
- Polynomials: Storing and performing operations on polynomial expressions.
Conclusion
In this comprehensive guide, we’ve explored the world of linked lists, from their fundamental structure to advanced operations and real-world applications. Armed with this knowledge, you’re now better equipped to make informed decisions when implementing linked lists in your programming endeavors. Remember, linked lists are just one of the many tools in your data structure arsenal, each serving a unique purpose in the grand landscape of computer science.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment