C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 15th, 2003, 05:26 AM
mew mew is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 2 mew User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
deletion from an array in C

Hey... I am newbie and have a question.
How do you delete any element from an array? Is shuffling every element along the only way... or is there more of an efficient method?
Thanks

Reply With Quote
  #2  
Old April 15th, 2003, 09:49 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,128 dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 19 h 16 m 26 sec
Reputation Power: 1949
In an array, that's about the size of it. Of course, you can do it in a for-loop, so coding it is not so painful:
Code:
/* index is the index of the item to be deleted
    ARY_SIZE is the array size; indices run from 0 to ARY_SIZE-1
*/
for (i=index; i < (ARY_SIZE-1); i++)
    ary[i] = ary[i+1];


And to insert an item, you do the reverse: shift the elements to the right to make room for the new item. But be sure to start from the end and work to the left.

For small collections of small data (no humungous structs), this is not too inefficient. When your list gets much larger, then a linked list would be preferable. Of course, the code for that is more complex, so you need to consider the trade-offs. Though if you move on to C++, the Standard Template Library (STL) would be available to help you create the more complex data structures.

BTW, even if you are working with arrays of huge structs, there's a more efficient way than shifting all that data left or right. Declare the array as an array of pointers to structs. Then create the structs dynamically (with malloc in C or new in C++) and insert the pointer into the array. Now when you are shifting the array's elements around, you're only moving the pointers and not the data itself.

Reply With Quote
  #3  
Old April 15th, 2003, 05:07 PM
Jason Doucette's Avatar
Jason Doucette Jason Doucette is offline
jasondoucette.com
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Canada
Posts: 378 Jason Doucette User rank is Private First Class (20 - 50 Reputation Level)Jason Doucette User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 7 h 23 m 8 sec
Reputation Power: 11
A faster way to shuffle the elements during delete / insert is to use pointers, so the CPU doesn't have to recompute the index each time.

Basically, a linked list would be faster for inserting / deleting elements, at the trade off for a random access to one. An array is very quick at a random index access in comparison to a linked list. So, you have to think about which operation is performed more often, if you are worried about speed.

Reply With Quote
  #4  
Old April 15th, 2003, 05:25 PM
Analyser's Avatar
Analyser Analyser is offline
*bounce*
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2002
Location: Delft, The Netherlands
Posts: 513 Analyser User rank is Sergeant Major (2000 - 5000 Reputation Level)Analyser User rank is Sergeant Major (2000 - 5000 Reputation Level)Analyser User rank is Sergeant Major (2000 - 5000 Reputation Level)Analyser User rank is Sergeant Major (2000 - 5000 Reputation Level)Analyser User rank is Sergeant Major (2000 - 5000 Reputation Level)Analyser User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 22 h 45 m 12 sec
Reputation Power: 41
Send a message via ICQ to Analyser
Another approach involves the use of "markers". Instead of re-arranging the array when you delete an element, you just mark the spot as being unused.

Of course, there's a chance that there's no value that can be used as a marker. For instance, with an array of chars, you might actually need all 256 distinct values, and not have one left to use as a marker.

Arrays of pointers don't have this problem, since you can always use the NULL value for that.

Just my $0.02.
__________________
"A poor programmer is he who blames his tools."
http://analyser.oli.tudelft.nl/

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > deletion from an array in C

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap