implement stack using linked list in vb.net

Vb program for implement stack using linked list. Here problem description and other solutions.

' Include namespace system
Imports System 
'  Vb.net program for
'  Implementation stack using linked list

'  Stack Node
Public Class StackNode
    Public  data As Integer
    Public  [next] As StackNode
    Public Sub New(ByVal data As Integer, 
                   ByVal top As StackNode)
        Me.data = data
        Me.next = top
    End Sub
End Class

Public Class MyStack
    Public  top As StackNode
    Public  count As Integer
    Public Sub New()
        Me.top = Nothing
        Me.count = 0
    End Sub
    '  Returns the number of element in stack
    Public Function  size() As Integer
        Return  Me.count
    End Function
    Public Function  isEmpty() As Boolean
        if (Me.size() > 0) Then
            Return  False
        Else
            Return  True
        End IF
    End Function
    '  Add a new element in stack
    Public Sub push(ByVal data As Integer)
        '  Make a new stack node
        '  And set as top
        Me.top = New StackNode(data, Me.top)
        '  Increase node value
        Me.count += 1
    End Sub
    '  Add a top element in stack
    Public Function  pop() As Integer
        Dim temp As Integer = 0
        if (Me.isEmpty() = False) Then
            '  Get remove top value
            temp = Me.top.data
            Me.top = Me.top.[next]
            '  Reduce size
            Me.count -= 1
        End If
        Return  temp
    End Function
    '  Used to get top element of stack
    Public Function  peek() As Integer
        if (Not Me.isEmpty()) Then
            Return  Me.top.data
        Else
            Return  0
        End IF
    End Function
End Class
public Class Test
    Public Shared Sub Main(ByVal args As String())
        '  Create new stack 
        Dim s As MyStack = New MyStack()
        Console.WriteLine( vbLf &" Is empty : {0}", s.isEmpty())
        '  Add element
        s.push(15)
        s.push(14)
        s.push(31)
        s.push(21)
        s.push(10)
        Console.WriteLine( vbLf &" Top  : {0}", s.peek())
        Console.WriteLine(" Size : {0}", s.size())
        Console.WriteLine( vbLf &" Is empty : {0}", s.isEmpty())
        '  Delete Stack Element
        Dim data As Integer = s.pop()
        Console.WriteLine( vbLf &" Pop element {0}", data)
        Console.WriteLine(" Size : {0}", s.size())
        data = s.pop()
        Console.WriteLine( vbLf &" Pop element {0}", data)
        Console.WriteLine(" Size : {0}", s.size())
    End Sub
End Class

Output

 Is empty : True

 Top  : 10
 Size : 5

 Is empty : False

 Pop element 10
 Size : 4

 Pop element 21
 Size : 3


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