Hi,

I am trying (Push)add data into my stack (length of five) and (Pop)delete from the end of my stack.
I need to know how can:

- Add the elements to the stack with keeping track of adding and deleting from it (adding an element will increment until 5 and deleting would decrement until the stack is empty).

Thank you in advance.

Private Sub Button1Push_Click(sender As System.Object, e As System.EventArgs) Handles Button1Push.Click
        Dim Stack(4) As String
        Dim userMsg As String

        userMsg = InputBox("Push", "Stacks", "Enter your letter here", 500, 500)

        For Index = 0 To userMsg.Length - 1

        Next

    End Sub

    Private Sub Button2Pop_Click(sender As System.Object, e As System.EventArgs) Handles Button2Pop.Click

    End Sub

Where is your stack declaration? Dim Stack(4) As String -> Your Declaration is for array

You need to declare stack before add it. Dim MyStack as New Stack(4) -> This for Stack

To Add stack MyStack.Push(userMsg )

' Declare as global variable
Dim MaxSize As Integer = 4
Dim MyStack As New Stack(maxsize)

Private Sub Button1Push_Click(sender As System.Object, e As System.EventArgs) Handles Button1Push.Click
       
        Dim userMsg As String

        userMsg = InputBox("Push", "Stacks", "Enter your letter here", 500, 500)
        If MyStack.Count <= MaxSize Then
            MyStack.Push(userMsg)
        Else
            MsgBox("Stack is Full")
        End If
End Sub

Private Sub Button2Pop_Click(sender As System.Object, e As System.EventArgs) Handles Button2Pop.Click
    MsgBox(MyStack.Pop) ' Remove and return object at the top of stack
End Sub
commented: Really Good explanation +3

Thank you for your quick reply, but how if I need to show these inputs (userMsg) in a (label) ? To find out what is the user entering (Pushing) into the stack and what is going to be left after I (Pop) the items ?

Thank you in advance.

Function to check if stack empty or not :

Private Function IsEmpty() As Boolean
    If Stacks.Count = 0 Then
        Return True
    Else
        Return False
    End If
End Function

how if I need to show these inputs (userMsg) in a (label) ? To find out what is the user entering (Pushing) into the stack

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    If IsEmpty() Then
        MsgBox("No elements in stack")
    Else
        Label1.Text = Stacks(0).ToString) 'Show the top of stack (last inputted data)
    End If
End Sub

what is going to be left after I (Pop) the items ?

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    Dim i As Integer
    ListBox1.Items.Clear()
    If IsEmpty() Then
        MsgBox("No elements in stack")
    Else
        For i = 0 To Stacks.Count - 1
            ListBox1.Items.Add(MyStack(i).ToString) ' load all data in stack to listbox
        Next
    End If
End Sub

Thank you for this detailed explanation.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.