Investing - FI

Visual Basic Tutorial 15 – Collections.Generic (Stack)

The stack is similar to the three other collections we’ve seen so far. The major difference resides in its structure. We can compare the Stack to a plate stack in a restaurant. The first plate that will be put in the stack will be the last one to be taken by a client. It’s the same principle for the Generic Stack. Here is an illustration:

First In, Last Out

The gray shapes are the plates, and the orange/blue shapes are the sides of the plate stack. As you can see, when you push a plate on the stack, it will be the first one to pop back out. Frankly, I’ve used the Stack just a few times in some really particular cases but it still came handy in those cases. For example, if I want to fetch an html text, I can store the tags in a stack because the first tag to come out should be the last one I put on the stack just like:

html – head – title and then /title – /head – /html backward.

That’s one thing you can do with a stack, but again, it’s useful to know that it exists for when the time comes you really need it!

Declaration

Here is how you declare a Stack that can contain String values.

Dim HTMLTags = New Stack(Of String)

Looks like the other collections doesn’t it?! You can also put any type of data in it, it could have been a Stack of Decimal too!

Push(T Element)

As with the plate stack, you can push and object using the method Push, in our case it’s going to be an object of type String.

'Push tags in the stack
HTMLTags.Push("html")
HTMLTags.Push("head")
HTMLTags.Push("title")

Peek()

The Peek method takes no argument but returns the next element that will be pop’ed.

'Peek the top element on the stack
Console.WriteLine("Peeked Tag: " & HTMLTags.Peek())

Pop()

Removes and returns the top element from the stack. Warning, you must retrieve the element Pop’ed in a variable or use it immediately because it will be removed from the stack as soon as you call the Pop method.

'Pop the top element
Dim FirstTag As String = HTMLTags.Pop()
Dim SecondTag As String = HTMLTags.Pop()
Dim ThirdTag As String = HTMLTags.Pop()

Console.WriteLine("First Tag: " & FirstTag)
Console.WriteLine("Second Tag: " & SecondTag)
Console.WriteLine("Third Tag: " & ThirdTag)

Result

This window shows what it should give you as a result.

Peek Pop

Complete Code

Here is the complete code for this exercise, you should put some breakpoints and play with it!

Module Program
    Sub Main()
        Dim HTMLTags = New Stack(Of String)

        'Push tags in the stack
        HTMLTags.Push("html")
        HTMLTags.Push("head")
        HTMLTags.Push("title")

        'Peek the top element on the stack
        Console.WriteLine("Peeked Tag: " & HTMLTags.Peek())

        'Pop the top element
        Dim FirstTag As String = HTMLTags.Pop()
        Dim SecondTag As String = HTMLTags.Pop()
        Dim ThirdTag As String = HTMLTags.Pop()

        Console.WriteLine("First Tag: " & FirstTag)
        Console.WriteLine("Second Tag: " & SecondTag)
        Console.WriteLine("Third Tag: " & ThirdTag)

        Console.ReadKey()
    End Sub
End Module

The Last Generic collection for now

This is the last Generic Collection we’re going to see for now. What’s important to remember is that these collections can apply to any type of object, that’s what is so great about them and that’s why you should use them as soon as you need something more complex than a simple array! Don’t forget that these collections (List, Dictionary, Queue and Stack) are not thread safe.

The next post will be an exercise and we will dive into the object oriented programming concepts (well more like a practical overview!).

Next article Visual Basic/C# Exercise 4 - Collections.Generic
Previous article Visual Basic Tutorial 14 - Collections.Generic (Queue)

Related posts

0 Comments

No Comments Yet!

You can be first to comment this post!

Leave a Comment

*