< !- START disable copy paste -->

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

2 comments:

  1. Can you make a blog on polynomial addition using linked list in python?

    ReplyDelete
  2. I haven’t checked in here for some time because I thought it was getting boring, but the last few posts are really good quality so I guess I’ll add you back to my daily bloglist. You deserve it my friend. insurance guides https://python.engineering/ru_ru-python-string-to-int/

    ReplyDelete

Reverse Doubly Linked List

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