Exemplo n.º 1
0
 /// <summary>
 /// Inserts an element to the back of the list and returns an index to it.
 /// </summary>
 /// <returns>The index where the new element was inserted</returns>
 private int PushBack(LQtElement elt)
 {
     // Check if the array is full
     if (Count == Capacity)
     {
         // Use double the size for the new capacity.
         SetCapacity(Count * 2);
     }
     _data[Count].element = elt;
     return(Count++);
 }
Exemplo n.º 2
0
        /// <summary>
        /// Inserts an element to a vacant position in the list and returns an index to it.
        /// </summary>
        /// <returns>index of the slot where the element was inserted</returns>
        public int Insert(LQtElement elt)
        {
            //if there's an open slot in the free list, pop that and use it
            if (_freeElement != -1)
            {
                int index = _freeElement;

                //set the free index to the next open index in the free list
                _freeElement = _data[index].next;

                //actually insert the element
                _data[index].element = elt;
                //return the index where the element was inserted
                return(index);
            }
            // Otherwise insert to the back of the array.
            return(PushBack(elt));
        }