Exemplo n.º 1
0
        static void Main(string[] args)
        {
            int choice,
                dataType = ListElementTypeChoice();

            GenericLinkedList <int>    intList    = new GenericLinkedList <int>();
            GenericLinkedList <double> doubleList = new GenericLinkedList <double>();
            GenericLinkedList <string> stringList = new GenericLinkedList <string>();

            switch (dataType)
            {
            case 1:
                intList.CreateList();
                break;

            case 2:
                doubleList.CreateList();
                break;

            case 3:
                stringList.CreateList();
                break;
            }

            while (true)
            {
                choice = GetMenuChoice();

                if (choice is 12)
                {
                    return;
                }

                switch (dataType)
                {
                case 1:
                    AppOperations(intList, choice);
                    break;

                case 2:
                    AppOperations(doubleList, choice);
                    break;

                case 3:
                    AppOperations(stringList, choice);
                    break;
                }

                WriteLine();
            }
        }
Exemplo n.º 2
0
        static void AppOperations <T>(GenericLinkedList <T> aList, int choice)
        {
            int    position;
            T      data;
            object holder = null;

            switch (choice)
            {
            case 1:
                aList.Display();
                break;

            case 2:
                Write("Enter the value of the node to be inserted: ");
                do
                {
                    try
                    {
                        holder = Convert.ChangeType(ReadLine(), typeof(T));
                    }
                    catch (Exception)
                    {
                        WriteLine($"Value entered must be {typeof(T).Name}.\n");
                        Write("Enter the value of the node to be inserted: ");
                    }
                } while (holder is null);

                data = (T)holder;
                aList.InsertAtBeginning(data);
                break;

            case 3:
                Write("Enter the value of the node to be inserted: ");
                do
                {
                    try
                    {
                        holder = Convert.ChangeType(ReadLine(), typeof(T));
                    }
                    catch (Exception)
                    {
                        WriteLine($"Value entered must be {typeof(T).Name}.\n");
                        Write("Enter the value of the node to be inserted: ");
                    }
                } while (holder is null);

                data = (T)holder;
                aList.InsertAtEnd(data);
                break;

            case 4:
                Write("Enter the value of the node to be inserted: ");
                do
                {
                    try
                    {
                        holder = Convert.ChangeType(ReadLine(), typeof(T));
                    }
                    catch (Exception)
                    {
                        WriteLine($"Value entered must be {typeof(T).Name}.\n");
                        Write("Enter the value of the node to be inserted: ");
                    }
                } while (holder is null);

                data = (T)holder;

                Write("Enter the position where the node is to be inserted at: ");
                while (!int.TryParse(ReadLine(), out position))
                {
                    WriteLine("Value entered must be an integer.\n");
                    Write("Enter the position where the node is to be inserted at: ");
                }

                aList.InsertAtPosition(data, position);
                break;

            case 5:
                try
                {
                    data = aList.DeleteFirstNode();
                    WriteLine($"Value of the node deleted was {data}.");
                }
                catch (InvalidOperationException ex)
                {
                    WriteLine(ex.Message);
                }
                break;

            case 6:
                try
                {
                    data = aList.DeleteLastNode();
                    WriteLine($"Value of the node deleted was {data}.");
                }
                catch (InvalidOperationException ex)
                {
                    WriteLine(ex.Message);
                }
                break;

            case 7:
                try
                {
                    Write("Enter the position where the node to be deleted is at: ");
                    while (!int.TryParse(ReadLine(), out position))
                    {
                        WriteLine("Value entered must be an integer.\n");
                        Write("Enter the position where the node to be deleted is at: ");
                    }

                    data = aList.DeleteNodeAtPosition(position);
                    WriteLine($"Value of the node deleted was {data}.");
                }
                catch (InvalidOperationException ex)
                {
                    WriteLine(ex.Message);
                }
                break;

            case 8:
                aList.ReverseList();
                break;

            case 9:
                Write("Enter the value where a cycle should be inserted: ");

                do
                {
                    try
                    {
                        holder = Convert.ChangeType(ReadLine(), typeof(T));
                    }
                    catch (Exception)
                    {
                        WriteLine($"Value entered must be {typeof(T).Name}.\n");
                        Write("Enter the value of the node to be inserted: ");
                    }
                } while (holder is null);

                data = (T)holder;
                aList.InsertCycle(data);
                break;

            case 10:
                if (aList.HasCycle())
                {
                    WriteLine("The current list has a cycle.\n");
                }
                else
                {
                    WriteLine("The current list doesn\'t have a cycle.\n");
                }
                break;

            case 11:
                aList.RemoveCycle();
                break;
            }

            WriteLine();
        }