Delete middle node from linked list in vb

Vb program for Delete middle node from linked list. Here problem description and explanation.

' Include namespace system
Imports System 
'    Vb.net program for
'    Delete middle node 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 Sub New()
        
        Me.head = Nothing
    
    End Sub
    '  Adding new node at beginning of linked list
    Public Sub addNode(ByVal data As Integer)
        '  Create new node
        Dim node As LinkNode = New LinkNode(data)
        '  Connect current node to previous head
        node.[next] = Me.head
        Me.head = 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) 
        
            '  Display node value
            Console.Write(temp.data.ToString() + " → ")
            '  Visit to next node
            temp = temp.[next]
        End While
        Console.Write(" NULL" & vbLf )
    
    End Sub
    '  Delete the middle node of linked list
    
    Public Sub deleteMid()
        if (Me.head  Is  Nothing) Then
            '  When linked list are no elements
            Console.WriteLine("Empty Linked List")
        ElseIf (Me.head.[next]  Is  Nothing AndAlso 
                Me.head.[next].[next]  Is  Nothing) Then
            '  When linked list are less than  of 3 nodes
            Console.WriteLine("Less then 3 node of linked list")
        Else 
            Dim temp As LinkNode = Me.head
            Dim midPrevious As LinkNode = Nothing
            '  Find the middle and its previous node
            
            while (temp IsNot Nothing AndAlso temp.[next] IsNot Nothing AndAlso
            temp.[next].[next] IsNot Nothing) 
            
                if (midPrevious  Is  Nothing) Then
                    '  When first node
                    midPrevious = temp
                Else 
                    '  Visit to next node
                    midPrevious = midPrevious.[next]
                End IF

                '  Visit to next second next node
                temp = temp.[next].[next]
            End While
            '  Get the node which is deleting 
            temp = midPrevious.[next]
            '  Show deleted node
            Console.WriteLine("Delete node : " + temp.data.ToString())
            '  Change link
            midPrevious.[next] = temp.[next]
        End IF

    
    End Sub
    
    Public Shared Sub Main(ByVal args As String())
        Dim sll As SingleLL = New SingleLL()
        '  Linked list
        '  7 → 6 → 5 → 4 → 3 → 2 → 1 → NULL
        sll.addNode(1)
        sll.addNode(2)
        sll.addNode(3)
        sll.addNode(4)
        sll.addNode(5)
        sll.addNode(6)
        sll.addNode(7)
        Console.WriteLine("Before Delete middle node")
        sll.display()
        '  Delete middle node
        sll.deleteMid()
        Console.WriteLine("After Delete middle node")
        sll.display()
    
    End Sub

End Class

Output

Before Delete middle node
7 → 6 → 5 → 4 → 3 → 2 → 1 →  NULL
Delete node : 4
After Delete middle node
7 → 6 → 5 → 3 → 2 → 1 →  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