Esempio n. 1
0
        static void Main(string[] args)

        {
            LinkedList rehberList = new LinkedList(new Person("Gökhan", "Gökalp", "05554443322", null));

            // Yeni eleman eklenebileceğinden bahsetmiştik şimdi düğüm ekleme mantığına bir bakalım:

            // NextNode parametresini null bıraktığımız için sona ekleyecektir.

            Person secondNode = new Person("Ramazan", "Gökalp", "04443332211", rehberList.FirstNode);

            rehberList.AddNode(secondNode);

            Person thirdNode = new Person("Salih", "Gökalp", "03332221100", secondNode);

            rehberList.AddNode(thirdNode);

            while (rehberList.NextNode())

            {
                if (rehberList.CurrentNode.FirstName == "Ramazan")

                {
                    // Bu sayede 2. elemanımız olan Ramazan kişisinden sonra araya Kezban kişisini eklemiş oluyoruz.

                    Person fifthNode = new Person("Kezban", "Ilhan", "07773334422", rehberList.CurrentNode);

                    rehberList.AddNode(fifthNode);
                }
            }
            rehberList.WriteList();
            Console.ReadKey();
        }
        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");
        }