< !- START disable copy paste -->

Sunday, 25 February 2018

Towers of Hanoi using python

This is Towers of Hanoi program. This concept uses three towers namely source, intermediate and destination. The concept is to place the disks from source tower to destination tower. Smaller disk should be always on larger disk vice versa is not possible. The order of disks on source tower and on destination tower should be same. This concept uses recursion.

Source code:

def hanoi(n,s,i,d):
    if n==1:
        print("move 1 disk from",s,"to",d)
        return
    else:
        hanoi(n-1,s,d,i)
        hanoi(1,s,i,d)
        hanoi(n-1,i,s,d)

n=int(input("enter no of disks"))
hanoi(n,"s","i","d")

 
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 ================================
>>> 
enter no of disks3
move 1 disk from s to d
move 1 disk from s to i
move 1 disk from d to i
move 1 disk from s to d
move 1 disk from i to s
move 1 disk from i to d
move 1 disk from s to d
>>>
Now my new Blog on Fundamentals of Python can be found at https://fundamentalsofpython.blogspot.com/2020/02/list-manipulations.html

Thursday, 22 February 2018

Queue using Linked list

This program is about Queue using Linked list. This program contains functions
1.Insert
2.Delete
3.Display

Source code:

class Node:
    def __init__(self,data):
        self.data=data
        self.next=None
class Queuell:
    def __init__(self):
        self.rear=None
        self.front=None
    def insert(self):
        data=int(input("enter data"))
        newnode=Node(data)
        if self.front == None:
            self.front=newnode
            self.rear=newnode
        else:
            self.rear.next=newnode
            self.rear=newnode
        print("element inserted")
    def delete(self):
        if self.front == None:
            print("empty")
        else:
            temp=self.front
            print("element deleted is %d" %(temp.data))
            self.front=self.front.next
            del temp

    def display(self):
        if self.front==None:
            print("empty")
        else:
            temp=self.front
            print("elements in Queue")
            while temp!=None:
                print("%d" %(temp.data))
                temp=temp.next
def menu():
    print("1.insert\n2.delete\n3.display\n4.quit")
def stop():
    print("you are about to terminate the program")
    exit(0)     
s=Queuell()
def default():
    print("check your input")
menu()
while True:
    menu= {
    1: s.insert,
    2: s.delete,
    3: s.display,
    4: 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.insert
2.delete
3.display
4.quit
Please enter your choice1
enter data1
element inserted
Please enter your choice1
enter data2
element inserted
Please enter your choice1
enter data3
element inserted
Please enter your choice3
elements in Queue
1
2
3
Please enter your choice2
element deleted is 1
Please enter your choice3
elements in Queue
2
3
Please enter your choice2
element deleted is 2
Please enter your choice3
elements in Queue
3
Please enter your choice2
element deleted is 3
Please enter your choice3
empty
Please enter your choice4
Now my new Blog on Fundamentals of Python can be found at https://fundamentalsofpython.blogspot.com/2020/02/list-manipulations.html

Stacks Using Linked list

This program is stacks using linked list. the program covers the following funtions:
1.Push
2.Pop
3.Display

Source Code:


class Node:
    def __init__(self,data):
        self.data=data
        self.next=None
class Stackll:
    def __init__(self):
        self.start=None
        self.top=None
    def push(self):
        data=int(input("enter data"))
        newnode=Node(data)
        if self.start==None:
           self.start=newnode
           self.top=newnode
        else:
            temp=self.start
            while temp.next!=None:
                temp=temp.next
            temp.next=newnode
            top=newnode
    def pop(self):
        temp=self.start
        if temp.next ==None:
            print ("last element of stack deleted is %d" %(temp.data))
            self.start=None
            del self.top
            self.top=None
        else:
            temp=self.start
            prev=self.start
            while temp.next != None:
                prev=temp
                temp=temp.next
         
            prev.next=None
            del temp
     
 
    def display(self):
        if self.top==None:
            print("stack empty")
        else:
            temp=self.start
            print("elements in stack are")
            while temp!=None:
                print ("%d" %(temp.data))
                temp=temp.next
             
def menu():
    print("1.push\n2.pop\n3.display\n4.quit")
def stop():
    print("you are about to terminate the program")
    exit(0)     
s=Stackll()
def default():
    print("check your input")
menu()
while True:
    menu= {
    1: s.push,
    2: s.pop,
    3: s.display,
    4: 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.push
2.pop
3.display
4.quit
Please enter your choice1
enter data10
Please enter your choice1
enter data20
Please enter your choice1
enter data30
Please enter your choice3
elements in stack are
10
20
30
Please enter your choice2
Please enter your choice3
elements in stack are
10
20
Please enter your choice2
Please enter your choice3
elements in stack are
10
Please enter your choice2
last element of stack deleted is 10
Please enter your choice3
stack empty

Please enter your choice4

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

     
         

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

Tuesday, 13 February 2018

Representation of Polynomial using single Linked list

This program is about representation of Polynomial expression using single linked list. Polynomial representation is one of the applications of single linked list.

Source Code:

class Node:

   def __init__(self,coeff,expo):
        self.coeff = coeff
        self.expo = expo
        self.next = None

class Polynomial:

    def __init__(self):
        self.start = None
     
    def createnode(self):
       coeff = int(input("enter coeff"))
       expo=int(input("enter expo"))
       newnode = Node(coeff,expo)
       return newnode
   
    def createpolynomiallist(self):
        while True:
            print("do you want to create polynomial node")
            answer=input()
            if answer == 'n':
                break
            newnode = self.createnode()
            if self.start == None:
               self.start = newnode
            else:
               temp=self.start
               while temp.next != None:
                 temp=temp.next
               temp.next=newnode
       
    def display(self):
         temp=self.start
         while temp!= None:
            print("%dx^%d+" %(temp.coeff,temp.expo),end="")
            temp=temp.next
            #print ("%d" %(temp.data))
   
p=Polynomial()
p.createpolynomiallist()
p.display()

 
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 ================================
>>>
do you want to create polynomial node
y
enter coeff3
enter expo2
do you want to create polynomial node
y
enter coeff2
enter expo1
do you want to create polynomial node
y
enter coeff1
enter expo0
do you want to create polynomial node
n
3x^2+2x^1+1x^0+
>>>



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

Circular Single linked list using python


This program is about circular single linked list. In circular single linked list as the last node contains the address of first node there will be no concept of Null. Either for deletion or insertion it is necessary to find the last node in this linked list except for insertion and deletion at intermediate position.

The program covers the following functions:

1.Creation of circular linked list
2.Insertion of node at beginning
3.Insertion of node at the end
4.Insertion of node at intermediate position
5.Deletion of node at beginning
6.Deletion of node at the end
7.Deletion of node at intermediate position
8.Count of nodes
9.Display of nodes
class Node:

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

class Csll:

    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
         newnode.next=self.start
       

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

    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.next
                 i=i+1
              prev.next=newnode
              newnode.next=temp
           
        else:
              print("check position")       
       
    def count(self):
        nc=1
        temp=self.start
        while temp.next!=self.start:
           nc=nc+1
           temp=temp.next
        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.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 != self.start:
                prev=temp
                temp=temp.next
         
            prev.next=self.start
            del temp
         
     
    def insertbegin(self):
        data=int(input("enter value"))
        newnode = Node(data)
        if self.start == None:
               self.start = newnode
               newnode.next=self.start
        else:
           temp=self.start
           while temp.next!=self.start:
              temp=temp.next
           newnode.next=self.start
           self.start=newnode
        temp.next=self.start
     
     
    def deletebegin(self):
        global prev
        if self.start == None:
            print('empty')
        else:
            temp=last=self.start
            while last.next!=self.start:
               last=last.next
            self.start=self.start.next
            last.next=self.start
            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 != self.start:
            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=Csll()
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 choice4
enter value6
enter position3
number of nodes till now:5
Please enter your choice9
elements in single linked list are:
4
1
6
2
3
5
Please enter your choice8
number of nodes till now:6
Please enter your choice5
Please enter your choice9
elements in single linked list are:
1
6
2
3
5
Please enter your choice6
Please enter your choice9
elements in single linked list are:
1
6
2
3
Please enter your choice7
enter position2
number of nodes till now:4
node deleted
Please enter your choice9
elements in single linked list are:
1
2
3
Please enter your choice8
number of nodes till now:3
Please enter your choice


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

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

Reverse Doubly Linked List

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