Skip to main content

Multiply two polynomials using linked list in

Vb program for Multiply two polynomials using linked list. Here problem description and explanation.

' Include namespace system
Imports System 
' program for
'    Multiplication of two polynomials using linked list

Public Class Node
    Public  data As Integer
    Public  power As Integer
    Public  [next] As Node
    Public Sub New(ByVal data As Integer, 
                   ByVal power As Integer) = data
        Me.power = power = Nothing
    End Sub
    '  Update node value
    Public Sub updateRecord(ByVal data As Integer, 
                            ByVal power As Integer) = data
        Me.power = power
    End Sub
End Class
public Class MultiplyPolynomial
    Public  head As Node
    Public Sub New()
        Me.head = Nothing
    End Sub
    '  Insert Node element
    Public Sub insert(ByVal data As Integer, 
                      ByVal power As Integer)
        if (Me.head  Is  Nothing) Then
            '  Add first node
            Me.head = New Node(data, power)
            Dim node As Node = Nothing
            Dim temp As Node = Me.head
            Dim location As Node = Nothing
            '  Find the valid new node location
            while (temp IsNot Nothing AndAlso temp.power >= power)
                location = temp
                temp = temp.[next]
            End While
            if (location IsNot Nothing AndAlso 
                location.power = power) Then
                '  When polynomial power already exists
                '  Then add current add to previous data
       = + data
                node = New Node(data, power)
                if (location  Is  Nothing) Then
                    '  When add node in begining
                    node.[next] = Me.head
                    Me.head = node
                    '  When adding node in intermediate 
                    '  location or end location
                    node.[next] = location.[next]
                    location.[next] = node
                End IF
            End IF
        End IF
    End Sub
    '  Perform multiplication of given polynomial
    Public Function  multiplyPolynomials(
      ByVal other As MultiplyPolynomial) As MultiplyPolynomial
        '  Define some useful variable
        Dim result As MultiplyPolynomial = New MultiplyPolynomial()
        '  Get first node of polynomial
        Dim poly1 As Node = Me.head
        Dim temp As Node = other.head
        Dim power_value As Integer = 0
        Dim coefficient As Integer = 0
        '  Execute loop until when polynomial are exist
        while (poly1 IsNot Nothing)
            temp = other.head
            while (temp IsNot Nothing)
                '  Get result info
                power_value = poly1.power + temp.power
                coefficient = *
                result.insert(coefficient, power_value)
                '  Visit to next node
                temp = temp.[next]
            End While
            '  Visit to next node
            poly1 = poly1.[next]
        End While
        '  return first node
        Return  result
    End Function
    '  Display given polynomial nodes
    Public Sub display()
        if (Me.head  Is  Nothing) Then
            Console.Write("Empty Polynomial ")
        End If
        Console.Write(" ")
        Dim temp As Node = Me.head
        while (temp IsNot Nothing)
            if (temp IsNot Me.head) Then
                Console.Write(" + " +
            End IF
            if (temp.power <> 0) Then
                Console.Write("x^" + temp.power.ToString())
            End If
            '  Visit to next node
            temp = temp.[next]
        End While
        Console.Write( vbLf )
    End Sub
    Public Shared Sub Main(ByVal args As String())
        Dim a As MultiplyPolynomial = New MultiplyPolynomial()
        Dim b As MultiplyPolynomial = New MultiplyPolynomial()
        '  Add node in polynomial A
        a.insert(9, 3)
        a.insert(4, 2)
        a.insert(3, 0)
        a.insert(7, 1)
        a.insert(3, 4)
        '  Add node in polynomial b
        b.insert(7, 3)
        b.insert(4, 0)
        b.insert(6, 1)
        b.insert(1, 2)
        '  Display Polynomial nodes
        Console.WriteLine( vbLf &" Polynomial A ")
        Console.WriteLine(" Polynomial B ")
        Dim result As MultiplyPolynomial = a.multiplyPolynomials(b)
        '  Display calculated result
        Console.WriteLine(" Result ")
    End Sub
End Class


 Polynomial A
 3x^4 + 9x^3 + 4x^2 + 7x^1 + 3
 Polynomial B
 7x^3 + 1x^2 + 6x^1 + 4
 21x^7 + 66x^6 + 55x^5 + 119x^4 + 88x^3 + 61x^2 + 46x^1 + 12


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment