Example #1
0
        static void Main(string[] args)
        {
            CustomLinkedList <string> customLinkedList = new CustomLinkedList <string>("Start");

            customLinkedList.Add("First");
            customLinkedList.Add("Second");
            customLinkedList.Add("Third");
            customLinkedList.Add("Fourth");
            customLinkedList.Add("Fifth");
            customLinkedList.Add("Sixth");
            customLinkedList.Add("Seventh");
            customLinkedList.Add("Eighth");
            customLinkedList.Add("Ninth");

            Console.WriteLine(customLinkedList.Count);

            //Print the data
            for (int i = 0; i < customLinkedList.Count; i++)
            {
                Console.WriteLine(customLinkedList.GetData(i));
            }
            Console.WriteLine("");

            //a.	Try to delete an invalid index (catch the exception and print an error)
            try
            {
                customLinkedList.Remove(1000);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            //b. Delete the last node
            customLinkedList.Remove(customLinkedList.Count - 1);
            //c. Delete the first node
            customLinkedList.Remove(0);
            //d. Delete a node somewhere in the middle
            customLinkedList.Remove(3);

            //Print the data again.
            Console.WriteLine("");
            for (int i = 0; i < customLinkedList.Count; i++)
            {
                Console.WriteLine(customLinkedList.GetData(i));
            }

            Console.ReadLine();
        }
Example #2
0
        static void Main(string[] args)
        {
            CustomLinkedList <string> customLinkedList = new CustomLinkedList <string>();

            bool   running = true;
            Random rnd     = new Random();
            int    index;
            string input;

            while (running)
            {
                Console.Write("Comands are quit, print, count, clear, remove, reverse, and scramble.\n" +
                              "Type a word to add it to the list\n" +
                              "Type input here: ");
                input = Console.ReadLine();

                switch (input)
                {
                case "quit":
                    running = false;
                    break;

                case "print":
                    Console.WriteLine("The following items are currently in the list:");
                    for (int i = 0; i < customLinkedList.Count; i++)
                    {
                        Console.WriteLine(customLinkedList[i]);
                    }
                    break;

                case "count":
                    Console.WriteLine(customLinkedList.Count + " items in the Linked List");
                    break;

                case "clear":
                    customLinkedList.Clear();
                    break;

                case "remove":
                    index = rnd.Next(customLinkedList.Count);
                    Console.WriteLine("Removed \"" + customLinkedList.RemoveAt(index) + "\" from the list");
                    break;

                case "reverse":
                    Console.WriteLine("The following items are in the list (in reverse order):");
                    customLinkedList.PrintReversed();
                    break;

                case "scramble":
                    //Removes node at a random index and stores the data
                    index = rnd.Next(customLinkedList.Count);
                    string data = customLinkedList.RemoveAt(index);

                    //Inserts the data as a new node at the new random index
                    index = rnd.Next(customLinkedList.Count);
                    customLinkedList.Insert(data, index);
                    Console.WriteLine("A random element has been moved to a new position");
                    break;

                case "":
                    Console.WriteLine("Please enter something");
                    break;

                default:
                    customLinkedList.Add(input);
                    Console.WriteLine("\"" + input + "\" has been added to the list");
                    break;
                }
                Console.WriteLine("");
            }
        }