Implement linked list version of Queue data structure. Define all operations that can be performed over a queue.
Question
Implement linked list version of Queue data structure.
Define all operations that can be performed over a queue.
Solution
Sure, here is a Python implementation of a Queue using a linked list:
class Node:
def __init__(self, data):
self.data = data
self.next = None
class Queue:
def __init__(self):
self.front = self.rear = None
def is_empty(self):
return self.front is None
def EnQueue(self, item):
temp = Node(item)
if self.rear is None:
self.front = self.rear = temp
return
self.rear.next = temp
self.rear = temp
def DeQueue(self):
if self.is_empty():
return
temp = self.front
self.front = temp.next
if(self.front == None):
self.rear = None
return str(temp.data)
Here are the operations that can be performed over a queue:
-
EnQueue(item)
: Adds an item to the queue. If the queue is empty, it adds the item and sets both front and rear to the new item. If the queue is not empty, it adds the item at the end and updates the rear. -
DeQueue()
: Removes an item from the queue. The items are popped in the same order in which they are pushed. If the queue is empty, returns None. If the queue has only one item, it removes it and sets both front and rear to None. If the queue has more than one item, it removes the front item and sets front to the next item. -
is_empty()
: Checks if the queue is empty. Returns True if the queue is empty, False otherwise.
Similar Questions
Which of the following data structures can be used to implement a queue?StackLinked listArrayBoth B and C
Which data structure can be used to implement both stack and queue (only one data structure)
How many stacks are needed to implement a queue? Consider the situation where no other data structure like arrays, linked list is available to you.ans.
With the help of well labeled diagrams illustrate and describe the four types of queues thatexist giving common place examples. (8 Marks)
A queue is a:AFIFO (First In First Out) listBLIFO (Last In First Out) listCOrdered arrayDLinear treePrevious
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.