Welcome to AspAdvice Sign in | Join | Help

Orcs Goblins and .NET

I enjoy reading and writing. I hope you enjoy at least the former. I have moved my blog to Brendan.Enrick.com.

Syndication

Tags

News

Add to Technorati Favorites

Locations of visitors to this page

All C# Feeds in one place!

View Brendan Enrick's profile on LinkedIn

Brendan Enrick's Facebook profile

Navigation

Archives

Advice Sites

Articles

Blogs

Music

Understanding the Stack Data Structure

I found myself recently explaining a few times how the data structure commonly called a stack works. The technical term for a stack would be a Last in First out Data Structure (LIFO). To practically repeat myself, this means that the last object to go into the structure is the first one which will come out of the structure.

I've heard plenty of interesting analogies for this behavior. All too often I hear this being described as a stack of plates. Plates can be added easily onto the top of the stack, and can be removed from the top of the stack easily. Working with any plates not on the top is difficult because there are other plates on top of them. This is basically how a stack works.

While explaining the push() and pop() methods of stacks, I came up with an analogy which I think is far more interesting. To my students I described the stack as a Pez dispenser. You load the dispenser from the top and unload it from the top.

PezAnalogy

When you get a candy from the dispenser it comes out of the top and you now have possession of it. With a stack you get the value from the top and you now have possession of it. When adding candy to the dispenser you will add it on top of any existing candy. With a stack you will add new values on top of the existing values.

One extra method available to a Stack is the ability to "peek" at the top value without removing it. This is often necessary when using a stack, and also in a Pez dispenser. When you lift the head of the dispenser a little bit you can peek in to check what color is on top. If it is not one you like, you can still close the dispenser without having removed the candy. The same can be done with a stack.

Basic Implementation

Stacks may be implemented easily with an array or a linked-list as the underlying data structures. When drawing a stack it is far more common to draw the array version. There is one pointer of importance with a stack. This pointer is usually called the "top" pointer. It is a pointer which updates every time a "push" or a "pop" occurs, because this pointer will always point to the top of the stack. This is the pointer used to get to the stack and work with it.

The Push and Pop Operations work as the following image shows.

StackOperations

 

Enjoy, and look forward to more blog posts explaining interesting Computer Science topics.

Published Friday, October 05, 2007 10:30 AM by Brendan

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Simple C# Stack Implementation @ Tuesday, October 09, 2007 10:27 AM

I've just written a quick little stack implementation for use in explaining the stack data structure.

Orcs Goblins and .NET

# re: Understanding the Stack Data Structure @ Tuesday, August 12, 2008 8:07 PM

I am teaching Data Structures to Secondary School students in Australia. I found the analogy very useful. I will use your blog with my students.

Peter Carmody

# re: Understanding the Stack Data Structure @ Saturday, October 11, 2008 1:17 PM

I am teaching data structures to second year under graduate students. I found the article v informative and presentation of the article is v good. I would also like to be intimated about the updates of this article. My e-mail -id is: sudha_ramachandra@yahoo.co.in Thanx, sudha Ramachandra

sudha Ramachandra

# Simple C# Stack Implementation @ Wednesday, May 06, 2009 2:18 PM

I've just written a quick little stack implementation for use in explaining the stack data structure. It is written in C# and is not exactly a robust solution, but it works well enough. It is stubbed out with the basics of what is required to have a working

Brendan Enrick's Blog

# Simple C# Stack Implementation @ Tuesday, May 19, 2009 9:21 AM

I've just written a quick little stack implementation for use in explaining the stack data structure. It is written in C# and is not exactly a robust solution, but it works well enough. It is stubbed out with the basics of what is required to have a working

Brendan Enrick's Blog

# Simple C# Stack Implementation @ Monday, July 26, 2010 3:20 PM

Simple C# Stack Implementation

Brendan Enrick

# Simple C# Stack Implementation @ Friday, July 30, 2010 8:10 AM

Simple C# Stack Implementation

Brendan Enrick

Leave a Comment

(required) 
required 
(required) 
Enter the code you see below