示例#1
0
文件: LinkedList.cs 项目: JvPost/AD
        public void RemoveAtIndex(int index)
        {
            if (index > Length - 1)
            {
                throw new IndexOutOfRangeException();
            }
            else if (index == Length - 1)
            {
                Pop();
            }
            else if (index == 0)
            {
                RemoveFirst();
            }
            else
            {
                LinkedListIterator <T> iter = this.Iterator();
                while (iter.CurrentIndex < index)
                {
                    iter.Next();
                }

                iter.Remove();

                Length--;
            }
        }
示例#2
0
文件: LinkedList.cs 项目: JvPost/AD
        public T Get(int index)
        {
            LinkedListIterator <T> itr = Iterator();
            T item = default(T);

            while (itr.CurrentIndex < index && itr.HasNext())
            {
                item = itr.Next();
            }
            return(item);
        }
示例#3
0
文件: LinkedList.cs 项目: JvPost/AD
        public void Remove(T item)
        {
            LinkedListIterator <T> iter = this.Iterator();

            while (iter.HasNext())
            {
                if (iter.Next().Equals(item))
                {
                    iter.Remove();
                    Length--;
                }
            }
        }
示例#4
0
文件: LinkedList.cs 项目: JvPost/AD
        public T[] ToArray()
        {
            T[] array = new T[Length];

            LinkedListIterator <T> iter = this.Iterator();
            int index = 0;

            while (iter.HasNext())
            {
                array[index] = iter.Next();
                index++;
            }
            return(array);
        }
示例#5
0
文件: LinkedList.cs 项目: JvPost/AD
        public override string ToString()
        {
            string str = "";

            LinkedListIterator <T> iter = this.Iterator();

            while (iter.HasNext())
            {
                str += $"{iter.Next()}, ";
                if (iter.CurrentIndex >= Length)
                {
                    break;
                }
            }
            return($"[{str}]");
        }
示例#6
0
文件: LinkedList.cs 项目: JvPost/AD
        public bool Contains(T item)
        {
            bool contains = false;
            LinkedListIterator <T> iter = this.Iterator();

            while (iter.HasNext())
            {
                if (iter.Next().Equals(item))
                {
                    contains = true;
                    break;
                }
            }

            return(contains);
        }
示例#7
0
文件: LinkedList.cs 项目: JvPost/AD
        // ******************************** //
        //		Private Helper Methods		//
        // ******************************** //
        /// <summary>
        /// Gets a node on an index;
        /// </summary>
        /// <param name="index">index of node to get.</param>
        /// <returns></returns>
        private Node GetNode(int index)
        {
            if (index > Length)
            {
                throw new IndexOutOfRangeException();
            }
            LinkedListIterator <T> itr = Iterator();

            while (index >= itr.CurrentIndex)
            {
                if (!itr.HasNext())
                {
                    throw new IndexOutOfRangeException();
                }
                else
                {
                    itr.Next();
                }
            }
            return(itr.CurrentNode);
        }
示例#8
0
文件: LinkedList.cs 项目: JvPost/AD
        public void Set(int index, T item)
        {
            if (index > Length || index < 0)
            {
                throw new IndexOutOfRangeException();
            }

            Node newNode = new Node(item);
            LinkedListIterator <T> iterator = this.Iterator();

            if (index == 0)
            {
                // empty linked list
                if (!iterator.HasNext())
                {
                    HeaderNode.Next = newNode;
                    TailNode        = newNode;
                }
                else                 // Add newNode to first item
                {
                    newNode.Next    = HeaderNode.Next;
                    HeaderNode.Next = newNode;
                }
            }
            else if (index == Length)
            {
                Push(item);
            }
            // Add item somewhere in the middle.
            else
            {
                while (iterator.HasNext() && iterator.CurrentIndex < index)
                {
                    iterator.Next();
                }
                iterator.Data = item;
            }
        }