3

I have this program where I want to insert and delete items in a list. I'm having trouble with my remove function. I want the user to tell me which index they want to delete in the list and then decrease the size of the list and then move the items together. For example: 333 222 111 if I delete the 2nd number then the list would look like 333 111 and the size of the list would decrease to 2.

thanks in advance!

/*  insert
 *  parameters:
 *    index  -- the place in the list to insert newItem
 *    newItem -- the item to insert into the list
 *  returns:
 *    true -- if the item is successfully inserted
 *    false -- otherwise
 *  precondition:  0 < index
 *  postcondition:  newItem is in postiion "index" of the list
 *  Algorithm:  stuff
 */

bool myList::insert(int index, ListItemType newItem) {
    if (!(index > 0)) {
        cerr << "insert:  precondition failed with index = " << index << endl;
        return false;
    }

    if (size == MAX_LIST) {
        cout << "List is full" << endl;
        return false;
    }

    if (index > size) {
        items[size] = newItem;
        size++;
        return true;
    }

    //list is not full and index is b/w items 1 and size-1
    for (int i = size; i >= index; i--) {
        items[i] = items[i - 1];

    }

    items[index - 1] = newItem;
    size++;

    return true;
}

bool myList::remove(int index) {
    //I tried this but it doesn't work well enough
    if (!(index > 0)) {
        cerr << "insert:  precondition failed with index = " << index << endl;
        return false;
    }

    for (int i = size; i >= 0; i--) {
        items[index] = items[index + 1];

    }

    size--;
    return true;
}
10
  • 2
    Unless this is a learning exercise, prefer std::list<>. Commented Oct 31, 2012 at 20:34
  • 6
    no, std::vector<> Commented Oct 31, 2012 at 20:35
  • 1
    the problem lies in your for loop. For each item after the target deletion index, you need to move it back one position. You have all the information, but the loop logic is incorrect. Commented Oct 31, 2012 at 20:38
  • 1
    Yeah, what @BenjaminLindley said. Commented Oct 31, 2012 at 20:39
  • 1
    @BenjaminLindley: No. If he used unsigned type for the index, negative number would add to the end, because too large numbers are explicitly handled in the code and negative number would just be a too large number. Commented Oct 31, 2012 at 21:07

1 Answer 1

2

Like other people said, you should try to use stl. But going with the code you have so far. You should change your for for something like this:

for (int i = index; i < size - 1; i++)
{
    items[i] = items[i+1];

}

What this does is, from the deleted item on, replace each item with the one that follows. It is like shifting to the left.

This wont destroy any elements, but I am guessing we can let that go.

Sign up to request clarification or add additional context in comments.

2 Comments

It works, & what do you mean by "destroy any elements"? @Nick & thanks!
It means that you loose a handle to them before deleting them. But that depends on how you create them. If newItem is not a pointer don't worry about that, they will be deleted automatically.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.