Jump to content

C# Stacks

- - - - -

  • Please log in to reply
2 replies to this topic

#1
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
  • Programming Language:Java, C#, PHP
  • Learning:C, C++, C#, PHP, Transact-SQL, Assembly, Scheme
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:

Attached File  Stack1.jpg   8.25K   103 downloads

Adding another item:

 stack.Push(4);
 

The stack now looks like this:

Attached File  Stack2.jpg   9.18K   96 downloads

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:

Quote

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:

Quote

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:

Quote

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:

Quote

empty

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

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


#2
CommittedC0der

CommittedC0der

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,565 posts
Overall nice tut but dont you mean "empty"? :)

Quote

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:

Quote

not empty

~ Committed.
A man can be defined by what he does when no one is looking.
Science is only an educated theory, which we cannot disprove.

#3
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
  • Programming Language:Java, C#, PHP
  • Learning:C, C++, C#, PHP, Transact-SQL, Assembly, Scheme
Fixed!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users