Skip to main content

Delete all even parity nodes from circular linked list in vb.net

Vb program for Delete all even parity nodes from circular linked list. Here more information.

' Include namespace system
Imports System 
'  Vb.net program for
'  Remove all even parity nodes from a circular single 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
    Public Sub insert(ByVal value As Integer)
        Dim node As LinkNode = New LinkNode(value)
        if (Me.head  Is  Nothing) Then
            Me.head = node
        Else
            Me.tail.[next] = node
        End IF
        node.[next] = Me.head
        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
            Console.WriteLine( vbLf &" Linked List Element ")
            '  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
    '  Handles the request to find set bits in given integer
    Public Function  coutSetBit(ByVal num As Integer) As Integer
        Dim result As Integer = num
        result = result - ((result >> 1) And 1431655765)
        result = (result And 858993459) + ((result >> 2) And 858993459)
        result = (result + (result >> 4)) And 252645135
        result = result + (result >> 8)
        result = result + (result >> 16)
        result = result And 63
        Return  result
    End Function
    Public Sub removeNode()
        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.coutSetBit(temp.data) Mod 2 = 0) Then
                auxiliary = temp
                '  Visit to next node
                temp = temp.[next]
                if (auxiliary  Is  Me.head) Then
                    '  Remove head node
                    if (auxiliary  Is  Me.tail) Then
                        '  Removing a single node
                        Me.tail = Nothing
                        Me.head = Nothing
                        temp = Nothing
                    Else
                        Me.head = temp
                        Me.tail.[next] = temp
                    End IF
                ElseIf (auxiliary  Is  Me.tail) Then
                    if (point IsNot Nothing) Then
                        '  When Remove last node
                        point.[next] = Me.head
                    End If
                    Me.tail = point
                Else
                    if (point IsNot Nothing) Then
                        '  When Remove last node
                        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(3)
        cll.insert(12)
        cll.insert(32)
        cll.insert(14)
        cll.insert(27)
        cll.insert(19)
        cll.insert(1)
        cll.insert(18)
        Console.Write( vbLf &" Before Remove ")
        cll.display()
        '    Node  Binary     Even Parity
        '     3     (11)         Yes
        '    12     (1100)       Yes
        '    32     (100000)     No
        '    14     (1110)       No
        '    27     (11011)      Yes
        '    19     (10011)      No
        '    1         (1)       No        
        '    18     (10010)      Yes
        cll.removeNode()
        Console.Write( vbLf & vbLf &" After Remove")
        cll.display()
    End Sub
End Class

Output

 Before Remove
 Linked List Element
  3  12  32  14  27  19  1  18

 After Remove
 Linked List Element
  32  14  19  1




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