Пример #1
0
        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
        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
        public void Remove(T item)
        {
            LinkedListIterator <T> iter = this.Iterator();

            while (iter.HasNext())
            {
                if (iter.Next().Equals(item))
                {
                    iter.Remove();
                    Length--;
                }
            }
        }
Пример #4
0
        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
        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
        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
        // ******************************** //
        //		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
        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;
            }
        }