Remove duplicates from unsorted linked list in vb.net

Vb program for Remove duplicates from unsorted linked list. Here problem description and explanation.

' Include namespace system
Imports System 
'  Vb.net Program to 
'  Delete duplicate nodes in unsorted 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 LinkedList 
    Public  head As LinkNode
    Public  tail As LinkNode
    '  Class constructors
    Public Sub New()
        
        Me.head = Nothing
        Me.tail = Nothing
    
    End Sub
    '  Insert new element at end position
    Public Sub insert(ByVal value As Integer)
        '  Create new node
        Dim node As LinkNode = New LinkNode(value)
        if (Me.head  Is  Nothing) Then
            '  Add first node
            Me.head = node
        Else 
            '  Add new node at the last position
            Me.tail.[next] = node
        End IF

        '  Make new tail 
        Me.tail = node
    
    End Sub
    '  Display all node value
    Public Sub display()
        if (Me.head IsNot Nothing) Then
            Console.Write("Linked List Element :")
            Dim temp As LinkNode = Me.head
            
            while (temp IsNot Nothing) 
                '  Display node value
                Console.Write("  " + temp.data.ToString())
                '  Visit to next node
                temp = temp.[next]
            End While
        Else 
            Console.WriteLine("Empty Linked list")
        End IF

    
    End Sub
    
    Public Sub removeNode()
        if (Me.head  Is  Nothing) Then
            '  When linked list empty
            Console.Write("Empty Linked list")
        Else 
            '  Auxiliary variable
            Dim temp As LinkNode = Me.head
            Dim hold As LinkNode = Nothing
            Dim initial As LinkNode = Nothing
            Dim current As LinkNode = Nothing
            '  Outer loop
            while (temp IsNot Nothing) 
            
                current = temp
                initial = current.[next]
                '  Inner loop
                '  Remove all node which value is similar to temp node      
                while (initial IsNot Nothing) 
                
                    if (temp.data = initial.data) Then
                        '  Get remove node
                        hold = initial
                    Else 
                        current = initial
                    End IF

                    '  Visit to next node
                    initial = initial.[next]
                    if (hold IsNot Nothing) Then
                        current.[next] = initial
                        '  remove node
                        hold = Nothing
                    End If

                End While
                '  Visit to next node
                temp = temp.[next]
            End While
            '  New last node
            Me.tail = current
        End IF

    
    End Sub
    
    Public Shared Sub Main(ByVal args As String())
        '  new linked list
        Dim task As LinkedList = New LinkedList()
        '  Add tested element
        task.insert(1)
        task.insert(2)
        task.insert(9)
        task.insert(4)
        task.insert(9)
        task.insert(3)
        task.insert(1)
        task.insert(7)
        task.insert(2)
        task.insert(1)
        Console.WriteLine( vbLf & "Before Delete ")
        task.display()
        task.removeNode()
        Console.WriteLine( vbLf & "After Delete ")
        task.display()
    
    End Sub

End Class

Output

Before Delete
Linked List Element :  1  2  9  4  9  3  1  7  2  1
After Delete
Linked List Element :  1  2  9  4  3  7


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







© 2021, kalkicode.com, All rights reserved