Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            int sizeofStack;

            Console.Write("Enter the size of Stack: ");
            sizeofStack = int.Parse(Console.ReadLine());
            StackClass Stack = new StackClass(sizeofStack);


            // For Push
            for (;;)
            {
                if (!Stack.ISFull())
                {
                    Console.Write("Enter A Value For Stack : ");
                    char Chartoenter = Convert.ToChar(Console.ReadLine());
                    Stack.push(Chartoenter);
                }
                else
                {
                    Console.WriteLine("Stack is Full. Now Fetching Value to empty the Stack ");

                    for (;;)
                    {
                        if (!Stack.ISEmpty())
                        {
                            char value = Stack.pop();
                            Console.WriteLine("Value For Stack :{0}   ", value);
                        }
                        else
                        {
                            break;
                        }
                    }
                    Console.Read();
                    break;
                }
            }
        }
Ejemplo n.º 2
0
        public static void Main(string[] args)
        {
            int size;

            Console.WriteLine("Enter the size of the stack: ");
            size = int.Parse(Console.ReadLine());

            StackClass <object> stck = new StackClass <object>(size);

            while (true)
            {
                Console.WriteLine("1.Push");
                Console.WriteLine("2.Pop");
                Console.WriteLine("3.Search (given position)");
                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 the element to push : ");
                    object temp   = Console.ReadLine();
                    int    result = stck.push(temp);
                    if (result != -1)
                    {
                        Console.WriteLine("Element has been pushed to stack!");
                    }
                    else
                    {
                        Console.WriteLine("Stack overflow!");
                    }
                    break;
                }

                case 2:
                {
                    object result = stck.pop();
                    if (result != null)
                    {
                        Console.WriteLine("Deletd Element : " + result);
                    }
                    else
                    {
                        Console.WriteLine("Stack underflow!");
                    }
                    break;
                }

                case 3:
                {
                    Console.WriteLine("Enter the position of the element to search:");
                    int    position = int.Parse(Console.ReadLine());
                    object result   = stck.peep(position);

                    if (result != null)
                    {
                        Console.WriteLine("Element at Position " + position + "is " + result);
                    }
                    else
                    {
                        Console.WriteLine("Entered element id Out of Stack Range");
                    }
                    break;
                }

                case 4:
                {
                    object[] Elements = stck.GetAllStackElements();
                    Console.WriteLine("*******Stack Content**********");
                    foreach (object obj in Elements)
                    {
                        Console.WriteLine(obj);
                    }
                    break;
                }

                case 5:
                {
                    System.Diagnostics.Process.GetCurrentProcess().Kill();
                    break;
                }

                default:
                {
                    Console.WriteLine("You have Entered wrong choice ");
                    break;
                }
                }
            }
        }