Tuesday, December 15, 2015

C# : Implementation of Stack using C#

How to implement a Stack using C# 


C# is pretty good at supporting various Data Structures and Stack is one of them. Let us first understand the concept of a stack.

Definition

According to Wikipedia,

"In Computer Science a stack or LIFO (last in, first out) is an abstract data type that serves as a collection of elements, with two principal operations: push, which adds an element to the collection, and pop, which removes the last element that was added.

The term LIFO stems from the fact that, using these operations, each element "popped off" a stack in series of pushes and pops is the last (most recent) element that was "pushed into" within the sequence."

The following image illustrates the above concept:


Image Source: wikipedia.org

Simple representation of stack runtime with push and pop operations

Applications of Stack

There are many applications of stack. The most profound use that we mostly encounter is the Function Calls. For example, while executing the code we branch of from a certain instruction, the current instruction and related data is pushed on to the stack. Now the branched instruction and related data is also pushed on to stack. Once the branch instruction is executed, it is popped of the stack. Now we have the instruction from where we left or branched, on top of the stack. Hence Stack are very useful at certain places. Some of the applications are listed below:


  • Parsing
  • Recursive Functions
  • Calling Function
  • Expression Evaluation
  • Expression Conversion 
              1) Infix to Postfix
              2) Infix to Prefix
              3) Postfix to Infix
              4) Prefix to Infix
    • Towers of Hanoi, etc...
    Let us directly jump to the implementation of Stack using C#. Let us follow the following steps to implement the Stack. We are be using Visual Studio Professional 2015 for this.

    Step 1Open Visual Studio Professional 2015


    Step 2Click on New Project. Select Console Application and give desired Name to the application.


    Step 3Click on OK and you will get the following Screen



    Step 4: The overall implementation or the code should like the following.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleStackExample
    {
        class Program
        {
            static void Main(string[] args)
            {
                // Main Method
                // In this example we will see basic implementation of 
                // Data Structure: Stack using the language C#.
    
                //Declaring a Stack of integers
                Stack<int> testStack = new Stack<int>();
    
                //Pushing elements on the stack
                Console.WriteLine("Pushing Elements on the Stack");
                for (int i = 0; i < 10; i++)
                {
                    testStack.Push(i);
                }
                Console.WriteLine("Pushing of Elements on the Stack is finished");
    
                // Popping elements of the stack
                // Please note stack is LIFO (Last In First Out), Hence 9 should be 
                // popped first,then 8, then 7, nd so on ... 
    
                Console.WriteLine("Popping elements of the stack started");
    
                for (int i = 0; i < 10; i++)
                {
                    Console.WriteLine("Popped element is : " + testStack.Pop());
                }
                Console.WriteLine("Popping of Elements from the Stack is finished");
    
                // Waiting for enter to be pressed by user to hold the 
                //   Console Screen from closing automatically.
                Console.ReadLine();
    
            }
        }
    } 


    OutputThe output would look like this:


    Hope you liked the article. Let us know your feedback through comments. Cheers!!

    No comments:

    Post a Comment

    Thanks for your valuable comment