Delete a node from linked list specific position in vb.net

Vb program for Delete a node from linked list specific position. Here problem description and explanation.

' Include namespace system
Imports System 
'  Vb.net Program for
'  Delete node at given index of linked list

'  Linked list node

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 SingleLL 
    Public  head As LinkNode
    Public  tail As LinkNode
    
    Public Sub New()
        
        Me.head = Nothing
        Me.tail = Nothing
    
    End Sub
    '  Add new node at the end of linked list
    Public Sub addNode(ByVal value As Integer)
        '  Create a new node
        Dim node As LinkNode = New LinkNode(value)
        if (Me.head  Is  Nothing) Then
            Me.head = node
        Else 
            Me.tail.[next] = node
        End IF

        Me.tail = node
    
    End Sub
    '  Display linked list element
    
    Public Sub display()
        if (Me.head  Is  Nothing) Then
            Return
        End If

        Dim temp As LinkNode = Me.head
        '  iterating linked list elements
        
        while (temp IsNot Nothing) 
        
            Console.Write(temp.data.ToString() + " → ")
            '  Visit to next node
            temp = temp.[next]
        End While
        Console.WriteLine("NULL")
    
    End Sub
    '  Delete node at given position
    
    Public Sub deleteAtIndex(ByVal index As Integer)
        Dim temp As LinkNode = Me.head
        if (Me.head  Is  Nothing) Then
            Console.WriteLine("Empty linked list")
            Return
        ElseIf (index < 0) Then
            Console.WriteLine("Invalid positions")
            Return
        ElseIf (index = 1) Then
            '  Means deleting a first node
            if (Me.tail  Is  Me.head) Then
                '  Only one node
                Me.tail = Nothing
            End If

            '  Make second node as head
            Me.head = Me.head.[next]
        Else 
            Dim count As Integer = index - 1
            '  iterating linked list elements and find deleted node
            
            while (temp IsNot Nothing AndAlso count > 1) 
                '  Visit to next node
                temp = temp.[next]
                count-=1
            End While
            if (count = 1 AndAlso temp.[next] IsNot Nothing) Then
                '  Get deleted node
                Dim n As LinkNode = temp.[next]
                if (n  Is  Me.tail) Then
                    '  When delete last node
                    Me.tail = temp
                End If

                '  Unlink the deleting node
                temp.[next] = n.[next]
            Else 
                Console.WriteLine(" Index " + 
                                  index.ToString() + " are not present")
                Return
            End IF

        End IF

    
    End Sub
    
    Public Shared Sub Main(ByVal args As String())
        Dim sll As SingleLL = New SingleLL()
        '  Test with your index
        '  Note index start with 1
        Dim index As Integer = 5
        '  Linked list
        '  10 → 20 → 30 → 40 → 50 → 60 → 70 → 80 → NULL
        sll.addNode(10)
        sll.addNode(20)
        sll.addNode(30)
        sll.addNode(40)
        sll.addNode(50)
        sll.addNode(60)
        sll.addNode(70)
        sll.addNode(80)
        Console.WriteLine(" Before Delete Linked List")
        sll.display()
        '  Delete node at 5-th position
        sll.deleteAtIndex(index)
        '  Display linked list
        Console.WriteLine(" After Delete Linked List")
        sll.display()
    
    End Sub

End Class

Output

 Before Delete Linked List
10 → 20 → 30 → 40 → 50 → 60 → 70 → 80 → NULL
 After Delete Linked List
10 → 20 → 30 → 40 → 60 → 70 → 80 → NULL


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