implement queue using linked list in vb.net

Vb program for implement queue using linked list. Here problem description and explanation.

' Include namespace system
Imports System 
'  Vb.net Program for
'  Implement queue using linked list

'  Create Q node
Public Class QNode 
    Public  data As Integer
    Public  [next] As QNode
     Public Sub New(ByVal value As Integer)
        Me.data = value
        Me.next = Nothing
    End Sub
End Class

public Class MyQueue 
    Public  head As QNode
    Public  tail As QNode
    Public  count As Integer
    Public Sub New()
        
        Me.head = Nothing
        Me.tail = Nothing
        Me.count = 0
    End Sub
    
    Public Function  size() As Integer
        Return Me.count
    End Function 
    
    Public Function  isEmpty() As Boolean
        Return Me.count = 0
    
    End Function 
    '  Add new node of queue
    
    Public Sub enqueue(ByVal value As Integer)
        '  Create a new node
        Dim node As QNode = New QNode(value)
        if (Me.head  Is  Nothing) Then 
            '  Add first element into queue
            Me.head = node
        Else 
            '  Add node at the end using tail 
            Me.tail.[next] = node
        End IF

        Me.count += 1
        Me.tail = node
    
    End Sub
    
    '  Delete a element into queue
     Public Function  dequeue() As Integer
        if (Me.head  Is  Nothing) Then 
            Console.WriteLine("Empty Queue")
            Return -1
        End If
        '  Pointer variable which are storing 
        '  the address of deleted node
        Dim temp As QNode = Me.head
        '  Visit next node 
        Me.head = Me.head.[next]
        Me.count -= 1
        if (Me.head  Is  Nothing) Then 
            '  When deleting a last node of linked list
            Me.tail = Nothing
        End If

        Return temp.data
    
    End Function 
    '  Get front node
    
    Public Function  peek() As Integer
        if (Me.head  Is  Nothing) Then 
            Console.WriteLine("Empty Queue")
            Return -1
        End If

        Return Me.head.data
    
    End Function 
    
    Public Shared Sub Main(ByVal args As String())
        Dim task As MyQueue = New MyQueue()
        '  Initially number of element
        Console.WriteLine("isEmpty : " + task.isEmpty().ToString())
        '  Add element into queue
        task.enqueue(10)
        task.enqueue(20)
        task.enqueue(30)
        task.enqueue(40)
        task.enqueue(50)
        '  Test other function
        Console.WriteLine("size : " + task.size().ToString())
        Console.WriteLine("peek : " + task.peek().ToString())
        Console.WriteLine("dequeue : " + task.dequeue().ToString())
        Console.WriteLine("size : " + task.size().ToString())
        Console.WriteLine("peek : " + task.peek().ToString())
        Console.WriteLine("isEmpty : " + task.isEmpty().ToString())
    
    End Sub

End Class

Output

isEmpty : True
size : 5
peek : 10
dequeue : 10
size : 4
peek : 20
isEmpty : False


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