Coding Lab - TechOnTechnology

Program in C# to implement Stack operations.

using System;
using System.Collections;

namespace Lab_Ex_5
{
    class cstack
    {
        int index;
        ArrayList list;
        public cstack()
        {
            index = -1;
            list = new ArrayList();
        }

        public void push()
        {
            Console.Write("\n Enter an item to push: ");
            string str = Console.ReadLine();
            list.Add(str);
            index++;
            Console.WriteLine("\n {0} is pushed onto the Stack.", str);
        }
        public void pop()
        {
            if (index == -1)
                Console.WriteLine("\n Sorry ! Stack is empty.");
            else
            {
                object obj = list[index];
                list.RemoveAt(index);
                index--;
                Console.WriteLine("\n Poped item is {0}", obj);
            }
        }
        public void status()
        {
            if (index == -1)
                Console.WriteLine("\n Sorry ! Stack is empty.");
            else
            {
                Console.WriteLine("\n Contents of the STACK: \n");
                foreach (object obj in list)
                    Console.WriteLine(obj);
            }

        }
        public void clear()
        {
            if (index == -1)
                Console.WriteLine("\n Sorry ! Stack is empty.");
            else
            {
                list.Clear();
                index = -1;
                Console.WriteLine("\n The stack is emptied. ");
            }
        }
    }

    class StackProgram
    {
        static void Main(string[] args)
        {
            cstack st = new cstack();
            int i=1, ch;
            while(i==1)
            {
                Console.Clear();
                Console.WriteLine("\n ********************************");
                Console.WriteLine("\n Welcome to STACK Operations.");
                Console.WriteLine("\n ********************************");
                Console.WriteLine("\n 1-----> PUSH");
                Console.WriteLine("\n 2-----> POP");
                Console.WriteLine("\n 3-----> STATUS");
                Console.WriteLine("\n 4-----> CLEAR");
                Console.WriteLine("\n 5-----> EXIT");
                Console.WriteLine("\n ********************************");
                Console.Write("\n\n Enter your choice: ");
                ch = int.Parse(Console.ReadLine());

                switch (ch)
                {
                    case 1: st.push();
                            break;
                    case 2: st.pop();
                            break;
                    case 3: st.status();
                            break;
                    case 4: st.clear();
                            break;
                    case 5: Environment.Exit(-1);
                            break;
                    default: Console.WriteLine(" Sorry !!! Wrong  choice.");
                            break;
                }
                Console.Write("\n Press ENTER to Continue: ");
                Console.ReadLine();
            }
            Console.WriteLine("\n End of STACK operations... Bye");
        }
    }
}

Name

Email *

Message *