< !- START disable copy paste -->

Thursday, 15 February 2018

Double linked list

This is python program for Double linked list concept. This code covers the following functions of Double linked list.
1.Creation of the list
2.Insertion at the beginning of the list
3. Insertion at the end of list
4. Insertion at intermediate position in the list
5. Deletion from the beginning of the list
6. Deletion at the end of the list
7.Deletion from intermediate position of the list
8.Count nodes of a list
9.Display the list
10.Reverse display

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 insertend(self):
        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 insertmid(self):
        data=int(input("enter value"))
        newnode = Node(data)
        pos=int(input("enter position"))
        c=self.count()
        if pos>1 and pos<=c:
              temp=self.start
              prev=temp
              i=1
              while i<pos:
                 prev=temp
                 temp=temp.right
                 i=i+1
              prev.right=newnode
              newnode.left=prev
              temp.left=newnode
              newnode.right=temp
           
        else:
              print("check position")       
       
    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 deletemid(self):
      count=1
      if self.start==None:
         print("empty")
      else:
         position=int(input("enter position"))
         c=self.count()
         if position>c:
            print("check position")
         if position>1 and position<c:
            temp=prev=self.start
            while count<position:
               prev=temp
               temp=temp.right
               count=count+1
            prev.right=temp.right
            temp1=temp.right
            temp1.left=prev
            del temp
            print("node deleted")
         else:
            print("check position")
     
    def deleteend(self):
        global prev
        if self.start == None:
            print('empty')
        else:
            temp=self.start
            prev=self.start
            while temp.right != None:
                prev=temp
                temp=temp.right
            prev.right=None
            temp.left=None
            del temp
         
     
    def insertbegin(self):
        data=int(input("enter value"))
        newnode = Node(data)
        if self.start == None:
            self.start=newnode
        else:
           temp=self.start
           newnode.right=temp
           temp.left=newnode
           self.start=newnode
     
     
    def deletebegin(self):
        global prev
        if self.start == None:
            print('empty')
        else:
            temp=self.start
            self.start=self.start.right
            self.start.left=None
            del temp
                 
    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 reversedisplay(self):
       temp=self.start
       print("reverse display")
       if self.start==None:
          priint("empty")
       else:
          while temp.right!=None:
             temp=temp.right
          while temp!=None:
             print("%d" %(temp.data))
             temp=temp.left
     
       
def menu():
    print("1.createlist\n2.insertbegin\n3.insertend\n4.insertmid")
    print("5.deletebegin\n6.deleteend\n7.deletemid\n8.count\n9.display\n10.reversedisplay\n11.exit")
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.insertbegin,
    3: s.insertend,
    4: s.insertmid,
    5: s.deletebegin,
    6: s.deleteend,
    7: s.deletemid,
    8: s.count,
    9: s.display,
    10: s.reversedisplay,
    11:stop}
    option = int(input("Please enter your choice"))
    menu.get(option,default)()


Output:

Python 3.4.0 (v3.4.0:04f714765c13, Mar 16 2014, 19:24:06) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
1.createlist
2.insertbegin
3.insertend
4.insertmid
5.deletebegin
6.deleteend
7.deletemid
8.count
9.display
10.reversedisplay
11.exit
Please enter your choice1
enter no of nodes3
enter value2
enter value3
enter value4
Please enter your choice9
elements in double linked list are:
2
3
4
Please enter your choice10
reverse display
4
3
2
Please enter your choice2
enter value1
Please enter your choice9
elements in double linked list are:
1
2
3
4
Please enter your choice3
enter value5
Please enter your choice9
elements in double linked list are:
1
2
3
4
5
Please enter your choice4
enter value0
enter position3
number of nodes till now:5
Please enter your choice9
elements in double linked list are:
1
2
0
3
4
5
Please enter your choice7
enter position3
number of nodes till now:6
node deleted
Please enter your choice9
elements in double linked list are:
1
2
3
4
5
Please enter your choice5
Please enter your choice9
elements in double linked list are:
2
3
4
5
Please enter your choice6
Please enter your choice9
elements in double linked list are:
2
3
4
Please enter your choice8
number of nodes till now:3

Please enter your choice10


Now my new Blog on Fundamentals of Python can be found at https://fundamentalsofpython.blogspot.com/2020/02/list-manipulations.html

No comments:

Post a Comment

Reverse Doubly Linked List

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