Example #1
0
 public DoubleLinkedNode(DoubleLinkedNode value)
 {
     if (!value.avaluable)
     {
         next      = this;
         last      = this;
         avaluable = false;
     }
     data      = value.data;
     next      = this;
     last      = this;
     avaluable = true;
 }
Example #2
0
 public void Remove(DoubleLinkedNode <T> node)
 {
     if (m_Count > 0)
     {
         Assert.IsTrue(node != null && node.m_Next != null && node.m_Previous != null, "Remove方法要求node不为null, 并且Node在链表中!");
         node.m_Previous.m_Next = node.m_Next;
         node.m_Next.m_Previous = node.m_Previous;
         node.m_Next            = null;
         node.m_Previous        = null;
         m_Count--;
         m_Pool.Recycle(node);
     }
 }
        public List <DoubleLinkedNode> ToArray()
        {
            List <DoubleLinkedNode> list = new List <DoubleLinkedNode>();
            DoubleLinkedNode        p    = this.Head;

            while (p.Next != null)
            {
                p = p.Next;
                list.Add(p);
            }

            return(list);
        }
        public List <DoubleLinkedNode> Traversal()
        {
            List <DoubleLinkedNode> list = new List <DoubleLinkedNode>();
            DoubleLinkedNode        p    = this.Head;

            while (p.Next != null)
            {
                p = p.Next;
                Console.Write(p.Value + " ,");
                list.Add(p);
            }

            return(list);
        }
        public void InsertBetween(DoubleLinkedNode <T> previous, DoubleLinkedNode <T> next)
        {
            this.Previous = previous;
            if (previous != null)
            {
                previous.Next = this;
            }

            this.Next = next;
            if (next != null)
            {
                next.Previous = this;
            }
        }
Example #6
0
        /// <summary>
        /// Insert after previousNode
        /// </summary>
        /// <param name="item">Data for the node iserted.</param>
        /// <param name="p">Location after which the item and node should be located.</param>
        public void Insert(T item, DoubleLinkedListIterator p)
        {
            if (item != null && p != null)
            {
                DoubleLinkedNode newNode = new DoubleLinkedNode();
                newNode.Data = item;

                p.Current.Next = newNode;
                Tail.Previous  = newNode;

                newNode.Next     = Tail;
                newNode.Previous = p.Current;
            }
        }
Example #7
0
        public void DoubleLinkedNodeTests_Initialize()
        {
            this.TestName = "DoubleLinkedNodeTests_Initialize()";
            this.Log.AppendLine(this.TestName);

            this.ValueA = "A";
            this.ValueB = "B";
            this.ValueC = "C";

            this.NodeA = new DoubleLinkedNode <string>(this.ValueA);
            this.NodeB = new DoubleLinkedNode <string>(this.ValueB);
            this.NodeC = new DoubleLinkedNode <string>(this.ValueC);
            this.Log.AppendLine("");
        }
        public new void Remove()
        {
            if (this.Previous != null)
            {
                this.Previous.Next = this.Next;
                this.Previous      = null;
            }
            if (this.Next != null)
            {
                this.Next.Previous = this.Previous;
                this.Next          = null;
            }

            this.Dispose();
        }
Example #9
0
        public LRUCache(int capacity)
        {
            this.count    = 0;
            this.Capacity = capacity;

            head     = new DoubleLinkedNode();
            head.pre = null;

            tail      = new DoubleLinkedNode();
            tail.post = null;

            head.post    = tail;
            tail.pre     = head;
            this.handler = new DoubleLinkedNodeHandler(head, tail);
        }
Example #10
0
        /// <summary>
        /// <para>Sorts the list based on <see cref="Comparer"/>. </para>
        /// <para>Expected runtime: O(n^2), where n is the number of elements in the list.</para>
        /// </summary>
        /// TODO: Allow for different sorting methods that use this.Comparer
        public new void Sort()
        {
            //Grab the min and max from the current List as new nodes
            DoubleLinkedNode <T> min = new DoubleLinkedNode <T>(this.GetMinimum());
            DoubleLinkedNode <T> max = new DoubleLinkedNode <T>(this.GetMaximum());

            //Create the sorted list with the head node being the min
            //and the tail node being the Max
            min.Next = max;

            DoubleLinkedNode <T> nodeFromOriginalList = this.headNodeOfList;

            //Iterrate over the current List
            for (int i = 0; i < this.Count(); i++)
            {
                DoubleLinkedNode <T> currentNodeFromSortedList  = min;
                DoubleLinkedNode <T> previousNodeFromSortedList = currentNodeFromSortedList;

                //While the currentNodeFromSortedList is less than nodeFromOriginalList,
                //Keep traversing down the sorted list
                while (this.Comparer.Compare(currentNodeFromSortedList.Value, nodeFromOriginalList.Value) == -1)
                {
                    previousNodeFromSortedList = currentNodeFromSortedList;
                    currentNodeFromSortedList  = currentNodeFromSortedList.Next;
                }

                //Ensure we don't duplicate adding min and max to the sorted list
                if (nodeFromOriginalList.Value.Equals(min.Value) == false &&
                    nodeFromOriginalList.Value.Equals(max.Value) == false)
                {
                    //Add this new node between previous and current
                    //Example: on the first iterration, this should add a value between min and max
                    //such that the sorted list looks like: A -> B -> C
                    new DoubleLinkedNode <T>(nodeFromOriginalList.Value).InsertBetween(
                        previousNodeFromSortedList,
                        currentNodeFromSortedList
                        );
                }

                //Grab the next node from the unsortedList
                nodeFromOriginalList = nodeFromOriginalList.Next;
            }

            //Remove and overwrite the unsorted list
            this.Clear();
            this.headNodeOfList = min;
            this.tailNodeOfList = max;
        }
Example #11
0
        public void delete(int start, int num)//删除元素
        {
            DoubleLinkedNode now = findPotion(pos, start);
            int count            = 0;

            while (count < num)
            {
                now = now.Next;
                if (now.Last == pos)
                {
                    pos = now;
                }
                now.Last.delete();
                count++;
            }
        }
Example #12
0
        public iterator <T> sub(int start = 0)
        {
            DoubleLinkedNode now = findPotion(pos, start);
            iterator <T>     it  = new iterator <T>();

            it.pos = new DoubleLinkedNode(now);
            DoubleLinkedNode next = it.pos;

            do
            {
                now = now.Next;
                new DoubleLinkedNode(now, next, it.pos);
                next = next.Next;
            } while (now != pos.Last);
            return(it);
        }
Example #13
0
 public DoubleLinkedNode(T value, DoubleLinkedNode m_last, DoubleLinkedNode m_next)
 {
     if (!m_last.avaluable || !m_next.avaluable)
     {
         data      = value;
         next      = this;
         last      = this;
         avaluable = true;
     }
     data        = value;
     next        = m_next;
     m_next.last = this;
     last        = m_last;
     m_last.next = this;
     avaluable   = true;
 }
Example #14
0
        /// <summary>
        /// Add the node at the end of the list
        /// </summary>
        /// <param name="data"></param>
        public override void AddLast(T data)
        {
            // Create a node with the data
            DoubleLinkedNode <T> newNode = new DoubleLinkedNode <T>(data);

            // Traverse till the end
            DoubleLinkedNode <T> current = this.Head;

            while (current.Next != null)
            {
                current = current.Next;
            }

            current.Next     = newNode;
            newNode.Previous = current;
        }
Example #15
0
        /// <summary>
        /// Find the element
        /// </summary>
        /// <param name="data">an element to find</param>
        /// <returns>Target node</returns>
        private DoubleLinkedNode <T> FindNode(T data)
        {
            DoubleLinkedNode <T> current = this.Head.Next;

            while (current != null)
            {
                if (current.Data.Equals(data))
                {
                    return(current);
                }

                current = current.Next;
            }

            return(null);
        }
Example #16
0
 private void InplaceAdd(DoubleLinkedNode /*!*/ cur)
 {
     if (count == 0)
     {
         head  = cur;
         last  = cur;
         count = 1;
     }
     else
     {
         last.next = cur;
         cur.prev  = last;
         last      = cur;
         count++;
     }
 }
Example #17
0
 /// <summary>
 /// Adds the object to the sequence [Time: constant]
 /// </summary>
 private int InplaceAdd(T o)
 {
     if (count == 0 || last.next == null)
     {
         InplaceAdd(new DoubleLinkedNode(o));
     }
     else
     {
         Sequence <T> s = this.Dup();
         s.InplaceAdd(new DoubleLinkedNode(o));
         this.count = s.count;
         this.head  = s.head;
         this.last  = s.last;
     }
     return(this.count);
 }
Example #18
0
 public T this[int id]
 {
     get
     {
         return(findPotion(pos, id).Data);
     }
     set
     {
         DoubleLinkedNode next   = findPotion(pos, id);
         DoubleLinkedNode newPos = new DoubleLinkedNode(value, next.Last, next);
         if (id == 0)
         {
             pos = newPos;
         }
         next.delete();
     }
 }
Example #19
0
        /// <summary>
        /// Returns the zero-based index of the last occurrence of the given object in the Vector, -1 if it doesn't occur (linear time).
        /// </summary>
        public int LastIndexOf(T o)
        {
            DoubleLinkedNode cur = last;

            for (int i = count - 1; i >= 0; i--)
            {
                if (Object.Equals(o, cur.elem))
                {
                    return(i);
                }
                else
                {
                    cur = cur.prev;
                }
            }
            return(-1);
        }
Example #20
0
        public new bool MoveNext()
        {
            if (this.enumerationStarted == false && this.currentEnumeratorNode == null)
            {
                this.currentEnumeratorNode = this.headNodeOfList;
                this.enumerationStarted    = true;
                return(true);
            }

            if (this.currentEnumeratorNode.Next == null)
            {
                return(false);
            }

            this.currentEnumeratorNode = this.currentEnumeratorNode.Next;
            return(true);
        }
Example #21
0
        //public int Add(object o){ return _seq.Add(o);}
        //public bool IsReadOnly{ get {return _seq.IsReadOnly; } }
        //public bool IsFixedSize { get {return _seq.IsFixedSize; }}

        /// <summary>
        /// Returns the zero-based index of the first occurrence of the given object in the sequence, -1 if it doesn't occur.
        /// </summary>
        /// <param name="o">The item to be bound</param>
        /// <returns>The zero-based index of the first occurrence of <paramref name="o"/></returns>
        /// <remarks>[Complexity: O(this.Count)]</remarks>
        public int IndexOf(T o)
        {
            DoubleLinkedNode cur = this.head;

            for (int i = 0; i < this.count; i++)
            {
                if (Object.Equals(o, cur.elem))
                {
                    return(i);
                }
                else
                {
                    cur = cur.next;
                }
            }
            return(-1);
        }
Example #22
0
        public static void InsertSort(DoubleLinkedList <int> list, int num)
        {
            Sort(list);
            DoubleLinkedNode <int> p    = list.Head.Next;
            DoubleLinkedNode <int> item = new DoubleLinkedNode <int>(num);

            while (p != null)
            {
                if ((p.Item < num) && (p.Next.Item > num))
                {
                    item.Front   = p;
                    item.Next    = p.Next;
                    p.Next.Front = item;
                    p.Next       = item;
                }
                p = p.Next;
            }
        }
Example #23
0
        public Sequence <T> Reverse()
        {
            Sequence <T> s = this;

            if (s.count == 0)
            {
                return(s);
            }
            DoubleLinkedNode cur = s.last;
            Sequence <T>     r   = new Sequence <T>();

            for (int i = 0; i < s.count; i++)
            {
                r.InplaceAdd(new DoubleLinkedNode(cur.elem));
                cur = cur.prev;
            }
            return(r);
        }
        public void InstanceShouldThrowExceptionIfNextIsNotDoubleLinkedNodeType()
        {
            Assert.DoesNotThrow(() => DoubleLinkedNode <int> .Instance(0, null, null),
                                "Passing null as next or previous should be fine - indicates first item being added to the list");

            DoubleLinkedNode <int> node = DoubleLinkedNode <int> .Instance(0, null, null);

            DoubleLinkedNode <int> nextNode = DoubleLinkedNode <int> .Instance(1, null, node);

            node.Next = nextNode;
            DoubleLinkedNode <int> lastNode = DoubleLinkedNode <int> .Instance(2, null, nextNode);

            nextNode.Next = lastNode;

            SingleLinkedNode <int> invalidNode = SingleLinkedNode <int> .Instance(-1, node);

            _ = Assert.Throws(typeof(ArgumentException), () => { node.Next = invalidNode; }, "Should throw an exception due to invalid type of node");
        }
Example #25
0
 private static DoubleLinkedNode findPotion(DoubleLinkedNode start, int id)
 {
     if (id >= 0)
     {
         for (int i = 0; i < id; i++)
         {
             start = start.Next;
         }
     }
     else
     {
         for (int i = 0; i > id; i--)
         {
             start = start.Last;
         }
     }
     return(start);
 }
Example #26
0
        public iterator(T[] tar)//使用数组初始化
        {
            if (tar.Length == 0)
            {
                pos = new DoubleLinkedNode();
                return;
            }
            DoubleLinkedNode first;
            DoubleLinkedNode next;

            first = new DoubleLinkedNode(tar[0]);
            pos   = first;
            for (int i = 1; i < tar.Length; i++)
            {
                next  = new DoubleLinkedNode(tar[i], first, pos);
                first = next;
            }
        }
Example #27
0
        /// <summary>
        /// <para>Adds the given value to the end of the list.</para>
        /// <para>Expected Runtime: O(1)</para>
        /// </summary>
        /// <param name="value"></param>
        public new void Add(T value)
        {
            //if (this.Comparer == null)
            //    this.Comparer = Comparer<T>.Default;

            if (this.Count() > 0)
            {
                if (this.cachedMinimum != null && this.Comparer.Compare(value, this.cachedMinimum.Value) == -1)
                {
                    this.isCachedMinimumValid = false;
                }
                if (this.cachedMaximum != null && this.Comparer.Compare(value, this.cachedMaximum.Value) == 1)
                {
                    this.isCachedMaximumValid = false;
                }
            }
            this.isCachedCountValid = false;

            if (this.headNodeOfList == null)
            {
                this.headNodeOfList = new DoubleLinkedNode <T>(value);
                this.tailNodeOfList = this.headNodeOfList;

                //WARNING!
                //Do not comment this out. This creates a circular loop that will
                //lead to a stack overflow.
                //this.headNodeOfList.ConnectTo(this.tailNodeOfList);
            }
            else if (this.headNodeOfList == this.tailNodeOfList)
            {
                this.tailNodeOfList = new DoubleLinkedNode <T>(value);
                this.headNodeOfList.ConnectTo(this.tailNodeOfList);
            }
            else
            {
                this.tailNodeOfList.ConnectTo(new DoubleLinkedNode <T>(value));
                this.tailNodeOfList = this.tailNodeOfList.Next;
            }

            //WARNING!
            //Do not comment this out. This list is built with the assumption
            //that you cannot wrap around to the beginning.
            //this.tailNodeOfList.Next = this.headNodeOfList;
        }
        public void CreateFromArrayShouldMaintainPreviousAndNextLinks()
        {
            int[] array = new int[] { 5, 12, 15, -61, 0 };
            DoubleLinkedNode <int> actual = DoubleLinkedNode <int> .CreateFromArray(array);

            Assert.AreEqual(5, actual.Value, "Value");
            Assert.AreEqual(12, actual.Next.Value, "Next Value");
            Assert.IsNull(actual.Previous, "Previous");
            Assert.IsTrue(actual.IsHeader, "IsHeader");
            Assert.IsFalse(actual.IsLast, "IsLast");
            Assert.AreEqual(5, DoubleLinkedNode <int> .Length(actual), "Length");

            actual = (DoubleLinkedNode <int>)actual.Next;
            Assert.AreEqual(12, actual.Value, "Value");
            Assert.AreEqual(15, actual.Next.Value, "Next Value");
            Assert.AreEqual(5, actual.Previous.Value, "Previous Value");
            Assert.IsFalse(actual.IsHeader, "IsHeader");
            Assert.IsFalse(actual.IsLast, "IsLast");
            Assert.AreEqual(4, DoubleLinkedNode <int> .Length(actual), "Length");

            actual = (DoubleLinkedNode <int>)actual.Next;
            Assert.AreEqual(15, actual.Value, "Value");
            Assert.AreEqual(-61, actual.Next.Value, "Next Value");
            Assert.AreEqual(12, actual.Previous.Value, "Previous Value");
            Assert.IsFalse(actual.IsHeader, "IsHeader");
            Assert.IsFalse(actual.IsLast, "IsLast");
            Assert.AreEqual(3, DoubleLinkedNode <int> .Length(actual), "Length");

            actual = (DoubleLinkedNode <int>)actual.Next;
            Assert.AreEqual(-61, actual.Value, "Value");
            Assert.AreEqual(0, actual.Next.Value, "Next Value");
            Assert.AreEqual(15, actual.Previous.Value, "Previous Value");
            Assert.IsFalse(actual.IsHeader, "IsHeader");
            Assert.IsFalse(actual.IsLast, "IsLast");
            Assert.AreEqual(2, DoubleLinkedNode <int> .Length(actual), "Length");

            actual = (DoubleLinkedNode <int>)actual.Next;
            Assert.AreEqual(0, actual.Value, "Value");
            Assert.IsNull(actual.Next, "Next");
            Assert.AreEqual(-61, actual.Previous.Value, "Previous Value");
            Assert.IsFalse(actual.IsHeader, "IsHeader");
            Assert.IsTrue(actual.IsLast, "IsLast");
            Assert.AreEqual(1, DoubleLinkedNode <int> .Length(actual), "Length");
        }
Example #29
0
        /// <summary>
        /// <para>Removes the given value from the list.</para>
        /// <para>Expected Runtime: O(n), where n is the number of elements in the list.</para>
        /// </summary>
        /// <param name="value"></param>
        public new void Remove(T value)
        {
            DoubleLinkedNode <T> currentNode  = this.headNodeOfList;
            DoubleLinkedNode <T> previousNode = currentNode;

            while (currentNode.Value.Equals(value) == false)
            {
                previousNode = currentNode;
                currentNode  = currentNode.Next;

                //Reached the end of the list, value wasn't in the lsit to begin with
                //Exit
                if (currentNode == null)
                {
                    return;
                }
            }

            //Handle special cases
            if (currentNode.Equals(this.headNodeOfList))
            {
                this.headNodeOfList = this.headNodeOfList.Next;
            }
            else if (currentNode.Equals(this.tailNodeOfList))
            {
                this.tailNodeOfList      = previousNode;
                this.tailNodeOfList.Next = null;
            }

            //Invalidate all of the caches
            this.isCachedCountValid = false;
            if (currentNode.Value.Equals(this.GetMinimum()))
            {
                this.isCachedMinimumValid = false;
            }
            if (currentNode.Value.Equals(this.GetMaximum()))
            {
                this.isCachedMaximumValid = false;
            }

            //Remove the node and its value
            currentNode.Remove();
        }
Example #30
0
        /// <summary>
        /// <para>Gets the node for the given index.</para>
        /// <para>Expected runtime: O(n), where n = index</para>
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        protected new DoubleLinkedNode <T> GetNodeAt(int index)
        {
            if (index < 0 || index > this.Count())
            {
                string exceptionMessage = (index < 0) ? " less than 0." : " greater than the size of the list.";
                throw new IndexOutOfRangeException(
                          "The provided index (" + index.ToString() + ") was invalid. index cannot be" + exceptionMessage
                          );
            }

            DoubleLinkedNode <T> node = this.headNodeOfList;

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

            return(node);
        }