コード例 #1
0
        int SeachCount(AnnularDoubleLinkedList <T> begin)
        {
            int count = 0;
            AnnularDoubleLinkedList <T> temp = begin;

            if (temp.data.ToString() == "0")
            {
                return(0);
            }
            else
            {
                do
                {
                    count++;
                    if (temp.next != null)
                    {
                        temp = temp.next;
                    }
                    else
                    {
                        break;
                    }
                } while (temp != begin);
            }
            return(count);
        }
コード例 #2
0
        public static AnnularDoubleLinkedList <int> CreateCollection(int n)
        {
            AnnularDoubleLinkedList <int> collection = new AnnularDoubleLinkedList <int>();

            for (int i = 1; i <= n; i++)
            {
                collection.Add(i);
            }
            return(collection);
        }
コード例 #3
0
        public static T Take(int index, AnnularDoubleLinkedList <T> collection)
        {
            AnnularDoubleLinkedList <T> begin = collection;
            AnnularDoubleLinkedList <T> temp  = begin;

            for (int i = 1; i < index; i++)
            {
                temp = temp.next;
            }
            return(temp.data);
        }
コード例 #4
0
        public IEnumerator <T> GetEnumerator()
        {
            AnnularDoubleLinkedList <T> current = this;

            for (int i = 0; i < this.Count; i++)
            {
                yield return(current.data);

                current = current.next;
            }
        }
コード例 #5
0
        public AnnularDoubleLinkedList <T> Remove(int index)
        {
            AnnularDoubleLinkedList <T> begin = this;
            AnnularDoubleLinkedList <T> temp  = begin;

            for (int i = 1; i < index; i++)
            {
                temp = temp.next;
            }
            AnnularDoubleLinkedList <T> temp1 = temp.next;
            AnnularDoubleLinkedList <T> temp2 = temp.previous;

            temp1.previous = temp2;
            temp2.next     = temp1;
            return(begin);
        }
コード例 #6
0
        public int IndexOf(T item)
        {
            int index = -1;
            AnnularDoubleLinkedList <T> begin = this;
            AnnularDoubleLinkedList <T> temp  = begin;

            for (int i = 1; i < begin.Count + 1; i++)
            {
                if (item.ToString() == temp.data.ToString())
                {
                    index = i;
                    break;
                }
                temp = temp.next;
            }
            return(index);
        }
コード例 #7
0
        public AnnularDoubleLinkedList <T> Add(T item)
        {
            AnnularDoubleLinkedList <T> begin = this;

            if (begin.Count == 0)
            {
                begin.data     = item;
                begin.next     = null;
                begin.previous = null;
            }
            else
            {
                if (begin.Count == 1)
                {
                    AnnularDoubleLinkedList <T> temp = new AnnularDoubleLinkedList <T>();
                    temp.data      = item;
                    temp.previous  = begin;
                    temp.next      = begin;
                    begin.next     = temp;
                    begin.previous = temp;
                }
                else
                {
                    AnnularDoubleLinkedList <T> temp = new AnnularDoubleLinkedList <T>(item);
                    temp.next      = begin;
                    begin.previous = temp;
                    AnnularDoubleLinkedList <T> temp1 = begin;
                    for (int i = 1; i < begin.Count; i++)
                    {
                        temp1 = temp1.next;
                    }
                    temp1.next    = temp;
                    temp.previous = temp1;
                }
            }
            return(begin);
        }
コード例 #8
0
        static void Functions(int choice)
        {
            switch (choice)
            {
            case 1:
            {
                Console.Write("Введите количество элементов в коллекции: ");
                int  count = 0;
                bool ok    = false;
                do
                {
                    ok = int.TryParse(Console.ReadLine(), out count);
                    if (!ok || choice <= 0)
                    {
                        Console.Write("Некорректный ввод, попробуйте еще: ");
                        ok = false;
                    }
                } while (!ok);
                MainList = AnnularDoubleLinkedList <int> .CreateCollection(count);

                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Коллекция, содержащая {0} элементов успешно создана", count);
                Console.ResetColor();
                break;
            }

            case 2:
            {
                if (MainList == null)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Коллекция не создана");
                    Console.ResetColor();
                }
                else
                {
                    int element = 0;
                    Console.Write("Введите значение элемента (целое число): ");
                    bool ok = false;
                    do
                    {
                        ok = int.TryParse(Console.ReadLine(), out element);
                        if (!ok)
                        {
                            Console.Write("Некорректный ввод, попробуйте еще: ");
                        }
                    } while (!ok);
                    MainList.Add(element);
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("Элемент {0} успешно добавлен", element);
                    Console.ResetColor();
                }
                break;
            }

            case 3:
            {
                if (MainList == null)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Коллекция не создана");
                    Console.ResetColor();
                }
                else
                {
                    int data = 0;
                    Console.Write("Введите значение элемента для поиска: ");
                    bool ok = false;
                    do
                    {
                        ok = int.TryParse(Console.ReadLine(), out data);
                        if (!ok)
                        {
                            Console.Write("Некорректный ввод, попробуйте еще: ");
                        }
                    } while (!ok);
                    int index = MainList.IndexOf(data);
                    if (index >= 0)
                    {
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.WriteLine("Элемент найден под номером {0}", index);
                        Console.ResetColor();
                    }
                    else
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("Элемент не найден");
                        Console.ResetColor();
                    }
                }
                break;
            }

            case 4:
            {
                if (MainList == null)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Коллекция не создана");
                    Console.ResetColor();
                }
                else
                {
                    int index = 0;
                    Console.Write("Введите номер элемента: ");
                    bool ok = false;
                    do
                    {
                        ok = int.TryParse(Console.ReadLine(), out index);
                        if (!ok || index <= 0)
                        {
                            Console.Write("Некорректный ввод, попробуйте еще: ");
                            ok = false;
                        }
                        if (index > MainList.Count)
                        {
                            Console.ForegroundColor = ConsoleColor.Red;
                            Console.Write("Введенный номер больше количества элементов в коллекции, попробуйте еще: ");
                            Console.ResetColor();
                            ok = false;
                        }
                    } while (!ok);
                    int val = AnnularDoubleLinkedList <int> .Take(index, MainList);

                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("Полученное значение по номеру - {0}", val);
                    Console.ResetColor();
                }
                break;
            }

            case 5:
            {
                if (MainList == null)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Коллекция не создана");
                    Console.ResetColor();
                }
                else
                {
                    int index = 0;
                    Console.Write("Введите номер элемента для удаления: ");
                    bool ok = false;
                    do
                    {
                        ok = int.TryParse(Console.ReadLine(), out index);
                        if (!ok || index <= 0)
                        {
                            Console.Write("Некорректный ввод, попробуйте еще: ");
                            ok = false;
                        }
                        if (index > MainList.Count)
                        {
                            Console.ForegroundColor = ConsoleColor.Red;
                            Console.Write("Введенный номер больше количества элементов в коллекции, попробуйте еще: ");
                            Console.ResetColor();
                            ok = false;
                        }
                    } while (!ok);
                    MainList.Remove(index);
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("Элемент успешно удален");
                    Console.ResetColor();
                }
                break;
            }

            case 6:
            {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Печать коллекции: ");
                Console.ResetColor();
                if (MainList == null)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Коллекция не создана");
                    Console.ResetColor();
                }
                else
                {
                    foreach (int s in MainList)
                    {
                        Console.WriteLine(s);
                    }
                }
                break;
            }
            }
        }
コード例 #9
0
 private AnnularDoubleLinkedList(T item)
 {
     data     = item;
     next     = null;
     previous = null;
 }
コード例 #10
0
 public AnnularDoubleLinkedList()
 {
     data     = default;
     next     = null;
     previous = null;
 }