Answer to Question #255273 in Databases | SQL | Oracle | MS Access for Tarurendra Kushwah

Question #255273

Write a program to create a queue that permits insertion at any vacant location at the rear end.


1
Expert's answer
2021-10-22T16:47:00-0400
# A linked list (LL) node
# to store a queue entry
class Node:
    
    def __init__(self, data):
        self.data = data
        self.next = None

# A class to represent a queue

# The queue, front stores the front node
# of LL and rear stores the last node of LL
class Queue:
    
    def __init__(self):
        self.front = self.rear = None

    def isEmpty(self):
        return self.front == None
    
    # Method to add an item to the queue
    def EnQueue(self, item):
        temp = Node(item)
        
        if self.rear == None:
            self.front = self.rear = temp
            return
        self.rear.next = temp
        self.rear = temp

    # Method to remove an item from queue
    def DeQueue(self):
        
        if self.isEmpty():
            return
        temp = self.front
        self.front = temp.next

        if(self.front == None):
            self.rear = None

# Driver Code
if __name__== '__main__':
    q = Queue()
    q.EnQueue(10)
    q.EnQueue(20)
    q.DeQueue()
    q.DeQueue()
    q.EnQueue(30)
    q.EnQueue(40)
    q.EnQueue(50)
    q.DeQueue()
    print("Queue Front " + str(q.front.data))
    print("Queue Rear " + str(q.rear.data))
    

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS