Exemple #1
0
 public int this[int index]
 {
     get
     {
         DoubleLinkedNode current = GetDoubleNodeByIndex(index);
         return(current.Value);
     }
     set
     {
         DoubleLinkedNode current = GetDoubleNodeByIndex(index);
         current.Value = value;
     }
 }
Exemple #2
0
        public void PushFront(T toPush)
        {
            if (first == null)
            {
                first = new DoubleLinkedNode <T>(toPush);
                last  = first;
                ++count;
                return;
            }

            first.LinkToPrev(new DoubleLinkedNode <T>(toPush));
            first = first.GetPrev();
            ++count;
        }
Exemple #3
0
        public void PushBack(T toPush)
        {
            if (last == null)
            {
                first = new DoubleLinkedNode <T>(toPush);
                last  = first;
                ++count;
                return;
            }

            last.LinkToNext(new DoubleLinkedNode <T>(toPush));
            last = last.GetNext();
            ++count;
        }
Exemple #4
0
        private DoubleLinkedNode GetCurrentNodeAndCheckPosition(int index, DoubleLinkedNode preIndex)
        {
            DoubleLinkedNode curIndex;

            if (index == 0)
            {
                curIndex = _head;
            }
            else
            {
                curIndex = preIndex.Next;
            }

            return(curIndex);
        }
Exemple #5
0
        public void Add(int value)
        {
            if (Length == 0)
            {
                _root = new DoubleLinkedNode(value);
                _tail = _root;
            }
            else
            {
                _tail.Next          = new DoubleLinkedNode(value);
                _tail.Next.Previous = _tail;
                _tail = _tail.Next;
            }

            ++Length;
        }
Exemple #6
0
        public override bool Equals(object obj)
        {
            DoubleLinkedList list = (DoubleLinkedList)obj;

            if (this.Length != list.Length)
            {
                return(false);
            }
            DoubleLinkedNode currentThis = _head;
            DoubleLinkedNode currentList = list._head;

            if (currentThis.Value != currentList.Value)
            {
                return(false);
            }

            while (!(currentThis.Next is null))
            {
                currentList = currentList.Next;
                currentThis = currentThis.Next;
                if (currentThis.Value != currentList.Value)
                {
                    return(false);
                }
            }

            currentThis = _tail;
            currentList = list._tail;

            if (currentThis.Value != currentList.Value)
            {
                return(false);
            }

            while (!(currentThis.Previous is null))
            {
                currentList = currentList.Previous;
                currentThis = currentThis.Previous;
                if (currentThis.Value != currentList.Value)
                {
                    return(false);
                }
            }


            return(true);
        }
Exemple #7
0
        public void AddByIndex(int index, int value)
        {
            CheckIndex(index);
            DoubleLinkedNode tmp;
            DoubleLinkedNode NodeBeforeIndex;
            DoubleLinkedNode current;

            if (Length == 0)
            {
                CreateDoubleLinkedListWithOneElment(value);
            }
            else if (index == 0)
            {
                tmp     = new DoubleLinkedNode(value);
                current = GetDoubleNodeByIndex(index);

                current.Previous = tmp;
                tmp.Next         = current;
                _head            = tmp;
                Length++;
            }
            else if (index == Length)
            {
                tmp = new DoubleLinkedNode(value);
                Length++;
                current      = GetDoubleNodeByIndex(index);
                current.Next = tmp;
                tmp.Previous = current;
                _tail        = tmp;
            }



            else
            {
                tmp             = new DoubleLinkedNode(value);
                NodeBeforeIndex = GetDoubleNodeByIndex(index - 1);
                current         = GetDoubleNodeByIndex(index);

                NodeBeforeIndex.Next = tmp;
                tmp.Next             = current;
                current.Previous     = tmp;
                tmp.Previous         = NodeBeforeIndex;
                Length++;
            }
        }
Exemple #8
0
        public override string ToString()
        {
            string s = "";

            if (Length != 0)
            {
                DoubleLinkedNode current = _head;
                s += current.Value;
                while (!(current.Next is null))
                {
                    current = current.Next;
                    s      += " " + current.Value;
                }
                return(s);
            }
            return(s);
        }
Exemple #9
0
        public void AddFirst(int value)
        {
            if (Length != 0)
            {
                DoubleLinkedNode newNode = new DoubleLinkedNode(value);
                _root.Previous = newNode;
                newNode.Next   = _root;
                _root          = newNode;
            }
            else
            {
                _root = new DoubleLinkedNode(value);
                _tail = _root;
            }

            ++Length;
        }
Exemple #10
0
        public void AddList(IList list)
        {
            if (list is null)
            {
                throw new ArgumentNullException();
            }

            DoubleLinkedList linkedList = (DoubleLinkedList)list;

            if ((!(linkedList._root is null)) && (!(_root is null)))
            {
                Length = Length + linkedList.Length;

                DoubleLinkedNode tmp = _tail;
                tmp.Next = linkedList._root;
                linkedList._root.Previous = tmp;
                _tail = linkedList._tail;
            }
Exemple #11
0
        public int FirstIndexByValue(int value)
        {
            int index = 0;

            DoubleLinkedNode current = _head;

            for (int i = 1; i < Length; i++)
            {
                current = current.Next;
                if (current.Value == value)
                {
                    index = i;
                    break;
                }
            }


            return(index);
        }
Exemple #12
0
        public void Reverse()
        {
            DoubleLinkedNode current = _head;

            DoubleLinkedNode tmp = null;

            while (!(current is null))
            {
                tmp = current.Previous;
                current.Previous = current.Next;
                current.Next     = tmp;
                current          = current.Previous;
            }
            if (!(tmp is null))
            {
                _tail = GetDoubleNodeByIndex(0);
                _head = tmp.Previous;
            }
        }
Exemple #13
0
        public void Reverse()
        {
            DoubleLinkedNode current = _root;
            DoubleLinkedNode tmp     = _tail;
            int value;
            int count = 0;

            while (count != Length / 2)
            {
                value         = current.Value;
                current.Value = tmp.Value;
                tmp.Value     = value;

                current = current.Next;
                tmp     = tmp.Previous;

                ++count;
            }
        }
Exemple #14
0
        public void AddDoubleLinkedListByIndex(int index, DoubleLinkedList insertList)
        {
            CheckIndex(index);
            DoubleLinkedList copyInsertList = CopyList(insertList);
            DoubleLinkedNode currentCopy    = copyInsertList._head;


            DoubleLinkedNode preIndex = GetPreNodeAndCheckIndexPosition(index);
            DoubleLinkedNode curIndex;

            curIndex = GetCurrentNodeAndCheckPosition(index, preIndex);

            if (index == 0)
            {
                copyInsertList._tail.Next = curIndex;
                curIndex.Previous         = copyInsertList._tail;
                _head = copyInsertList._head;
            }
            else if (index == Length)
            {
                curIndex.Next = currentCopy;

                currentCopy.Previous = curIndex;
                _tail = copyInsertList._tail;
            }
            else if (index == Length - 1)
            {
                preIndex.Next                 = copyInsertList._head;
                copyInsertList._tail.Next     = _tail;
                copyInsertList._head.Previous = preIndex;
                _tail.Previous                = copyInsertList._tail;
            }
            else
            {
                preIndex.Next                 = copyInsertList._head;
                copyInsertList._tail.Next     = curIndex;
                copyInsertList._head.Previous = preIndex;
                curIndex.Previous             = copyInsertList._tail;
            }

            this.Length += copyInsertList.Length;
        }
Exemple #15
0
        public (int, int) MaxValue()
        {
            int indexOfMaxValue       = 0;
            DoubleLinkedNode current  = _head;
            DoubleLinkedNode maxValue = _head;


            for (int i = 1; i < Length; i++)
            {
                if (maxValue.Value < current.Next.Value)
                {
                    maxValue        = current.Next;
                    indexOfMaxValue = i;
                }
                current = current.Next;
            }
            int maxV = maxValue.Value;

            return(maxV, indexOfMaxValue);
        }
Exemple #16
0
        public int GetFirstIndexByValue(int value)
        {
            int index = 0;

            while (index < Length)
            {
                DoubleLinkedNode current = GetNodeByIndex(index);

                if (current.Value == value)
                {
                    return(index);
                }
                else
                {
                    ++index;
                }
            }

            return(-1);
        }
Exemple #17
0
        public (int, int) MinValue()
        {
            DoubleLinkedNode current  = _head;
            DoubleLinkedNode minValue = _head;
            int indexOfMinValue       = 0;
            int min = 0;

            for (int i = 1; i < Length; i++)
            {
                if (minValue.Value > current.Next.Value)
                {
                    minValue        = current.Next;
                    indexOfMinValue = i;
                    min             = minValue.Value;
                }
                current = current.Next;
            }

            return(min, indexOfMinValue);
        }
Exemple #18
0
        public void RemoveByIndex(int index)
        {
            CheckIndex(index);

            if (index == 0)
            {
                RemoveFirstElement();
            }
            else if (index == Length - 1)
            {
                RemoveLastElement();
            }
            else
            {
                DoubleLinkedNode current    = GetDoubleNodeByIndex(index);
                DoubleLinkedNode currentPre = GetDoubleNodeByIndex(index - 1);
                currentPre.Next       = current.Next;
                current.Next.Previous = currentPre;
                Length--;
            }
        }
Exemple #19
0
        public DoubleLinkedList(int[] values)
        {
            _head = new DoubleLinkedNode(values[0]);
            _tail = _head;

            if (values.Length != 0)
            {
                Length = values.Length;

                for (int i = 1; i < values.Length; i++)
                {
                    _tail.Next          = new DoubleLinkedNode(values[i]);
                    _tail.Next.Previous = _tail;
                    _tail = _tail.Next;
                }
            }
            else
            {
                CreateEmptyDoubleLinkedList();
            }
        }
Exemple #20
0
        private DoubleLinkedList(int[] values)
        {
            Length = values.Length;

            if (values.Length != 0)
            {
                _root = new DoubleLinkedNode(values[0]);
                _tail = _root;

                for (int i = 1; i < values.Length; i++)
                {
                    _tail.Next          = new DoubleLinkedNode(values[i]);
                    _tail.Next.Previous = _tail;
                    _tail = _tail.Next;
                }
            }
            else
            {
                _root = null;
                _tail = null;
            }
        }
Exemple #21
0
        public void RemoveFirstNElements(int numberOfElements)
        {
            if (Length == 0)
            {
                throw new InvalidOperationException("The list is empty");
            }

            if (Length > numberOfElements)
            {
                DoubleLinkedNode tmp = GetNodeByIndex(numberOfElements);

                _root          = tmp;
                _root.Previous = null;

                Length = Length - numberOfElements;
            }
            else
            {
                Length = 0;
                _root  = null;
            }
        }
Exemple #22
0
        public int MinValueIndex()
        {
            if (Length == 0)
            {
                throw new InvalidOperationException("The list is empty");
            }
            else
            {
                int minIndex = 0;

                for (int i = 1; i < Length; i++)
                {
                    DoubleLinkedNode current = GetNodeByIndex(minIndex);
                    DoubleLinkedNode next    = GetNodeByIndex(i);

                    if (current.Value > next.Value)
                    {
                        minIndex = i;
                    }
                }

                return(minIndex);
            }
        }
Exemple #23
0
        public int this[int index]
        {
            get
            {
                if (index < 0 || index >= Length)
                {
                    throw new IndexOutOfRangeException();
                }

                DoubleLinkedNode current = _root;

                for (int i = 1; i <= index; i++)
                {
                    current = current.Next;
                }

                return(current.Value);
            }

            set
            {
                if (index < 0 || index >= Length)
                {
                    throw new IndexOutOfRangeException();
                }

                DoubleLinkedNode current = _root;

                for (int i = 1; i <= index; i++)
                {
                    current = current.Next;
                }

                current.Value = value;
            }
        }
Exemple #24
0
        public void Remove()
        {
            if (Length == 0)
            {
                throw new InvalidOperationException("The list is empty");
            }

            if (Length == 1)
            {
                _root = null;
            }
            else
            {
                int indexOfNodeBeforeTail = Length - 2;

                DoubleLinkedNode tmp = GetNodeByIndex(indexOfNodeBeforeTail);

                tmp.Next = null;

                _tail = tmp;
            }

            --Length;
        }
Exemple #25
0
        public void AddAtIndex(int value, int index)
        {
            if (index < 0 || index >= Length)
            {
                throw new IndexOutOfRangeException();
            }

            if (index == 0)
            {
                AddFirst(value);
            }
            else
            {
                DoubleLinkedNode newNode = new DoubleLinkedNode(value);
                DoubleLinkedNode tmp     = GetNodeByIndex(index - 1);

                newNode.Next      = tmp.Next;
                newNode.Previous  = tmp;
                tmp.Next.Previous = newNode;
                tmp.Next          = newNode;

                ++Length;
            }
        }
Exemple #26
0
 public void Reset()
 {
     iterator = start;
 }
Exemple #27
0
 public DoubleLinkedList(int value)
 {
     Length = 1;
     _root  = new DoubleLinkedNode(value);
     _tail  = _root;
 }
Exemple #28
0
 public void Clear()
 {
     first = last = null;
     count = 0;
 }
Exemple #29
0
 public LinkedEnumerator(DoubleLinkedNode <T> startNode)
 {
     start = startNode;
 }
Exemple #30
0
 public void Dispose()
 {
     iterator = null;
 }