Creating our own Generic Stack T Class like Inbuild Stack Class

Introduction

The basic intention of this article is to teach you how to build simple generic classes on your own. Like List<T>, Dictionary <T>, and Stack<T>are inbuilt Generic Classes .in; this article, we will create our own Stack<T> Generic  Class like the one provided in .net . that can Work with Any Data Types like String, Object, Double, etc to store.

Technologies

  • .NETframework 2.0/3.5

Language

  • C#

Prerequisites

Basic Knowledge of Array Operations, Class, Constructor

Implementation of Stack Class

Before creating Stack<T>, we will see what is Stack. Stack is a linear data structure in which we can store/ delete elements from only one end. It can have Tree Operations, basically PUSH, POP, and PEEP.

As a real-life example, if we have so many dishes which are laying on one another, the man will take only the dish which is on top of all dishes. Same as the scenario stack is working in which if we put a new element, that can be on top position only, or if we remove an element, it can be on top only.

Here one variable TOP is maintained .top indicates. The last element in the stack insertion of element and deletion of element can be done with the top position of the stack.

  1. Push Operation: Store the element at the top position of the stack.
  2. POP operation: Return the Element at the top and decrement the top by 1.
  3. PEEP Operation: return the element to a position specified by the user.

So let's start we make out Own Stack<T> Structure. That can store any data type element s in stack Fashion.

Below is a basic outline of what methods we are going to develop in the Stack<T> class.

1.gif

class MyStack<T>
{
    int capacity;
    T[] stack;
    int top;
    public MyStack(int MaxElements)
    {
        capacity = MaxElements;
        stack = new T[capacity]; // Corrected the missing closing square bracket
        top = -1;
    }
    public int Push(T Element)
    {
        if (top == capacity - 1)
        {
            return -1;
        }
        else
        {
            top = top + 1;
            stack[top] = Element;
        }
        return 0;
    }
    public T Pop()
    {
        T RemovedElement;
        T temp = default(T);

        if (top >= 0)
        {
            RemovedElement = stack[top];
            top = top - 1;
            return RemovedElement;
        }
        return temp;
    }
    public T Peep(int position)
    {
        T temp = default(T);

        if (position < capacity && position >= 0)
        {
            return stack[position];
        }
        return temp;
    }
    public T[] GetAllStackElements()
    {
        T[] Elements = new T[top + 1];
        Array.Copy(stack, 0, Elements, 0, top + 1);
        return Elements;
    }
}

Now we can implement our Class MyStack<T> that we have coded in the program, Like below.

static void Main(string[] args)
{
    int capacity;
    Console.WriteLine("Enter Capacity of Stack:");
    capacity = int.Parse(Console.ReadLine());
    MyStack<string> stack = new MyStack<string>(capacity);
    while (true)
    {
        Console.WriteLine("1. Push");
        Console.WriteLine("2. Pop");
        Console.WriteLine("3. Peep");
        Console.WriteLine("4. Print Stack Elements:");
        Console.WriteLine("5. Exit");

        Console.WriteLine("Enter your Choice:");
        int choice = int.Parse(Console.ReadLine());
        switch (choice)
        {
            case 1:
            {
                Console.WriteLine("Enter String to Push:");
                string temp = Console.ReadLine();
                int result = stack.push(temp);
                if (result != -1)
                {
                    Console.WriteLine("Element Pushed into Stack!");
                }
                else
                {
                    Console.WriteLine("Stack Overflow!");
                }
                break;
            }
            case 2:
            {
                string Result = stack.pop();
                if (Result != null)
                {
                    Console.WriteLine("Deleted Element: " + Result);
                }
                else
                {
                    Console.WriteLine("Stack Underflow!");
                }
                break;
            }
            case 3:
            {
                Console.WriteLine("Enter Position of Element to Peep:");
                int Position = int.Parse(Console.ReadLine());
                string Result = stack.peep(Position);

                if (Result != null)
                {
                    Console.WriteLine("Element at Position " + Position + " is " + Result);
                }
                else
                {
                    Console.WriteLine("Entered Position is Out of Stack Range");
                }
                break;
            }
            case 4:
            {
                string[] Elements = stack.GetAllStackElements();
                Console.WriteLine("************** Stack Content **************");
                foreach (string str in Elements)
                {
                    Console.WriteLine(str);
                }
                break;
            }
            case 5:
            {
                Environment.Exit(0);
                break;
            }
            default:
            {
                Console.WriteLine("You have Entered Wrong Choice");
                break;
            }
        }
    }
}

Conclusion

This article explained how to create our own Stack<T> class using Generics.


Similar Articles