コード例 #1
0
        /// <summary>
        /// Time complexity:O(1).
        /// </summary>
        public DoublyLinkedListNode <T> InsertLast(T data)
        {
            if (Tail == null)
            {
                return(InsertFirst(data));
            }

            var newNode = new DoublyLinkedListNode <T>(data);

            Tail.Next = newNode;

            newNode.Previous = Tail;
            newNode.Next     = null;

            Tail = newNode;

            return(newNode);
        }
コード例 #2
0
        /// <summary>
        /// O(1) time complexity
        /// </summary>
        /// <param name="newList"></param>
        internal void Union(DoublyLinkedList <T> newList)
        {
            if (Head == null)
            {
                Head = newList.Head;
                Tail = newList.Tail;
                return;
            }

            if (newList.Head == null)
            {
                return;
            }

            Head.Previous     = newList.Tail;
            newList.Tail.Next = Head;

            Head = newList.Head;
        }
コード例 #3
0
        /// <summary>
        /// Insert right before this node
        /// </summary>
        /// <param name="node"></param>
        public DoublyLinkedListNode <T> InsertBefore(DoublyLinkedListNode <T> node, DoublyLinkedListNode <T> data)
        {
            if (node == null)
            {
                throw new Exception("Empty node");
            }

            if (node == Head && node == Tail)
            {
                node.Previous = data;
                node.Next     = null;
                Tail          = node;

                data.Previous = null;
                data.Next     = node;

                Head = data;

                return(data);
            }

            if (node == Head)
            {
                data.Previous = null;
                data.Next     = node;

                node.Previous = data;
                Head          = data;
            }
            else
            {
                data.Previous = node.Previous;
                data.Next     = node;

                node.Previous.Next = data;
                node.Previous      = data;
            }


            return(data);
        }
コード例 #4
0
        public bool MoveNext()
        {
            if (headNode == null)
            {
                return(false);
            }

            if (currentNode == null)
            {
                currentNode = headNode;
                return(true);
            }

            if (currentNode.Next != null)
            {
                currentNode = currentNode.Next;
                return(true);
            }

            return(false);
        }
コード例 #5
0
        //marks this data as the new head
        //cost O(1)
        public DoublyLinkedListNode <T> InsertFirst(T data)
        {
            var newNode = new DoublyLinkedListNode <T>(data);

            if (Head != null)
            {
                Head.Previous = newNode;
            }

            newNode.Next     = Head;
            newNode.Previous = null;

            Head = newNode;

            if (Tail == null)
            {
                Tail = Head;
            }

            return(newNode);
        }
コード例 #6
0
        /// <summary>
        /// Insert right after this node
        /// </summary>
        /// <param name="node"></param>
        public DoublyLinkedListNode <T> InsertAfter(DoublyLinkedListNode <T> node, DoublyLinkedListNode <T> data)
        {
            if (node == null)
            {
                throw new Exception("Empty reference node");
            }

            if (node == Head && node == Tail)
            {
                node.Next     = data;
                node.Previous = null;

                data.Previous = node;
                data.Next     = null;

                Head = node;
                Tail = data;

                return(data);
            }

            if (node != Tail)
            {
                data.Previous = node;
                data.Next     = node.Next;

                node.Next.Previous = data;
                node.Next          = data;
            }
            else
            {
                data.Previous = node;
                data.Next     = null;

                node.Next = data;
                Tail      = data;
            }

            return(data);
        }
コード例 #7
0
        //cost O(1)
        public T DeleteLast()
        {
            if (Tail == null)
            {
                throw new Exception("Empty list");
            }

            var tailData = Tail.Data;

            if (Tail == Head)
            {
                Head = null;
                Tail = null;
            }
            else
            {
                Tail.Previous.Next = null;
                Tail = Tail.Previous;
            }

            return(tailData);
        }
コード例 #8
0
        //cost O(1)
        public T DeleteFirst()
        {
            if (Head == null)
            {
                throw new Exception("Empty list");
            }

            var headData = Head.Data;

            if (Head == Tail)
            {
                Head = null;
                Tail = null;
            }
            else
            {
                Head.Next.Previous = null;
                Head = Head.Next;
            }

            return(headData);
        }
コード例 #9
0
        public bool MoveNext()
        {
            if (currentNode != null && currentNode.Next != null)
            {
                currentNode = currentNode.Next;
                return(true);
            }

            while (currentNode == null || currentNode.Next == null)
            {
                position++;

                if (position < length)
                {
                    if (hashList[position] == null)
                    {
                        continue;
                    }

                    currentNode = hashList[position].Head;

                    if (currentNode == null)
                    {
                        continue;
                    }

                    return(true);
                }
                else
                {
                    break;
                }
            }

            return(false);
        }
コード例 #10
0
 public void Dispose()
 {
     headNode    = null;
     currentNode = null;
 }
コード例 #11
0
 public void Reset()
 {
     currentNode = headNode;
 }
コード例 #12
0
 internal AsDoublyLinkedListEnumerator(ref DoublyLinkedListNode <T> headNode)
 {
     this.headNode = headNode;
 }
コード例 #13
0
 public void Reset()
 {
     position    = -1;
     currentNode = null;
 }