< !- START disable copy paste -->

Monday, 3 February 2020

Reverse Doubly Linked List

Source Code

class Node:
   def __init__(self,data):
        self.data = data
        self.left = None
        self.right = None

class Dll:
    def __init__(self):
        self.start = None

    def createlist(self):
         n=int(input("enter no of nodes"))
         for i in range(n):
            data = int(input("enter value"))
            newnode = Node(data)
            if self.start == None:
               self.start = newnode
            else:
               temp=self.start
               while temp.right != None:
                 temp=temp.right
               temp.right=newnode
               newnode.left=temp     

    def count(self):
        nc=0
        temp=self.start
        while temp!=None:
           nc=nc+1
           temp=temp.right
        print("number of nodes till now:%d" %nc)
        return nc               

    def display(self):
      print("elements in double linked list are:")
      if self.start == None:
            print("empty")
      else:
         temp=self.start
         while temp!= None:
            print("%d" %(temp.data))
            temp=temp.right
    def rev(self):     
        current = self.start       
        while current!=None:
            temp = current.left 
            current.left = current.right
            current.right = temp
            current = current.left       
        if temp!=None:
            self.start = temp.left
        print("Reverse done")

def menu():
    print("1.createlist\n2.count\n3.display\n4.reverse\n5.exit\n")   
def stop():
    print("you are about to terminate the program")
    exit(0)   
s=Dll()
def default():
    print("check your input")
menu()
while True:
    menu= {
    1: s.createlist,   
    2: s.count,
    3: s.display,
    4: s.rev,
    5:stop}
    option = int(input("Please enter your choice"))
    menu.get(option,default)()

output:

1.createlist
2.count
3.display
4.reverse
5.exit

Please enter your choice1
enter no of nodes3
enter value1
enter value2
enter value3
Please enter your choice2
number of nodes till now:3
Please enter your choice3
elements in double linked list are:
1
2
3
Please enter your choice4
Reverse done
Please enter your choice3
elements in double linked list are:
3
2
1
Please enter your choice5

Reverse Doubly Linked List

Source Code class Node:    def __init__(self,data):         self.data = data         self.left = None         self.right = None cla...