Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

C# Stacks

stack

  • Please log in to reply
2 replies to this topic

#1 chili5

chili5

    CC Mentor

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3038 posts
  • Programming Language:Java, C#, PHP, JavaScript, Ruby, Transact-SQL
  • Learning:C, Java, C++, C#, PHP, JavaScript, Ruby, Transact-SQL, Assembly, Scheme, Haskell, Others

Posted 21 August 2011 - 05:20 AM

C# Stack

The Stack is a fundamental data structure in computer programming. The stack maintains a last-in, first-out (LIFO) order. This means that the first item you add is the last item to be popped (removed from the stack). For more details on implementation of stacks see WingedPanther’s tutorial here. The stack is useful for implementing various algorithms with data structures like trees and graphs. Also the stack is used in systems with recursive functions. So it is technically to write recursive functions iteratively using a stack you define yourself. The benefit of this is you do all your work in one stack frame.

This tutorial covers the use of Stacks in the C# programming language. First, make sure that you are using the System.Collections.Generics namespace with this code at the top of your .cs file:

 using System.Collections.Generics;

Creating a Stack

To create a stack we simply use the Stack constructor as follows:

 var stack = new Stack<int>();

This is just a stack that will hold integers. We can change int to any type we want to have stacks of different types.

Adding Items to the Stack

To add an item to a stack we simply use the Push method as follows which adds an item to the top of the stack. This method adds an item to the top of the stack.

For example:

 stack.Push(10);

The stack at this point now looks like this:

Stack1.jpg

Adding another item:

 stack.Push(4);

The stack now looks like this:

Stack2.jpg

Removing Items From the Stack

To remove an item from the top of the stack we use the pop method.
For example:

 int top = stack.Pop();
 Console.WriteLine(top);

The output is:

4


Top of Stack

The Peek method returns the top item of the stack but does not remove it from the stack.
Example:

var stack = new Stack<int>();
stack.Push(10);
stack.Push(4);
Console.WriteLine(stack.Peek());
Console.WriteLine(stack.Peek());
Console.ReadKey();

The output is:

4
4


Is the Stack Empty?


There is not a method in the Stack class to test if a stack is empty or not. To accomplish this we need to make use of the Count property.
Try this code:

var stack = new Stack<int>();
stack.Push(10);
stack.Push(4);
Console.WriteLine(stack.Peek());
if(stack.Count == 0)
    Console.WriteLine("empty");
else
    Console.WriteLine("not empty");

The output is:

not empty


Now let us try it with a Stack that is empty. First, let us use the Clear() method to erase the items in the stack.

var stack2 = new Stack<int>(); 
stack2.Push(10);
stack2.Push(5);
stack2.Push(8);

stack2.Clear();
if(stack2.Count == 0)
     Console.WriteLine("empty");
else
     Console.WriteLine("not empty");

The output is:

empty


If you have any questions feel free to ask. :)

Edited by chili5, 22 August 2011 - 01:34 PM.

  • 0

#2 CommittedC0der

CommittedC0der

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1065 posts
  • Programming Language:C#
  • Learning:C#, JavaScript, PL/SQL

Posted 22 August 2011 - 01:33 PM

Overall nice tut but dont you mean "empty"? :)

var stack2 = new Stack<int>();  
stack2.Push(10); 
stack2.Push(5); 
stack2.Push(8);  
stack2.Clear(); 
if(stack2.Count == 0)      
Console.WriteLine("empty"); 
else      Console.WriteLine("not empty");

The output is:

not empty


~ Committed.
  • 0
There IS a war going on for your mind.
Nothing of eternal significance happens besides prayer.

#3 chili5

chili5

    CC Mentor

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3038 posts
  • Programming Language:Java, C#, PHP, JavaScript, Ruby, Transact-SQL
  • Learning:C, Java, C++, C#, PHP, JavaScript, Ruby, Transact-SQL, Assembly, Scheme, Haskell, Others

Posted 22 August 2011 - 01:35 PM

Fixed!
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download