Introduction to VB.NET Generic QueuesLink Round-Up for 8/23/07

Introduction To VB.NET Generic Stacks

August 22nd, 2007

A Wedding Cake - An example of a Last-In-First-Out Stack StructureStacks are another classic data structure. Anyone who’s taken a Computer Science 101 class has probably encountered them and if you’ve done any low level coding in C++ or assembly you’ve probably written quite a few of them. However, this data structure wasn’t seen that often in classic VB or traditional BASIC programming because of the lack of pointers in the language. But, in VB.NET, we now have the Stack class and, in Framework 2.0, the strongly typed generic Stack class so you should familiarize yourself with how a stack works.

A stack is simply a Last-In-First-Out (aka LIFO) buffer where only the topmost item is visible. It’s just the opposite of the Queue structure that was covered in a previous article. A common visual metaphor used to describe a stack is a spring loaded cafeteria plate dispenser or coin holder. The standard commands for a stack are Pop, which removes and returns the top item on the stack, and Push, which places a new item at the top of the stack. Here is a simple example:

'add a url entered in a text box


'get next url from stack and navigate to location

In this example, we have a stack of URLs entered by the user. As the user enters a value into the textbox, we Push it onto the stack. Later in the program, we want to use the browser control to navigate to the sites so we Pop the next URL value off the stack.

What if you want to see what the topmost value is before removing it from the stack? In that case, you use the Peek function to return the value without popping it off the stack. Here’s an example:

If UrlStack.Peek().Contains("localhost") Then

In this example, we may want to perform a few other actions if the site is local before removing the URL string from the stack.

The .NET generic stack also has the standard collection management properties and methods. To get the number of items in the collection you access the Count property. To see if an item is already in the stack, you can use the Contains function. Clear is used to empty the stack. Lastly, you can use the ToArray function to move the contents of the stack to an array of the same type. Here are some examples:

If UrlStack.Count > 0 Then
    'do something with the stack
End If

If Not UrlStack.Contains(txtUrl.Text) Then
End If


Dim UrlList() As String = UrlStack.ToArray()

Note that it is a good idea to check the Count property of the stack before you Pop or Peek since it will generate an InvalidOperationException if it is empty. Also, while you don’t have to worry about stack overflows like you do in C++ you should be mindful of how much memory your stack is using, particularly if you use it for large custom searches and sorts.

How can you use stacks in your code?

A common use for stacks are in complex searches. If you see your search code headed in a brute force, linear, model, step back and investigate how your code might benefit from using stacks. You can Google up many examples of how to do this or read Wikipedia articles on the various search algorithms. While they probably won’t be in VB.NET, you can probably extract the logic and use it.

Games are another area where using stacks can be quite helpful. For example, you’ll find many game map or maze building programs use stacks extensively.

If you have any questions about using Generic Stacks or if you have suggestions or examples on how you use them, please leave a comment.

Share This Article: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Reddit
  • StumbleUpon
  • Technorati
  • DotNetKicks
  • DZone

Entry Filed under: VB.NET Tutorials

Rate This Article:

Not That GoodCould Be BetterOKGoodGreat (No Ratings Yet)
Loading ... Loading ...

Leave a Comment


Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed

Visit Me At My New Site, Programming In C#

Most Popular Articles

Highest Rated Articles


Most Recent Articles


 Subscribe in a reader

To subscribe by e-mail
Enter your address here

Delivered by FeedBurner

VB Opportunities