Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8793783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:10:01+00:00 2026-06-13T23:10:01+00:00

I have this program where I want to insert and delete items in a

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T23:10:02+00:00Added an answer on June 13, 2026 at 11:10 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this statement in my c program and I want to optimize. By
I have written php program and uploaded on server. I want run this program
I have a Python program (with Django - does this matter?) that I want
I have this program in Python which should save text files to a folder
Alright, I have this program to sparse code in Newick Format, which extracts both
i have this full code: program List; {$APPTYPE CONSOLE} {$R *.res} uses System.SysUtils, Generics.Collections;
I have a list of 10 data objects that I want to insert/update to
I have this program and i want to add a jquery datepicker on it,
I have a map which contains integer values. i want to re-arange this map
I have this program that should execute a piece of code base on the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.