Exemplo n.º 1
0
        /** Delete the index-th node in the linked list, if the index is valid. */
        public void DeleteAtIndex(int index)
        {
            if (index == 0)
            {
                if (Head.Next == null)
                {
                    Head = null;
                    Tail = null;
                }
                else
                {
                    Head      = Head.Next;
                    Head.Prev = null;
                }
                return;
            }

            var i    = 0;
            var curr = Head;

            while (true)
            {
                if (i == index - 1)
                {
                    if (curr.Next == null)
                    {
                        return;
                    }
                    else
                    {
                        var after = curr.Next.Next;

                        if (after == null)
                        {
                            curr.Next = null;
                            Tail      = curr;
                        }
                        else
                        {
                            after.Prev = curr;
                            curr.Next  = after;
                        }
                    }
                    return;
                }
                else if (curr.Next == null)
                {
                    return;
                }
                else
                {
                    curr = curr.Next;
                    i++;
                }
            }
        }
Exemplo n.º 2
0
        /** Append a node of value val to the last element of the linked list. */
        public void AddAtTail(int val)
        {
            var node = new DoubleListNode(val, Tail, null);

            if (node.Prev != null)
            {
                Tail.Next = node;
                Tail      = node;
            }
            else
            {
                Tail = node;
                Head = Tail;
            }
        }
Exemplo n.º 3
0
        /** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */
        public void AddAtHead(int val)
        {
            var node = new DoubleListNode(val, null, Head);

            if (Head != null)
            {
                Head.Prev = node;
                Head      = node;
            }
            else
            {
                Head = node;
                Tail = Head;
            }
        }
Exemplo n.º 4
0
        /** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
        public void AddAtIndex(int index, int val)
        {
            if (index == 0)
            {
                AddAtHead(val);
                return;
            }

            var i    = 0;
            var curr = Head;

            while (true)
            {
                if (i == index - 1)
                {
                    if (curr.Next == null)
                    {
                        AddAtTail(val);
                        return;
                    }
                    else
                    {
                        var node = new DoubleListNode(val, curr, curr.Next);
                        curr.Next.Prev = node;
                        curr.Next      = node;
                        return;
                    }
                }
                else if (curr == null)
                {
                    throw new IndexOutOfRangeException($"Cannot add node with value {val} at index {index}.");
                }
                else
                {
                    curr = curr.Next;
                    i++;
                }
            }
        }
Exemplo n.º 5
0
 public MyLinkedList(DoubleListNode head = null, DoubleListNode tail = null)
 {
     Head = head;
     Tail = tail == null ? head : tail;
 }
Exemplo n.º 6
0
 public DoubleListNode(int value, DoubleListNode prev = null, DoubleListNode next = null)
 {
     Value = value;
     Prev  = prev;
     Next  = next;
 }