Exemplo n.º 1
0
        public void ToStringShouldPrintValuesInOrder()
        {
            int[] array = new int[] { 5, 12, 15, -61, 0 };
            SingleLinkedNode <int> actual = SingleLinkedNode <int> .CreateFromArray(array);

            string toString = actual.ToString();

            Assert.IsNotNull(toString, "Should not be null string");
            Assert.IsNotEmpty(toString, "Should not be empty string");
            Assert.AreEqual("5, 12, 15, -61, 0", toString, "String is incorrect");
        }
Exemplo n.º 2
0
        public void LastShouldReturnFinalNode()
        {
            int[] array = new int[] { 12, 15, -61, 0, 5 };
            SingleLinkedNode <int> actual = SingleLinkedNode <int> .CreateFromArray(array);

            SingleLinkedNode <int> last = actual.Last();

            Assert.IsNotNull(last, "Last should not be null");
            Assert.IsTrue(last.IsLast, "IsLast should be true for last node");
            Assert.AreEqual(5, last.Value, "Value should be equal to final array value");
        }
Exemplo n.º 3
0
    public override string ToString()
    {
        StringBuilder        s = new StringBuilder();
        SingleLinkedNode <T> p = first;

        while (p != null)
        {
            s.Append(p);
        }
        return(s.ToString());
    }
Exemplo n.º 4
0
        public void SearchShouldReturnNodeWhenValueFound()
        {
            int[] array = new int[] { 12, 15, -61, 0, 5 };
            SingleLinkedNode <int> actual = SingleLinkedNode <int> .CreateFromArray(array);

            SingleLinkedNode <int> result = actual.Search(15);

            Assert.IsNotNull(result, "Should not return null");
            Assert.AreEqual(15, result.Value, "Value");
            Assert.AreEqual(-61, result.Next.Value, "Next value");
        }
Exemplo n.º 5
0
        /// <summary>
        /// Add a node at the begining of the list
        /// </summary>
        /// <param name="data">value of the node</param>
        public override void AddFirst(T data)
        {
            // Create a node with the data
            SingleLinkedNode <T> newNode = new SingleLinkedNode <T>(data);

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

            this.Head.Next = newNode;
        }
Exemplo n.º 6
0
        public bool Contain(T item)
        {
            bool isContain         = false;
            SingleLinkedNode <T> p = head;

            while ((p.Next != null) && (!isContain))//一旦找到就可以提前退出循环
            {
                isContain = item.Equals(p.Next.Item);
                p         = p.Next;
            }
            return(isContain);
        }
Exemplo n.º 7
0
        public override string ToString()
        {
            StringBuilder        s = new StringBuilder();
            SingleLinkedNode <T> p = head;

            while (p.Next != null)
            {
                s.Append(p.Next);
                p = p.Next;
            }
            return(s.ToString());
        }
Exemplo n.º 8
0
        public SingleLinkedList(T[] itemArray) : this()//使用一个数组创建线性表
        {
            SingleLinkedNode <T> rear, q;

            rear = head;//指向链表尾结点
            for (int i = 0; i < itemArray.Length; i++)
            {
                q         = new SingleLinkedNode <T>(itemArray[i]);//新建一个结点
                rear.Next = q;
                rear      = q;
            }
        }
Exemplo n.º 9
0
        public static int Sum(SingleLinkedList <int> list1)
        {
            int sum = 0;
            SingleLinkedNode <int> p = list1.Head;

            while (p.Next != null)
            {
                sum += p.Next.Item;
                p    = p.Next;
            }
            return(sum);
        }
Exemplo n.º 10
0
        //出圈的人用OutValue表示
        public static void JosephusStart2(int n, int s, int d)
        {
            int OutValue = 0;
            //创建包含所有人的一个链表
            CircularLinkedList <int> person = new CircularLinkedList <int>();

            for (int i = 1; i <= n; i++)
            {
                person.Add(i);
            }

            //定位到开始的那个人
            SingleLinkedNode <int> start_p = person.Head;

            for (int i = 0; i < s; i++)
            {
                start_p = start_p.Next;
            }

            int           cnt = 0, total = person.Count;
            StringBuilder out_sequence = new StringBuilder();

            while (total > 1)
            {
                start_p = start_p.Next;
                if (start_p.Item != OutValue)
                {
                    cnt++;
                }
                if ((cnt != 0) && (cnt % d == 0))//有一个人出圈
                {
                    out_sequence.Append(start_p.Item + " -> ");
                    start_p.Item = OutValue;
                    while (start_p.Item == 0)
                    {
                        start_p = start_p.Next;
                    }
                    cnt = 0;
                    total--;
                    person.Show();
                    Console.WriteLine();
                }
            }
            //只剩最后一个人
            while (start_p.Item == 0)
            {
                start_p = start_p.Next;
            }
            out_sequence.Append(start_p.Item);
            Console.WriteLine("Out sequence:");
            Console.WriteLine(out_sequence.ToString());
        }
Exemplo n.º 11
0
        /// <summary>
        /// Find the element
        /// </summary>
        /// <param name="data">an element to find</param>
        /// <returns>Target node</returns>
        private SingleLinkedNode <T> FindNode(T data)
        {
            SingleLinkedNode <T> current = this.Head.Next;

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

            return(null);
        }
Exemplo n.º 12
0
        public T[] ToArray()
        {
            T[] array = new T[Count];
            SingleLinkedNode <T> p = head;
            int i = 0;

            while (p.Next != null)
            {
                array[i] = p.Next.Item;
                p        = p.Next;
                i++;
            }
            return(array);
        }
Exemplo n.º 13
0
        /// <summary>
        /// Pops the last added item from the top of the stack.
        /// </summary>
        /// <remarks>This is an O(1) operation since the size of the stack does not affect how long this operation takes.</remarks>
        /// <returns>The last item added to the stack.</returns>
        /// <exception cref="StackUnderflowException">Thrown when a call is made to an empty stack.</exception>
        public T Pop()
        {
            if (IsEmpty)
            {
                throw new StackUnderflowException("Cannot pop from an empty stack");
            }

            T returnValue = mInnerList.Value;

            mInnerList = mInnerList.Next;
            Count--;

            return(returnValue);
        }
Exemplo n.º 14
0
 /// <summary>
 /// Overrides the hook method to set the previous node for an any implementation that uses a double linked list.
 /// This override ensures that the double linked list Previous node reference is maintained.
 /// </summary>
 /// <param name="newNode">The new node.</param>
 /// <param name="previousNode">The node being set as the new node's previous node.</param>
 protected override void SetPrevious(SingleLinkedNode <T> newNode, SingleLinkedNode <T> previousNode)
 {
     if (newNode != null && newNode.GetType() == typeof(DoubleLinkedNode <T>))
     {
         if (previousNode == null)
         {
             ((DoubleLinkedNode <T>)newNode).Previous = null;
         }
         else if (previousNode.GetType() == typeof(DoubleLinkedNode <T>))
         {
             ((DoubleLinkedNode <T>)newNode).Previous = (DoubleLinkedNode <T>)previousNode;
         }
     }
 }
Exemplo n.º 15
0
        /// <summary>
        /// Add the node at the end of the list
        /// </summary>
        /// <param name="data">value of the node</param>
        public override void AddLast(T data)
        {
            // Create a node with the data
            SingleLinkedNode <T> newNode = new SingleLinkedNode <T>(data);

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

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

            current.Next = newNode;
        }
Exemplo n.º 16
0
        public void AddRange(T[] itemArray)
        {
            SingleLinkedNode <T> p = head;

            while (p.Next != null)
            {
                p = p.Next;
            }
            for (int i = 0; i < itemArray.Length; i++)
            {
                SingleLinkedNode <T> new_item = new SingleLinkedNode <T>(itemArray[i]);
                p.Next = new_item;
                p      = p.Next;
            }
        }
Exemplo n.º 17
0
        public T Pop()
        {
            T k = default(T);

            if (!Empty)
            {
                k              = top.Item;
                top            = top.Next;
                base.Head.Next = top;
                return(k);
            }
            else
            {
                throw new InvalidOperationException("Stack is Empty: " + this.GetType());
            }
        }
Exemplo n.º 18
0
        public void Remove(T item)
        {
            SingleLinkedNode <T> p = head;
            SingleLinkedNode <T> q = head.Next;

            while (q.Next != null)
            {
                if (item.Equals(q.Item))
                {
                    p.Next = q.Next;
                    break;
                }
                p = p.Next;
                q = q.Next;
            }
        }
Exemplo n.º 19
0
        /// <summary>
        /// Enqueues an item at the front of the queue.
        /// </summary>
        /// <remarks>This is an O(1) operation since the size of the queue does not matter.</remarks>
        /// <param name="value">The item to be added.</param>
        public void EnqueueFirst(T value)
        {
            SingleLinkedNode <T> node = CreateNode(value);

            node.Next = mInnerList;

            if (IsEmpty)
            {
                mLast = node;
            }
            else
            {
                SetPrevious(mInnerList, node);
            }

            mInnerList = node;
            mCount++;
        }
Exemplo n.º 20
0
    public void Add(T item)
    {
        SingleLinkedNode <T> new_item = new SingleLinkedNode <T>(item);
        SingleLinkedNode <T> p        = first;

        if (first == null)
        {
            first = new_item;
        }
        else
        {
            while (p.Next != null)
            {
                p = p.Next;
            }
            p.Next = new_item;
        }
    }
Exemplo n.º 21
0
 public void RemoveAt(int index)
 {
     if ((index < 0) || (index >= Count))
     {
         throw new IndexOutOfRangeException("Index out of Range!!!");
     }
     else
     {
         SingleLinkedNode <T> p = head;
         SingleLinkedNode <T> q = head.Next;
         for (int i = 0; i < index; i++)
         {
             p = p.Next;
             q = q.Next;
         }
         p.Next = q.Next;
     }
 }
Exemplo n.º 22
0
        /// <summary>
        /// Adds the supplied <paramref name="value"/> to the end of the queue.
        /// </summary>
        /// <remarks>This is an O(1) operation as it does not depend on the size of the queue to add a new item.</remarks>
        /// <param name="value">The value to be added.</param>
        public void Enqueue(T value)
        {
            SingleLinkedNode <T> node = CreateNode(value);

            if (IsEmpty)
            {
                mInnerList = node;
            }
            else
            {
                mLast.Next = node;
                SetPrevious(node, mLast);
            }

            mLast = node;

            mCount++;
        }
Exemplo n.º 23
0
        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");
        }
Exemplo n.º 24
0
 public void InsertRange(int index, T[] itemArray)
 {
     if ((index < 0) || (index >= Count))
     {
         throw new IndexOutOfRangeException("Index out of range!!!");
     }
     else
     {
         if (index == 0)
         {
             //保存原来的第一个元素
             SingleLinkedNode <T> p = first;
             first = new SingleLinkedNode <T>(itemArray[0]);
             //新链表的第一个元素
             SingleLinkedNode <T> q = first;
             for (int i = 1; i < itemArray.Length; i++)
             {
                 //新的数组有多少个元素就必须新建多少个对象
                 SingleLinkedNode <T> new_item = new SingleLinkedNode <T>(itemArray[i]);
                 q.Next = new_item;
                 q      = q.Next;
             }
             q.Next = p;
         }
         else
         {
             SingleLinkedNode <T> p = first;
             SingleLinkedNode <T> q = first.Next;
             for (int i = 1; i < index; i++)
             {
                 p = p.Next;
                 q = q.Next;
             }
             for (int i = 0; i < itemArray.Length; i++)
             {
                 //新的数组有多少个元素就必须新建多少个对象
                 SingleLinkedNode <T> new_item = new SingleLinkedNode <T>(itemArray[i]);
                 p.Next = new_item;
                 p      = p.Next;
             }
             p.Next = q;
         }
     }
 }
Exemplo n.º 25
0
        public int IndexOf(T item)
        {
            int index = 0;
            SingleLinkedNode <T> p = head;

            while (p.Next != null)
            {
                if (item.Equals(p.Next.Item))//一旦找到匹配的就立刻退出函数
                {
                    return(index);
                }
                else
                {
                    index++;
                    p = p.Next;
                }
            }
            return(-1);
        }
Exemplo n.º 26
0
        /// <summary>
        /// 使用自定义的单向链表实现,将元素删除(未实现)
        /// </summary>
        public static void singleListSolvePro(int n, int s, int d)
        {
            int[] a = new int[n];
            for (int i = 0; i < n; i++)
            {
                a[i] = i + 1;
            }
            // 将n个人依次插入线性表
            CircularLinkedList <int> ring = new CircularLinkedList <int>(a);
            //ring.Show(true);

            SingleLinkedNode <int> p = ring.Head;
            int t = 0, k = 0;

            while (p != null)
            {
                if (ring.Count == 1)
                {
                    break;
                }
                if (k < s - 1)
                {
                    k++;
                    // 因为链表长度肯定大于s,因此不判断next为空
                    p = p.Next;
                    continue;
                }
                t++;
                if (t % d == 0)
                {
                    p.Next = p.Next.Next;
                    //ring.Show(true);
                }
                else
                {
                    p = p.Next;
                }
            }

            //Console.WriteLine("Out :  " + ring[i]);
            ring.Show(true);
            //Console.WriteLine("\n{0} is the last person.", );
        }
Exemplo n.º 27
0
        //出圈的人直接从链表中去掉
        public static void JosephusStart1(int n, int s, int d)
        {
            //创建包含所有人的一个链表
            CircularLinkedList <int> person = new CircularLinkedList <int>();

            for (int i = 1; i <= n; i++)
            {
                person.Add(i);
            }

            //定位到开始的那个人
            SingleLinkedNode <int> start_p = person.Head;

            for (int i = 0; i < s; i++)
            {
                start_p = start_p.Next;
            }

            int           cnt          = 0;
            StringBuilder out_sequence = new StringBuilder();

            while (person.Count > 1)
            {
                start_p = start_p.Next;
                cnt++;
                if ((cnt != 0) && (cnt % d == 0))
                {
                    //利用cnt暂时存放一下出圈人的编号,可以避免新建一个变量
                    cnt     = start_p.Item;
                    start_p = start_p.Next;
                    out_sequence.Append(cnt + " -> ");
                    person.Remove(cnt);
                    cnt = 0;
                    person.Show();
                    Console.WriteLine();
                }
            }
            //只剩最后一个人
            out_sequence.Append(person.Head.Next.Item);
            Console.WriteLine("Out sequence:");
            Console.WriteLine(out_sequence.ToString());
        }
Exemplo n.º 28
0
 //索引器
 public virtual T this[int i]
 {
     get
     {
         if ((i < 0) || (i >= Count))
         {
             throw new IndexOutOfRangeException("Index is out of range " + this.GetType());
         }
         else
         {
             SingleLinkedNode <T> p = head.Next;
             while (i > 0)
             {
                 p = p.Next;
                 i--;
             }
             return(p.Item);
         }
     }
 }
Exemplo n.º 29
0
        public T Dequeue()
        {
            T k = default(T);

            if (!Empty)
            {
                k               = front.Item;
                front           = front.Next;
                items.Head.Next = front;
                if (front == null)
                {
                    rear = items.Head;
                }
                return(k);
            }
            else
            {
                throw new InvalidOperationException("Queue is Empty: " + this.GetType());
            }
        }
Exemplo n.º 30
0
 public void Insert(int index, T item)
 {
     if ((index < 0) || (index >= Count))
     {
         throw new IndexOutOfRangeException("Index is out of range " + this.GetType());
     }
     else
     {
         SingleLinkedNode <T> p        = head;
         SingleLinkedNode <T> q        = head.Next;
         SingleLinkedNode <T> new_item = new SingleLinkedNode <T>(item);
         for (int i = 0; i < index; i++)
         {
             p = p.Next;
             q = q.Next;
         }
         p.Next        = new_item;
         new_item.Next = q;
     }
 }