Home
Username:
Password:
Introduction to Microsoft C# 2008 Tutorials

Arrays & Collections / Stacks




Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com’s entire library of video tutorials.


Learn More

Subtitles of the Movie

Now let's take a look at Stacks. Now in a previous video we talked about Queues and really a stack is almost the exact opposite of a queue because whereas a queue was a first-in first-out the stack is a last-in first-out; in other words think of a stack just like when you're washing dishes and so you put a dish on the counter, then you grab another one and you dry it and you put it on top of that one, then you put another one up there, then you put another one. The one you're going to pick up first is the last one that you put on the stack and that is exactly what this stack does in C# and so what I want to do is take you through this code and again you'll notice I created a solution and a project called VTCStack and I will save that and make sure I've saved it and I will include that in the Working Files so you can open these up and play with them and follow right along with me. So, first up let's talk about how to create a stack and a stack is created with this statement right here. Notice we're going to use the Class name Stack; we're going to tell it that it's going to hold Strings and we want it to be named ml. So we're creating a variable named ml and it's of a type Stack that stores Strings. We instantiate it with a new key word and we tell it to take it from the Stack Class and we don't mention a size. Now, the methods and properties for stack are a little different because they're built to reflect kind of the actions that a stack takes. We add items to a stack using the Push method and if you'll think you just simply push an item onto the stack and it goes onto the top. And then, to view an item, this is the very same thing as a queue, we're going to Peek. We're going to Peek into the Stack and see what's next, in other words, we're going to see what's going to come off the stack first. And what I want to show you is, notice the first thing we're going to do is we're going to add or push Mark onto the stack, then we'll push Ted onto the stack. Then we'll push Julie, then we'll push Ace, alright? Now notice when we run this a Message Box is going to pop up and tell us who will be the first person to be processed should we remove somebody from this stack? So, let's run the app. We click this and you'll notice Ace is the next item to be processed. When we ran Peek Ace is the guy who shows up. Let me do that again - I'll moved the box out of the way - so notice when I clicked the button here we have created our stack, then we pushed Mark, Ted, Alice and I'm sorry, Mark, Ted, Julie and Ace on there, there's an interesting slip for some of you who are old enough. The Ace then, when we Peek is going to be the first one to come off the stack, so notice he was the last in, he will be the first out. Now, let's continue on through the code here. I'm going to uncomment the rest of this and now we're going to use the Pop method. Now the Pop is the stack's version of Dequeue, OK? We're going to us Pop to grab the first item from the list or from the stack and delete it. It's going to take it off the stack. Now again, this is going to be the last item that was put on there. Then I'm going to just throw a Message Box up to show you what the next item is going to be. Now if we take Ace off then the Peek should show us Julie, right, because she was the next to last that was put on. She's now the newest person on there. Then I'll just do a Message Box and show you the Count property on the stack is the same as the other ones and it will just simply give you the items in the stack since I have taken, since I have popped one off. We had four, I'm going to pop one off, then we should have 3. So, let's step through this. So I will kick this code off, move this over so we can watch and when I click the button we pushed four people on right here and then we peeked and Ace was the next person. Now right down here you can't see the code - let me see if I can pull this down, there we go - but we're going to Pop somebody off and then we're going to Peek again and see who's next and we're going to Pop Ace off and notice Julie is the next one. So, Julie's going to be the next one and now if we count the items in our stack we only have 3 because we popped Ace off of there. Now as you can see, the big deal with stack and queue and sorted lists and all these various types of collections that we're talking about in these videos, Microsoft has taken the idea of an Array and they've added classes into that .NET Framework, just like Stack here, that take care of a lot of the organization and housekeeping for you and all you have to do is call these various methods to look and see what's next, to pop, to grab something off of there, to push something onto the stack - so any time you have a list of things that you need to manipulate in some sort of order, all you need to do now is decide, OK, what works for this? Do I want to use a queue? Do I want to use a stack? A sorted list? What works better for me? So anyway, go back through these, keep these in mind and put these in your little administrative toolbox of things that you can pull out and throw into a program when you need to manipulate collections of data. So that's a stack.

Tutorial Information

Course: Introduction to Microsoft C# 2008
Author: Mark Long
SKU: 34046
ISBN: 1-935320-78-5
Release Date: 2009-10-09
Duration: 7 hrs / 76 lessons
Work Files: Yes
Captions: Available on CD and Online University
Compatibility: Vista/XP/2000, OS X, Linux
QuickTime 7, Flash 8

VTC Sign up & Benefits

  • Unlimited Access
  • 98,729 Video Tutorials (23,265 free)
  • Video Available as Flash or QuickTime
  • Over 1026 Courses
  • $30 for One Month Access
  • Multi-User Discounts Available