Queue Linked List in DSA Python

Program 1

# Queue Linked List
class Node:
     def __init__(self):
          self.add=None
          self.data=None
class QueueList:
     def __init__(self):
          self.start=None

     # This is create Method
     def createList(self):
          n=int(input("Enter an element"))
          self.start=Node()
          self.start.data=n
          self.start.add=None
          temp=self.start
          choice=input("Want to continue(Y/N)")
          while(choice=="Y" or choice=="y"):
               n=int(input("Enter next element "))
               newnode=Node()
               newnode.data=n
               newnode.add=None
               temp.add=newnode
               temp=temp.add
               choice=input("Want to continue(Y/N)")
        

# This is insert Method
     def insertData(self):
          if(self.start==None):
               print("Queue not found")
          else:
               n=int(input("Enter an element")) 
               newnode=Node()
               newnode.data=n
               newnode.add=None
               last=self.start
               while(last.add!=None):
                    last=last.add
               last.add=newnode    

# This is delete Method
     def deleteData(self):
          if(self.start==None):
               print("Queue not found")
          else:
               temp=self.start
               self.start=self.start.add
               print("deleted element is: ",temp.data)
               temp.add=None
               temp=None     

# This is display Method
     def displayData(self):
            if(self.start==None):
               print("Queue not found")
            else:
                 temp=self.start
                 while(temp!=None):
                      print(temp.data,end=" ")
                      temp=temp.add


# Main Menu
mylist=QueueList()
while(1):
    print("-----------------Queue Linked List-----------------")
    print("1.Create")
    print("2.Insert")
    print("3.Delete")
    print("4.Display")
    print("5.Exit")
    print("------------------------------------------------------")
    choice=int(input("Enter your choice"))
    if(choice==1):
         mylist.createList()
    elif(choice==2):
         mylist.insertData()
    elif(choice==3):
         mylist.deleteData()
    elif(choice==4):
         mylist.displayData()
    else:
         break

 

courses

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

Your email address will not be published. Required fields are marked *