How can I reverse a stack?

Posted by come pollinate me on Stack Overflow See other posts from Stack Overflow or by come pollinate me
Published on 2011-04-21T22:44:25Z Indexed on 2012/03/31 5:29 UTC
Read the original article Hit count: 369

Filed under:
|
|
|

I need to write a VB.NET code to reverse the given characters using a stack. Input: 'S','T','A','C','K'

So far I have input the letters, but I don't know how to get the console to reverse it. I'm a beginner to programming so please excuse my ignorance.

An explanation as to how it's done would also be greatly appreciated.

What I got so far.

Module Module1

Sub Main()
    Dim StackObject As New Stack
    StackObject.Push("S")
    Console.WriteLine(StackObject.Peek)
    StackObject.Push("T")
    Console.WriteLine(StackObject.Peek)
    StackObject.Push("A")
    Console.WriteLine(StackObject.Peek)
    StackObject.Push("C")
    Console.WriteLine(StackObject.Peek)
    StackObject.Push("K")
    Console.WriteLine(StackObject.Peek)
End Sub

End Module

I just need it to be reversed.

I got it!!

Module Module1

Sub Main()
    Dim StackObject As New Stack
    StackObject.Push("S")
    StackObject.Push("T")
    StackObject.Push("A")
    StackObject.Push("C")
    StackObject.Push("K")

    For Each cur As String In StackObject
        Console.WriteLine(cur)
    Next
End Sub

End Module

That's how it's done.

© Stack Overflow or respective owner

Related posts about vb.net

Related posts about homework