Ejemplo n.º 1
0
        /* Removes and returns the first value in the list */
        public T Remove()
        {
            /*
             * Store The First Value
             * head = head.next
             * return firstValue
             */

            T value = Head.Value;

            Head = Head.Next;
            Count--;

            return(value);
        }
Ejemplo n.º 2
0
        /* Returns the value at the given index. Any index less than zero or equal
         * to or greater than Count should throw an index out of bounds exception */
        public T Get(int index)
        {
            if (index < 0 || index >= Count)
            {
                throw new IndexOutOfRangeException();
            }

            SNode <T> node = Head;

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

            return(node.Value);
        }
Ejemplo n.º 3
0
        /* Removes and returns the value at a given index. Any index less than zero or
         * equal to or greater than Count should throw an index out of bounds exception */
        public T RemoveAt(int index)
        {
            /*
             * indexToFind = x
             * if (indexToFind < 0 || indexToFind >= count)
             *  throw error
             *
             * index = 0
             * iterateAllNodes from Start to Stop at x-1
             *  index++
             *
             * returnValue = previous.next.value
             * compareNode.next = previous.next?.next
             *
             * count--
             */
            if (index < 0 || index >= Count)
            {
                throw new IndexOutOfRangeException();
            }

            T value;

            if (index == 0)
            {
                value = Head.Value;
                Head  = Head.Next;
                Count--;
                return(value);
            }

            SNode <T> prevNode = Head;

            int currentIndex = 0;

            while (currentIndex < index - 1)
            {
                prevNode = prevNode.Next;
                currentIndex++;
            }

            value         = prevNode.Next.Value;
            prevNode.Next = prevNode.Next.Next;
            Count--;

            return(value);
        }