Exemple #1
0
        public void AddAtHead(int val)
        {
            if (Value != -1)
            {
                var oldHead = new MyDoublyLinkedList(Value);

                var next = Next;
                if (next != null)
                {
                    next.Prev = oldHead;
                }

                oldHead.Next = Next;
                oldHead.Prev = this;

                Value = val;
                Next  = oldHead;
                Prev  = null;
            }
            else
            {
                Value = val;
                Prev  = null;
                Next  = null;
            }
        }
Exemple #2
0
        public void AddAtIndex(int index, int val)
        {
            if (index > this.GetLength())
            {
                return;
            }

            if (index == 0)
            {
                AddAtHead(val);
                return;
            }

            var prev    = this.GetNodeAtIndex(index - 1);
            var element = new MyDoublyLinkedList(val)
            {
                Prev = prev
            };
            var next = prev.Next;

            if (next != null)
            {
                next.Prev = element;
            }
            element.Next = prev.Next;
            prev.Next    = element;
        }
        public static IEnumerable <int> ValuesToFlatList(this MyDoublyLinkedList head)
        {
            var result = new List <int>();

            while (head != null)
            {
                result.Add(head.Value);
                head = head.Next;
            }

            return(result);
        }
        public static MyDoublyLinkedList GetTail(this MyDoublyLinkedList head)
        {
            var tail = head;

            while (head != null)
            {
                tail = head;
                head = head.Next;
            }

            return(tail);
        }
        public static int GetLength(this MyDoublyLinkedList head)
        {
            var length = 0;

            while (head != null)
            {
                head = head.Next;
                length++;
            }

            return(length);
        }
Exemple #6
0
        public void AddAtTail(int val)
        {
            var newElement = new MyDoublyLinkedList(val);
            var tail       = this.GetTail();

            if (tail.Value == -1)
            {
                Value = val;
                return;
            }

            tail.Next       = newElement;
            newElement.Prev = tail;
        }
        public static MyDoublyLinkedList GetNodeAtIndex(this MyDoublyLinkedList current, int index)
        {
            if (index < 0)
            {
                return(null);
            }

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

            return(current);
        }