Item * insert_item(Item *list, Item *item, int position) { assert(list != NULL); assert(item != NULL); assert(position >= 0); if (position == 0) { return prepend_item(list, item); } Item *current_item = list; // In order to insert a new item, we must find the item 1 position before the // desired insert position. int i = position - 1; while(--i && current_item != NULL) { current_item = current_item->next_item; } if (current_item == NULL) { return append_item(list, item); } // Insert the new item Item *temp = current_item->next_item; current_item->next_item = item; item->next_item = temp; return list; }
item& item::prepend() { return *prepend_item(parent_item?(new item(parent_item)):(new item(relation_ptr))); }
item& item::prepend(item& other) { return *prepend_item(parent_item?(new item(other,parent_item)):(new item(other,relation_ptr))); }