< !- START disable copy paste -->

Sunday, 11 February 2018

Single linked list program using python in data structures

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

source code:

class Node:

   def __init__(self,data):
        self.data = data 
        self.next = None

class Sll:

    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.next != None:
                 temp=temp.next
               temp.next=newnode
         

    def insertend(self):
        data=int(input("enter value"))
        newnode = Node(data)
        if self.start == None:
               self.start = newnode
       else:
        temp=self.start
        while temp.next != None:
                temp=temp.next
        temp.next=newnode
        

    def insertmid(self):
        data=int(input("enter value"))
        newnode = Node(data)
        pos=int(input("enter position"))
        c=self.count()
         if self.start == None:
               self.start = newnode
       else:
          if pos>1 and pos<=c:
              temp=self.start
              prev=temp
              i=1
              while i<pos:
                 prev=temp
                 temp=temp.next
                 i=i+1
          prev.next=newnode
          newnode.next=temp
              
                 
    def count(self):
        nc=0
        temp=self.start
        while temp!=None:
           nc=nc+1
           temp=temp.next
        print("number of nodes :%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.next
               count=count+1
            prev.next=temp.next
            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.next != None:
                prev=temp
                temp=temp.next
            
            prev.next=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.next=temp
        self.start=newnode
        
        
    def deletebegin(self):
        global prev
        if self.start == None:
            print('empty')
        else:
            temp=self.start
            newstart=self.start.next
            del temp
            self.start=newstart
                   
    def display(self):
      print("elements in single linked list are:")
      if self.start == None:
            print("empty")
      else:
         temp=self.start
         print ("%d" %(temp.data))
         while temp.next != None:
            temp=temp.next
            print ("%d" %(temp.data))
def menu():
    print("1.createlist\n2.insertbegin\n3.insertend\n4.insertmid")
    print("5.deletebegin\n6.deleteend\n7.deletemid\n8.count\n9.display\n10.exit")
def stop():
    print("you are about to terminate the program")
    exit(0)        
s=Sll()
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: 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.exit
Please enter your choice1
enter no of nodes3
enter value1
enter value2
enter value3
Please enter your choice9
elements in single linked list are:
1
2
3
Please enter your choice2
enter value4
Please enter your choice9
elements in single linked list are:
4
1
2
3
Please enter your choice3
enter value5
Please enter your choice9
elements in single linked list are:
4
1
2
3
5
Please enter your choice8
number of nodes :5
Please enter your choice5
Please enter your choice9
elements in single linked list are:
1
2
3
5
Please enter your choice6
Please enter your choice9
elements in single linked list are:
1
2
3
Please enter your choice7
enter position2
number of nodes :3
node deleted
Please enter your choice9
elements in single linked list are:
1
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...