0

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
2
Contributors
4
Replies
5
Views
5 Years
Discussion Span
Last Post by Alex_2011
Featured Replies
  • 1

    Where is your stack declaration? [ICODE]Dim Stack(4) As String[/ICODE] -> Your Declaration is for array You need to declare stack before add it. [ICODE]Dim MyStack as New Stack(4)[/ICODE] -> This for Stack To Add stack [ICODE]MyStack.Push(userMsg[/ICODE]) [CODE] ' Declare as global variable Dim MaxSize As Integer = 4 Dim MyStack … Read More

  • 1

    Function to check if stack empty or not : [CODE]Private Function IsEmpty() As Boolean If Stacks.Count = 0 Then Return True Else Return False End If End Function[/CODE] [QUOTE]how if I need to show these inputs (userMsg) in a (label) ? To find out what is the user entering (Pushing) … Read More

1

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

Edited by Jx_Man: n/a

Votes + Comments
Really Good explanation
0

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.

1

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
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.