Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            var presidents = new LinkedList <string>();

            presidents.AddLast("JFK");
            presidents.AddLast("Lyndon B Johnson");
            presidents.AddLast("Richard Nixon");
            presidents.AddLast("Jimmy Carter");

            presidents.RemoveFirst();
            //presidents.AddFirst("John F Kennedy");                                    //This is perfectly valid
            LinkedListNode <string> kennedy = presidents.AddFirst("John F Kennedy"); //This is even better as we already specified the node thus no need to search for it in a future

            LinkedListNode <string> nixon = presidents.Find("Richard Nixon");        // This is how you specify the node

            presidents.AddAfter(nixon, "Gerald Ford");                               // This presidents.AddAfter("Richard Nixon", "Gerald Ford"); wont work as you need to specify the node
                                                                                     /* Find method in finding the node is not ver efficient as it needs to look up the whole list */

            foreach (var president in presidents)
            {
                Console.WriteLine(president);
            }

            //======================================        STACK        ==========================================

            /* Stack is a group of entries stack on top of each other.
             * It is not indexed thus it needs to be removed one by one starting from the top of the stack.
             * LAST-in First-out */

            Console.WriteLine("\n\t=====================      STACK       ===========================");
            Stack <string> books = new Stack <string>();

            /* Notice that books are displayed in a reverse order.
             * They are in the order they will be taken away from the stack */

            books.Push("Programming WPF");                          // Push adds the item on top of the stack
            books.Push("The Philosophy book");
            books.Push("Heat and Thermodynamics");
            books.Push("Harry Potter");

            Console.WriteLine(" All Books: ");
            foreach (var book in books)
            {
                Console.WriteLine(book);
            }

            string peek = books.Peek();                             // Peek just peeking on the items on a stack

            Console.WriteLine("\r\tThe item peeked is :" + peek);

            string topItem = books.Pop();                           // Pop removes the item from the top of the stack

            Console.WriteLine("\tTop item which was :" + topItem);
            Console.WriteLine(" All Books: ");
            foreach (var book in books)
            {
                Console.WriteLine(book);
            }

            //======================================        Queue        ==========================================

            /* Queue is essentially the same as stack just in an opposite way.
             * First-in First-out from the stack */

            // it is particuarly useful for tasks such as on a server.

            Console.WriteLine("\n\t=====================      QUEUE       ===========================");
            Queue <string> tasks = new Queue <string>();

            tasks.Enqueue("Buy washing powder");                // Enqueue adds the item to the end of the list
            tasks.Enqueue("Get the post");
            tasks.Enqueue("Play a game");
            tasks.Enqueue("Kiss your wife");

            Console.WriteLine(" All tasks: ");
            foreach (var task in tasks)
            {
                Console.WriteLine(task);
            }

            string peekTask = tasks.Peek();                     //  Peek just peeking on the items on a Queue

            Console.WriteLine("\tThe task that was peeked is : " + peekTask);

            string deleteTask = tasks.Dequeue();                // Dequeue removes the item from the beginning of the list

            Console.WriteLine("\tThe item that was dequeued was : " + deleteTask);

            Console.WriteLine(" All tasks: ");
            foreach (var task in tasks)
            {
                Console.WriteLine(task);
            }
        }
        public static void Main(string[] args)
        {
            string menuSelection = "", newNode = "", existingNode = "";

            LinkedList ll = new LinkedList(new Node("zzz"));

            do
            {
                newNode = "";

                PrintMainMenu();
                menuSelection = Console.ReadLine();
                Console.Clear();

                switch (menuSelection)
                {
                case "1":
                    Console.WriteLine("What value you like the new Node to contain?");
                    newNode = Console.ReadLine();

                    if (newNode != "")
                    {
                        ll.AddNode(new Node(newNode));
                    }
                    else
                    {
                        Console.Clear();
                        Console.WriteLine("Sorry, you must input a value for the Node. Please try again.");
                    }

                    Console.Write("\nPress any key to return to main menu...");
                    Console.ReadKey();
                    Console.Clear();
                    break;

                case "2":
                    Console.WriteLine("What value you like the new Node to contain?");
                    newNode = Console.ReadLine();
                    Console.Clear();
                    Console.WriteLine("What is the value of the existing Node to insert the new Node before?");
                    existingNode = Console.ReadLine();

                    if (newNode != "" && existingNode != "")
                    {
                        ll.AddBefore(new Node(newNode), new Node(existingNode));
                    }
                    else
                    {
                        Console.Clear();
                        Console.WriteLine("Sorry, you must input a value for both Nodes. Please try again.");
                    }

                    Console.Write("\nPress any key to return to main menu...");
                    Console.ReadKey();
                    Console.Clear();
                    break;

                case "3":
                    Console.WriteLine("What value you like the new Node to contain?");
                    newNode = Console.ReadLine();
                    Console.Clear();
                    Console.WriteLine("What is the value of the existing Node to insert the new Node after?");
                    existingNode = Console.ReadLine();

                    if (newNode != "" && existingNode != "")
                    {
                        ll.AddAfter(new Node(newNode), new Node(existingNode));
                    }
                    else
                    {
                        Console.Clear();
                        Console.WriteLine("Sorry, you must input a value for both Nodes. Please try again.");
                    }

                    Console.Write("\nPress any key to return to main menu...");
                    Console.ReadKey();
                    Console.Clear();
                    break;

                case "4":
                    Console.WriteLine("What value you like the new Node to contain?");
                    newNode = Console.ReadLine();

                    if (newNode != "")
                    {
                        ll.AddLast(new Node(newNode));
                    }
                    else
                    {
                        Console.Clear();
                        Console.WriteLine("Sorry, you must input a value for the Node. Please try again.");
                    }

                    Console.Write("\nPress any key to return to main menu...");
                    Console.ReadKey();
                    Console.Clear();
                    break;

                case "5":
                    Console.WriteLine("What is the value of the Node you would like to find?");
                    existingNode = Console.ReadLine();

                    if (existingNode != "")
                    {
                        Node checkNode = ll.Find(existingNode);
                        if (checkNode != null)
                        {
                            Console.WriteLine("\nFound!");
                        }
                        else
                        {
                            Console.WriteLine("\nThat Node doesn't exist.");
                        }
                    }
                    else
                    {
                        Console.Clear();
                        Console.WriteLine("Sorry, you must input a value for the Node. Please try again.");
                    }

                    Console.Write("\nPress any key to return to main menu...");
                    Console.ReadKey();
                    Console.Clear();
                    break;

                case "6":
                    ll.PrintNodes();

                    Console.Write("\nPress any key to return to main menu...");
                    Console.ReadKey();
                    Console.Clear();
                    break;

                case "7":
                    Environment.Exit(0);
                    break;

                default:
                    Console.WriteLine("That did not match one of the menu options. Try again.\n");
                    break;
                }
            } while (menuSelection != "7");
        }