Пример #1
0
        //function for all stack operations
        static void StackOperation()
        {
            Stack stack = new Stack();
            int   choiceStack;

            do
            {
                Console.WriteLine();
                Console.WriteLine("Enter 1 to add a value in stack " +
                                  "\n2 to remove value " +
                                  "\n3 to show the stack " +
                                  "\n4 to get top value " +
                                  "\n5 to sort the stack" +
                                  "\n6 to Exit ");
                choiceStack = int.Parse(Console.ReadLine());
                switch (choiceStack)
                {
                case 1:
                    Console.WriteLine("Enter Elements");
                    int data = int.Parse(Console.ReadLine());
                    stack.Add(data);
                    Console.WriteLine("Data Added");
                    break;

                case 2: stack.Remove();
                    Console.WriteLine("Data removed");
                    break;

                case 3:
                    Console.Write("Stack contains :- ");
                    stack.Display();
                    Console.WriteLine();
                    break;

                case 4:
                    int top = stack.GetTopValue();
                    Console.WriteLine("Top of the stack is :- " + top);
                    Console.WriteLine();
                    break;

                case 5:
                    stack.Sort();
                    Console.WriteLine("Stack Sorted");
                    break;

                case 6:
                    break;

                default: Console.WriteLine("Invalid Entry");
                    break;
                }
            }while (choiceStack != 6);
        }
        public void InvokeMethods()
        {
            int choice = 0;

            Console.WriteLine("\t\tStack Operations\n1.Add\n2.Remove\n3.Display\n4.Sort\n5.Top\n6.Exit\nEnter your Choice : ");
            choice = Convert.ToInt32(Console.ReadLine());
            switch (choice)
            {
            case 1:
                Console.WriteLine("Enter Data : ");
                _Element = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine(_Stack.Add(_Element));
                InvokeMethods();
                break;

            case 2:
                Console.WriteLine(_Stack.Remove());
                InvokeMethods();
                break;

            case 3:
                Console.WriteLine(_Stack.Display());
                InvokeMethods();
                break;

            case 4:
                Console.WriteLine(_Stack.Sort());
                InvokeMethods();
                break;

            case 5:
                _Stack.Top();
                InvokeMethods();
                break;

            case 6:
                Environment.Exit(0);
                break;

            default:
                Console.WriteLine("Wrong choice, try again");
                break;
            }
        }
Пример #3
0
        static void Main(string[] args)
        {
            Console.WriteLine(" enter your choice on which operator you want to operate on ");
            Console.WriteLine(" \n 1. linked list \n 2. Stack \n 3. Queue");
            int choiceOfFunction = Convert.ToInt32(Console.ReadLine());
            int i = 1;
            int x = 1;

            while (i == 1)
            {
                switch (choiceOfFunction)
                {
                case 1:     // inside
                    Interface1 objectLinkList = new LinkedList();
                    x = 1;
                    while (x == 1)
                    {
                        Console.WriteLine("enter your choice\n 1.add   2.remove   3.display  4.sort \n");
                        int choice = Convert.ToInt32(Console.ReadLine());
                        switch (choice)
                        {
                        case 1:
                            Console.WriteLine("enter the number to entered");
                            int val = Convert.ToInt32(Console.ReadLine());
                            objectLinkList.AddNewElement(val);
                            break;

                        case 2:
                            objectLinkList.RemoveElement();
                            break;

                        case 3:

                            objectLinkList.Display();
                            break;

                        case 4:
                            objectLinkList.Sort();
                            break;

                        default:

                            break;
                        }
                        Console.WriteLine("enter 1 and 0 to exit to continue working in queue");
                        x = Convert.ToInt32(Console.ReadLine());
                    }
                    break;

                case 2:
                    Interface1 objectStack = new Stack();
                    x = 1;
                    while (x == 1)
                    {
                        Console.WriteLine("enter your choice\n 1.add   2.remove   3.display  4.sort \n");
                        int choice = Convert.ToInt32(Console.ReadLine());
                        switch (choice)
                        {
                        case 1:
                            Console.WriteLine("enter the number to entered");
                            int val = Convert.ToInt32(Console.ReadLine());
                            objectStack.AddNewElement(val);
                            break;

                        case 2:
                            objectStack.RemoveElement();
                            break;

                        case 3:
                            objectStack.Display();
                            break;

                        case 4:
                            objectStack.Sort();
                            break;

                        default:
                            break;
                        }
                        Console.WriteLine("enter 1 to continue working in queue");
                        x = Convert.ToInt32(Console.ReadLine());
                    }

                    break;

                case 3:

                    Interface1 objectQueue = new Queue();

                    Queue Object2Queue = new Queue();
                    x = 1;
                    while (x == 1)
                    {
                        Console.WriteLine("enter your choice\n 1.add   2.remove   3.display  4.sort \n");
                        int choice = Convert.ToInt32(Console.ReadLine());
                        switch (choice)
                        {
                        case 1:
                            Console.WriteLine("enter the val");
                            int val = Convert.ToInt32(Console.ReadLine());
                            objectQueue.AddNewElement(val);
                            break;

                        case 2:
                            objectQueue.RemoveElement();
                            break;

                        case 3:
                            objectQueue.Display();
                            break;

                        case 4:
                            objectQueue.Sort();
                            break;

                        default:
                            break;
                        }
                        Console.WriteLine("enter 1 to continue working in queue");
                        x = Convert.ToInt32(Console.ReadLine());
                    }

                    break;

                default:
                    Console.WriteLine("wrong choice chosen");
                    break;
                }
                Console.WriteLine("enter 1 to continue and 0 to exit");
                i = Convert.ToInt32(Console.ReadLine());
            }
        }
Пример #4
0
        static void Main(string[] args)
        {
            int s = 5, index;

            Queue      queue;
            LinkedList linkList;
            Stack      stack;
            Node       sNode = null, qNode = null, lNode = null;
            char       flag = 'y', ch, c;

            while (flag == 'y')
            {
                Console.WriteLine("Do you want to continue");
                flag = (Console.ReadKey().KeyChar);
                if (flag != 'y')
                {
                    break;
                }

                Console.WriteLine("What operation you want to  perform\n" + "Press 'a' for adding \n" + "Press 'r' for remove \n" + "Press 'd' for display \n" + "Press 'f' to check if queue is full \n" + "Press 'e' to check if queue is empty \n" + "Press 'p' to get queue peek value \n" + "Press 'i' to remove element from specific pos\n" + "Press 'c' to insert at pos in ll  \n" + "Press 'h' to get  stack top value \n");
                c = Console.ReadKey().KeyChar;
                if (c == 'p' || c == 'f' || c == 'e')
                {
                    Console.WriteLine("you can perform only for queues");

                    ch = 'q';
                }
                else if (c == 'h')
                {
                    Console.WriteLine("you can perform only for stack");
                    ch = 's';
                }
                else if (c == 'i' || c == 'c')
                {
                    Console.WriteLine("you can perform only for linkedlist");
                    ch = 'l';
                }
                else
                {
                    Console.WriteLine("What data structure want to work on\n" + "Press 'l' for linkedlist \n" + "Press 'q' for queue \n" + "Press 's' for stack \n");

                    ch = Console.ReadKey().KeyChar;
                }
                switch (c)
                {
                case 'a':

                    Console.WriteLine("Enter element");
                    int n = Int32.Parse(Console.ReadLine());

                    if (ch == 'q')
                    {
                        queue = new Queue();

                        qNode = queue.AddElement(n, qNode);
                    }
                    else if (ch == 's')
                    {
                        stack = new Stack();

                        sNode = stack.AddElement(n, sNode);
                    }
                    else if (ch == 'l')
                    {
                        linkList = new LinkedList();
                        lNode    = linkList.AddElement(n, lNode);
                    }

                    break;

                case 'r':

                    if (ch == 'q')
                    {
                        queue = new Queue();
                        qNode = queue.RemElement(qNode);
                    }

                    else if (ch == 's')
                    {
                        stack = new Stack();
                        sNode = stack.RemElement(sNode);
                    }
                    else if (ch == 'l')
                    {
                        linkList = new LinkedList();
                        lNode    = linkList.RemElement(lNode);
                    }

                    break;

                case 'd':

                    if (ch == 'q')
                    {
                        queue = new Queue();
                        queue.Display(qNode);
                    }

                    else if (ch == 's')
                    {
                        stack = new Stack();
                        stack.Display(sNode);
                    }
                    else if (ch == 'l')
                    {
                        linkList = new LinkedList();
                        linkList.Display(lNode);
                    }
                    break;

                case 's':

                    if (ch == 'q')
                    {
                        queue = new Queue();
                        qNode = queue.Sort(qNode);
                    }

                    else if (ch == 's')
                    {
                        stack = new Stack();
                        sNode = stack.Sort(sNode);
                    }
                    else if (ch == 'l')
                    {
                        linkList = new LinkedList();
                        lNode    = linkList.Sort(lNode);
                    }
                    break;

                case 'f':

                    queue = new Queue();
                    Console.WriteLine(queue.IsFull(qNode, s));
                    break;

                case 'e':

                    queue = new Queue();
                    Console.WriteLine(queue.IsEmpty(qNode));
                    break;

                case 'c':

                    Console.WriteLine("Enter element");
                    n = Int32.Parse(Console.ReadLine());
                    Console.WriteLine("Enter index");
                    index    = Int32.Parse(Console.ReadLine());
                    linkList = new LinkedList();
                    lNode    = linkList.InsertAtPos(n, index, lNode);
                    break;

                case 'i':

                    index    = Int32.Parse(Console.ReadLine());
                    linkList = new LinkedList();
                    lNode    = linkList.RemoveAtPos(index, lNode);
                    break;

                case 'h':

                    stack = new Stack();
                    Console.WriteLine(stack.Peek(sNode));
                    break;

                case 'p':

                    queue = new Queue();
                    Console.WriteLine(queue.Peek(qNode));
                    break;
                }
            }
            Console.ReadLine();
        }