Exemple #1
0
        public static DoublyLinkedList <T> Append <T>(DoublyLinkedList <T> left, DoublyLinkedList <T> right)
        {
            if (left is null)
            {
                ThrowLeftNull();
            }

            if (right is null)
            {
                ThrowRightNull();
            }

            var result = new DoublyLinkedList <T>();

            result.AddLast(left);
            result.AddLast(right);
            return(result);

            void ThrowLeftNull() => throw new ArgumentNullException(nameof(left));
            void ThrowRightNull() => throw new ArgumentNullException(nameof(right));
        }
Exemple #2
0
        public DoublyLinkedList <T> Reverse()
        {
            var list = new DoublyLinkedList <T>
            {
                head    = null,
                tail    = null,
                count   = count,
                version = 0,
            };

            var current = head;

            if (!(current is null))
            {
                list.head = list.tail = new Node
                {
                    List     = list,
                    Value    = current.Value,
                    Next     = null,
                    Previous = null,
                };
                current = current.Next;
                while (!(current is null))
                {
                    var node = new Node
                    {
                        List     = list,
                        Value    = current.Value,
                        Next     = list.head,
                        Previous = null,
                    };
                    list.head.Previous = node;
                    list.head          = node;
                    current            = current.Next;
                }
            }

            return(list);
        }
 internal ForwardEnumeration(DoublyLinkedList <T> list)
 => this.list = list;
Exemple #4
0
 internal ReverseEnumeration(DoublyLinkedList <T> list)
 {
     this.list = list;
 }
Exemple #5
0
        public void AddLastFrom(DoublyLinkedList <T> list, bool reversed = false)
        {
            if (list is null)
            {
                ThrowListNull();
            }

            if (list.Count == 0)
            {
                return;
            }

            Node tempHead = null;
            Node tempTail = null;

            if (reversed)
            {
                AssignReversed();
            }
            else
            {
                Assign();
            }

            if (IsEmpty)
            {
                head = tempHead;
                tail = tempTail;
            }
            else
            {
                tail.Next         = tempHead;
                tempHead.Previous = tail;
                tail = tempTail;
            }
            count += list.count;
            version++;
            list.Invalidate();

            void ThrowListNull() => throw new ArgumentNullException(nameof(list));

            void Assign()
            {
                var current = list.head;

                while (!(current is null))
                {
                    current.List = this;

                    current = current.Next;
                }
                tempHead = list.head;
                tempTail = list.tail;
            }

            void AssignReversed()
            {
                var current = list.head;
                var next    = current.Next;

                current.List     = this;
                current.Next     = null;
                current.Previous = null;
                tempHead         = tempTail = current;

                current = next;
                while (!(current is null))
                {
                    next = current.Next;

                    current.List      = this;
                    current.Next      = tempHead;
                    current.Previous  = null;
                    tempHead.Previous = current;
                    tempHead          = current;

                    current = next;
                }
            }
        }
Exemple #6
0
        public void AddLast(DoublyLinkedList <T> list, bool reversed = false)
        {
            if (list is null)
            {
                ThrowListNull();
            }

            if (list.Count == 0)
            {
                return;
            }

            Node tempHead = null;
            Node tempTail = null;
            var  current  = list.head;

            tempHead = tempTail = new Node
            {
                List     = this,
                Value    = current.Value,
                Next     = null,
                Previous = null,
            };

            if (reversed)
            {
                AssignReversed();
            }
            else
            {
                Assign();
            }

            if (IsEmpty)
            {
                head = tempHead;
                tail = tempTail;
            }
            else
            {
                tail.Next         = tempHead;
                tempHead.Previous = tail;
                tail = tempTail;
            }
            count += list.count;
            version++;

            void ThrowListNull() => throw new ArgumentNullException(nameof(list));

            void Assign()
            {
                current = current.Next;
                while (!(current is null))
                {
                    var node = new Node
                    {
                        List     = this,
                        Value    = current.Value,
                        Next     = null,
                        Previous = tempTail,
                    };
                    tempTail.Next = node;
                    tempTail      = node;

                    current = current.Next;
                }
            }

            void AssignReversed()
            {
                current = current.Next;
                while (!(current is null))
                {
                    var node = new Node
                    {
                        List     = this,
                        Value    = current.Value,
                        Next     = tempHead,
                        Previous = null,
                    };
                    tempHead.Previous = node;
                    tempHead          = node;

                    current = current.Next;
                }
            }
        }