Adding things to Array-Based Lists

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2007
Posts: 31
Reputation: DemonSpeeding is an unknown quantity at this point 
Solved Threads: 0
DemonSpeeding DemonSpeeding is offline Offline
Light Poster

Adding things to Array-Based Lists

 
0
  #1
Oct 16th, 2007
I have a question about adding say integers to an array-based list of numbers.
I know how to add something at the end it would be something like:
values[count] = newValue;
count ++;

What about if someone wanted to add something to the beginning or somewhere between? My book doesn't say how to do such things because with an unsorted list order doesn't matter so it only thinks you would just throw things on the end. But what if you didn't want to?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,567
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 706
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Adding things to Array-Based Lists

 
0
  #2
Oct 16th, 2007
>What about if someone wanted to add something to the beginning or somewhere between?
It's more or less the same deal. You move everything from the index you want to add to, to the end of the array forward by one element. Then you add the new value to the index you want and increase the count:
  1. for ( int i = count; i > index; i-- )
  2. values[i] = values[i - 1];
  3.  
  4. values[index] = newValue;
  5. ++count;
It's not desirable because it's potentially very expensive to shift array elements like that.
Last edited by Narue; Oct 16th, 2007 at 6:29 pm.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,340
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1458
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Adding things to Array-Based Lists

 
0
  #3
Oct 16th, 2007
If you want to keep all existing values in the array then you need use a loop to shift everything down from the spot you want to add the new value.

[edit]^^^ what Narue said [/edit]
Last edited by Ancient Dragon; Oct 16th, 2007 at 6:31 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC