# Multiply two polynomials using linked list in vb.net

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

``````' Include namespace system
Imports System
'    Vb.net 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)
Me.data = data
Me.power = power
Me.next = Nothing
End Sub
'  Update node value
Public Sub updateRecord(ByVal data As Integer,
ByVal power As Integer)
Me.data = data
Me.power = power
End Sub
End Class
public Class MultiplyPolynomial
Public Sub New()
End Sub
'  Insert Node element
Public Sub insert(ByVal data As Integer,
ByVal power As Integer)
Else
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
location.data = location.data + data
Else
node = New Node(data, power)
if (location  Is  Nothing) Then
'  When add node in begining
Else
'  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)
while (temp IsNot Nothing)
'  Get result info
power_value = poly1.power + temp.power
coefficient = poly1.data * temp.data
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()
Console.Write("Empty Polynomial ")
End If
Console.Write(" ")
Dim temp As Node = Me.head
while (temp IsNot Nothing)
Console.Write(" + " + temp.data.ToString())
Else
Console.Write(temp.data)
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 ")
a.display()
Console.WriteLine(" Polynomial B ")
b.display()
Dim result As MultiplyPolynomial = a.multiplyPolynomials(b)
'  Display calculated result
Console.WriteLine(" Result ")
result.display()
End Sub
End Class

``````

Output

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

## Comment

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.