Skip to main content

Effectively insert node at beginning of circular linked list in

Vb program for Effectively insert node at beginning of circular linked list . Here problem description and other solutions.

' Include namespace system
Imports System 
' Program 
'  Insert node at beginning of circular linked list
'  Using head and tail nodes

'  Define class of linked list Node
Public Class LinkNode 
    Public  data As Integer
    Public  [next] As LinkNode
    Public Sub New(ByVal data As Integer, 
                   ByVal first As LinkNode)
        '  Set node value = data = first
    End Sub
End Class
public Class CircularLinkedList 
    Public  head As LinkNode
    Public  tail As LinkNode
     Public Sub New()
        '  Set head and tail values
        Me.head = Nothing
        Me.tail = Nothing
    End Sub
    '  Insert node at begining of circular linked list
    Public Sub insert(ByVal value As Integer)
        '  Create a node
        Dim node As LinkNode = New LinkNode(value, Me.head)
        if (Me.tail  Is  Nothing) Then 
            '  First node of linked list
            Me.tail = node
        End If

        '  Make new head
        Me.head = node
        '  Connecting the last node to the top of the next field
        Me.tail.[next] = Me.head
    End Sub
    '  Display node element of circular linked list
    Public Sub display()
        if (Me.head  Is  Nothing) Then 
            Console.WriteLine("Empty Linked List")
            Console.Write("Linked List Element :")
            Dim temp As LinkNode = Me.head
            '  iterate linked list
            while (temp IsNot Nothing) 
                '  Display node
                Console.Write("  " +
                if (temp  Is  Me.tail) Then 
                    '  Stop iteration
                    '  Or break;
                End If

                '  Visit to next node
                temp = temp.[next]
            End While
        End IF

    End Sub
    Public Shared Sub Main(ByVal args As String())
        Dim task As CircularLinkedList = New CircularLinkedList()
        '  Add following linked list nodes
        '  Display node
    End Sub

End Class


Linked List Element :  1  2  3  4  5  6  7  8


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