Skip to main content

Delete all nodes whose node digit sum is odd from circular linked list in vb.net

Vb program for Delete all nodes whose node digit sum is odd from circular linked list. Here more information.

' Include namespace system
Imports System 
'  Vb.net Program
'  Remove all the Odd digit sum nodes from a circular linked list

Public Class LinkNode
    Public  data As Integer
    Public  [next] As LinkNode
    Public Sub New(ByVal data As Integer)
        Me.data = data
        Me.next = Nothing
    End Sub
End Class

public Class CircularLinkedList
    Public  head As LinkNode
    Public  tail As LinkNode
    Public Sub New()
        '  Set initial value
        Me.head = Nothing
        Me.tail = Nothing
    End Sub
    ' Add node at the end position efficiently
    Public Sub insert(ByVal value As Integer)
        '  Create a new node
        Dim node As LinkNode = New LinkNode(value)
        if (Me.head  Is  Nothing) Then
            '  When first node
            Me.head = node
        Else
            '  Connect new node at the last
            Me.tail.[next] = node
        End IF
        '  Connect last node to first node
        node.[next] = Me.head
        '  Set new last node
        Me.tail = node
    End Sub
    '  Display node element of circular linked list
    Public Sub display()
        if (Me.head  Is  Nothing) Then
            Console.WriteLine( vbLf &" Empty Linked List")
        Else
            '  First node of linked list
            Console.Write("  " + Me.head.data.ToString())
            Dim temp As LinkNode = Me.head.[next]
            '  Display linked list node
            while (temp IsNot Nothing AndAlso temp IsNot Me.head)
                '  Display node value
                Console.Write("  " + temp.data.ToString())
                '  visit to next node
                temp = temp.[next]
            End While
        End IF
    End Sub
    '  Sum of digits
    Public Function  digitSum(ByVal num As Integer) As Integer
        Dim n As Integer = num
        Dim sum As Integer = 0
        if (n < 0) Then
            '  When node value is negative
            n = -n
        End If
        while (n > 0)
            sum += (n Mod 10)
            n = (n / 10)
        End While
        Return sum
    End Function
    Public Sub removeOddDigitSumNode()
        if (Me.head  Is  Nothing) Then
            Return
        End If
        Dim temp As LinkNode = Me.head
        Dim auxiliary As LinkNode = Me.head
        Dim point As LinkNode = Nothing
        while (temp IsNot Nothing)
            if ((Me.digitSum(temp.data) Mod 2) <> 0) Then
                auxiliary = temp
                temp = temp.[next]
                if (auxiliary  Is  Me.head) Then
                    '  Enter to remove first node
                    if (auxiliary  Is  Me.tail) Then
                        '  Enter to removing single node
                        Me.tail = Nothing
                        Me.head = Nothing
                        temp = Nothing
                        point = Nothing
                    Else
                        Me.head = temp
                        Me.tail.[next] = temp
                    End IF
                ElseIf (auxiliary  Is  Me.tail) Then
                    '  When Remove last node
                    if (point IsNot Nothing) Then
                        point.[next] = Me.head
                    End If
                    Me.tail = point
                Else
                    '  When removing intermediate node
                    if (point IsNot Nothing) Then
                        point.[next] = temp
                    End If
                End IF
                auxiliary = Nothing
            Else
                point = temp
                '  Visit to next node
                temp = temp.[next]
                if (temp  Is  Me.head) Then
                    '  Stop the process
                    temp = Nothing
                End If
            End IF
        End While
    End Sub
    Public Shared Sub Main(ByVal args As String())
        Dim cll As CircularLinkedList = New CircularLinkedList()
        '  Insert node
        cll.insert(131)
        cll.insert(12)
        cll.insert(33)
        cll.insert(10)
        cll.insert(143)
        cll.insert(91)
        cll.insert(27)
        cll.insert(125)
        cll.insert(89)
        Console.WriteLine( vbLf &" Before remove odd digit sum nodes")
        cll.display()
        '    Node   Digit Sum    Odd
        '    ----- -----------  --------
        '    131   [1+3+1] 5     Yes
        '    12    [1+2]   3     Yes
        '    33    [3+3]   6     No
        '    10    [10]    1     Yes
        '    143   [1+4+3] 8     No
        '    91    [9+1]   10    No
        '    27    [2+7]   9     Yes        
        '    125   [1+2+5] 8     No
        '    89    [8+9]   17    Yes
        '    --------------------------
        '    Result 
        '    ------
        '    33 → 143 → 91 → 125 ┐ 
        '     └-----------------⤶
        cll.removeOddDigitSumNode()
        Console.WriteLine( vbLf &" After remove odd digit sum nodes")
        cll.display()
    End Sub
End Class

Output

 Before remove odd digit sum nodes
  131  12  33  10  143  91  27  125  89
 After remove odd digit sum nodes
  33  143  27  125




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.

New Comment