Beispiel #1
0
        public Node2 <T> GetTActNode(int index) //поиск текущего элемента
        {
            if (index != null)
            {
                Node2 <T> tmpNode = this.Root; //ук-ль на начало списка

                for (int i = 0; i < index; i++)
                {
                    tmpNode = tmpNode.Next;
                }

                return(tmpNode);
            }
            else
            {
                return(null);
            }
        }
Beispiel #2
0
 public T[] Add(T newItem)
 {
     if (newItem != null)
     {
         if (Root == null)
         {
             Root = new Node2 <T>(newItem);
             Count++;
         }
         else
         {
             Node2 <T> end = GetLastNode();//получить ссылку на последний элемент
             end.Next          = new Node2 <T>(newItem);
             end.Next.Previous = end;
             Count++;
         }
     }
     return(ConvertToArr(Root));
 }
Beispiel #3
0
        public T[] Reverse()
        {
            Node2 <T> current = Root;
            Node2 <T> end     = GetLastNode();
            Node2 <T> next    = null;

            while (current != null)
            {
                Node2 <T> tmp = current.Next;
                current.Next     = next;
                next             = current;
                current.Previous = tmp;
                current          = tmp;
            }

            Root = next;

            return(ConvertToArr(Root));
        }
Beispiel #4
0
        public T[] SortToIncr()            //по возрастанию
        {
            Node2 <T> current = Root.Next; //ук-ль на начало списка

            //опт решение 4 вложенных while

            while (current != null)//находим мин эл-т
            {
                while (current != Root && current.Value.CompareTo(current.Previous.Value) < 0)
                {
                    if (current.Previous == Root)
                    {
                        current.Previous.Next = current.Next;
                        current.Next.Previous = current.Previous;
                        Root.Previous         = current;
                        current.Next          = Root;
                        current.Previous      = null;
                        Root = current;
                    }
                    else
                    {
                        Node2 <T> tmp1 = current.Previous;
                        Node2 <T> tmp2 = current.Next;
                        Node2 <T> tmp3 = tmp1.Previous;

                        tmp1.Next = tmp2;
                        if (tmp2 != null)
                        {
                            tmp2.Previous = tmp1;
                        }

                        current.Previous      = tmp1.Previous;
                        current.Next          = current.Previous.Next;
                        current.Previous.Next = current;
                        tmp1.Previous         = current;
                    }
                }
                current = current.Next;
            }
            return(ConvertToArr(Root));
        }
Beispiel #5
0
        public T[] AddIndex(T newItem, int index)
        {
            if (index == 0)
            {
                InsertStart(newItem);
            }
            else
            {
                Node2 <T> t = GetTActNode(index - 1);
                Node2 <T> n = new Node2 <T>(newItem);

                Node2 <T> tmp = t.Next;
                t.Next       = n;
                n.Previous   = t;
                n.Next       = tmp;
                tmp.Previous = n;
                Count++;
            }

            return(ConvertToArr(Root));
        }
Beispiel #6
0
        public T[] Half()
        {
            int       count   = 0;
            Node2 <T> tmpNode = Root;   //ук-ль на начало списка

            while (tmpNode != null)     //проход по списку
            {
                tmpNode = tmpNode.Next; //ук-ль на след эл-т
                count++;                //считает количество элементов
            }

            switch (count % 2)
            {
            case 0:
                HalfEven(count);
                break;

            case 1:
                HalfUneven(count);
                break;
            }

            return(ConvertToArr(Root));
        }
Beispiel #7
0
        public T[] SortToDecr()       //по убыванию
        {
            Node2 <T> current = Root; //ук-ль на начало списка
                                      //опт решение 4 вложенных while
            int count = 0;

            while (current != null)     //проход по списку
            {
                current = current.Next; //ук-ль на след эл-т
                count++;                //считает количество элементов
            }

            for (int i = 0; i < count; i++)
            {
                current = Root;

                for (int j = 0; j < count - i - 1; j++)
                {
                    if (current != null)
                    {
                        if (current.Value.CompareTo(current.Next.Value) < 0)
                        {
                            if (current == Root)
                            {
                                Node2 <T> tmp = current.Next;
                                current.Next          = tmp.Next;
                                current.Next.Previous = current;
                                current.Previous      = tmp;
                                tmp.Next     = current;
                                tmp.Previous = null;
                                Root         = tmp;
                            }
                            else
                            {
                                Node2 <T> tmp1 = current.Next;
                                Node2 <T> tmp2 = current.Previous;
                                current.Next = tmp1.Next;
                                if (tmp1.Next != null)
                                {
                                    current.Previous      = tmp1.Next.Previous;
                                    current.Next.Previous = current;
                                }
                                else if (tmp1.Next == null)
                                {
                                    current.Previous = tmp1;
                                }
                                current.Previous.Next     = current;
                                current.Previous.Previous = tmp2;
                                tmp2.Next = current.Previous;
                            }
                            if (current.Next == null)
                            {
                                break;
                            }
                        }
                        else
                        {
                            current = current.Next;
                        }
                    }
                }
            }
            return(ConvertToArr(Root));
        }
Beispiel #8
0
 public LList2(T rootVal)
 {
     Root = new Node2 <T>(rootVal);
     Count++;
 }
Beispiel #9
0
        public Node2 <T> Next; //ссылка на след узел

        public Node2(T val)
        {
            this.Value = val;
            Next       = null;
            Previous   = null;
        }