Answer to Question #267137 in Databases | SQL | Oracle | MS Access for tushar

Question #267137

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



1
Expert's answer
2021-11-16T23:54:51-0500
# Linked list node

# To store a queue entry

class Node:

    

    def __init__(self, data):

        self.data = data

        self.next = None




# Class to represent a queue




# The queue, front stores the front node

# of linked list and rear stores the last node of linked list

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(15)

    Q.EnQueue(25)

    Q.DeQueue()

    Q.DeQueue()

    Q.EnQueue(35)

    Q.EnQueue(45)

    Q.EnQueue(55)

    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