Friday, November 19, 2010

[C#] - Stack and Queue

Stack and Queue


Introduction


Stack and Queue both are collection classes sported by the dot net framework. Queue operates on "First in First Out (FIFO)" principle. Stack operates on "Last in First out (LIFO)" principle. That is; when you remove an item from the Queue, the first added item will be removed first. In the case of the stack it is in the reverse order, I mean, the item added Last removed first.

To use Stack and Queue on your application first, include the namespace "System.Collection".

//000: Use the Collection namespace to have access to collection classes
using System.Collections;

Using Queue Class – Built-in


We start using the Queue and stack both in our Static Main method. First, let us go with Queue.

1) A Queue is created and 5 integers are stored in it. The Queue class’s Enqueue() function is used to add an element at the back of the Q.

//===============> A. Queue  <==================
Console.WriteLine("===============> A. Queue  <==================");
//A_001: Create a Queue and populate it.
Queue Q = new Queue();

//A_002: populate 5 Integers to it. Enqueue adds an element to the Queue and the End.
for (int i=0; i<5; i++)
            Q.Enqueue(i+1);

2) To display all the elements in the Queue a function was written. The function takes "IEnumerable" interface as parameter meaning that the function expects an object that implemented the IEnumerable interface. Then, it walks through the collection object (Stack or Queue in this Example. Say thanks to polymorphism) and displays each element in it.

//001: Display the passed in collection. Note the collection Stack, Queue, Hash all implements IEnumerable
public static void DisplayContent(string collection_name, IEnumerable collection )
{
            Console.Write("Content of {0}: ", collection_name );
            foreach(int item in collection)
                        Console.Write(item + ", " );
            Console.WriteLine();
}

3) The "Peek()" method will return the first item in the Queue. That is; it will get the element first added (One that is there in the Front). However, Peek() method will not remove the item from the Queue. But, the "Dequeue()" will take the item from the front and removes it. The usage of Peek() and Dequeue() is shown in the below Code:

//A_003: Show the Queue Content
DisplayContent("Queue", Q );

//A_004: Return the Object at the begining of the Queue
Console.WriteLine("First element: {0}", Q.Peek());

//A_005: Show the Queue Content.
DisplayContent("Queue", Q );

//A_006: Remove the First two element from the Queue. Note: The first two entries added will be removed
Console.WriteLine("First Removed Element: {0}" , Q.Dequeue());
Console.WriteLine("Second Removed Element: {0}" , Q.Dequeue());

//A_007: Show the Queue Content
DisplayContent("Queue", Q );



Using Stack Class – Built-in


The code you saw above is copy pasted from Queue and changed for Stack. When you add an element it will be added in the Front or Top (Based on how you imagine it. horizontal or vertical). When you remove an item it will be removed from the Front. Hence, the item added last is removed first. The below code demonstrates the usage of Stack:

//===============> B. Stack  <==================
Console.WriteLine("===============> B. Stack  <==================");
//B_001: Create a Stack and populate it.
Stack S = new Stack();

//B_002: populate 5 Integers to it. Push adds an element to the Stack at the front that is top
for (int i=0; i<5; i++)
            S.Push(i+1);

//B_003: Show the Stack Content
DisplayContent("Stack", S );

//B_004: Return the Object at the begining of the Stack
Console.WriteLine("First element: {0}", S.Peek());

//B_005: Show the Stack Content.
DisplayContent("Stack", S );

//B_006: Remove the First two element from the Stack. Note: The Last two entries added will be removed
Console.WriteLine("First Removed Element: {0}" , S.Pop());
Console.WriteLine("Second Removed Element: {0}" , S.Pop());

//B_007: Show the Queue Content
DisplayContent("Stack", S );



Pictorial representation of Stack and Queue used in this Example




Note: The sample is created in an older version (Dot.net 2003) so that everyone can use it. When you open it in higher versions, say yes to convert the solution.

Source Code : Download

No comments:

Post a Comment

Leave your comment(s) here.

Like this site? Tell it to your Firend :)