Posted on by Kalkicode
Code Single linked list

Sort absolute sorted linked list in vb.net

Vb program for Sort absolute sorted linked list. Here more information.

' Include namespace system
Imports System 
'  Vb.net program for
'  Sort linked list which is already 
'  sorted on absolute values

'  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()
        '  Set head value
        Me.head = Nothing
    End Sub
    '  Add new node at the end of linked list
    Public Sub insert(ByVal value As Integer)
        '  Create a node
        Dim node As LinkNode = New LinkNode(value)
        if (Me.head  Is  Nothing) Then
            Me.head = node
        Else
            Dim temp As LinkNode = Me.head
            '  Find last node
            while (temp.[next] IsNot Nothing)
                '  Visit to next node
                temp = temp.[next]
            End While
            '  Add node at last position
            temp.[next] = node
        End IF
    End Sub
    '  Display all Linked List elements
    Public Sub display()
        if (Me.head IsNot Nothing) Then
            Console.WriteLine("Linked list")
            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 sort()
        if (Me.head  Is  Nothing) Then
            Console.WriteLine("Empty linked list")
        Else
            Dim current As LinkNode = Me.head
            Dim temp As LinkNode = Nothing
            while (current IsNot Nothing AndAlso 
                   current.[next] IsNot Nothing)
                if (current.data > current.[next].data) Then
                    '  Move node at the beginning
                    temp = current.[next].[next]
                    current.[next].[next] = Me.head
                    '  Set new head
                    Me.head = current.[next]
                    current.[next] = temp
                Else
                    '  Visit to next node
                    current = current.[next]
                End IF
            End While
        End IF
    End Sub
    Public Shared Sub Main(ByVal args As String())
        Dim sll As SingleLL = New SingleLL()
        '  Linked list sll1
        '  0 → 5 → -5 → 6 → -7 → 8 → 9 → -10 → NULL
        sll.insert(0)
        sll.insert(5)
        sll.insert(-5)
        sll.insert(6)
        sll.insert(-7)
        sll.insert(8)
        sll.insert(9)
        sll.insert(-10)
        Console.WriteLine("Before sort")
        '  Display all node
        sll.display()
        sll.sort()
        Console.WriteLine( vbLf &"After sort")
        '  Display all node
        sll.display()
    End Sub
End Class

Output

Before sort
Linked list
  0  5  -5  6  -7  8  9  -10
After sort
Linked list
  -10  -7  -5  0  5  6  8  9

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