answersLogoWhite

0


Best Answer

To delete an array element you must create a new array with one less element, then traverse both arrays, copying elements from one to the other, but skipping over the element you want to delete. Once all elements are copied, you can release the original array. A slightly more efficient method would be to move the empty element to the end of the array and then re-allocate the entire array with one less element, using the realloc() command.

To insert a new element, create a new array with one additional element. Then traverse both arrays, copying elements from one to the other. The original array can then be deleted, and the new data can be placed at the end of the new array. Again, the realloc() command is the most efficient method of doing so. However, if the array must be sorted, you must compare the new data with each element prior to copying, and insert at the appropriate point. Once inserted, the remaining elements can be copied into place and then the original array can be released.

Due to the reallocation and copying of elements, this approach is highly inefficient. You can alleviate some of the re-allocations by allocating several empty elements at a time, and keeping track of how many empty elements there are available for insertions. This can be achieved by keeping all empty elements at the end of the array, and maintaining a count of those elements. To delete an existing element, shunt data to the right one place to the left to remove the gap. And if the number of empty elements exceeds a given minimum, re-allocate the array to remove redundancy.

Although this is a better approach, it is still quite inefficient, particularly if the array is large. If the array is small and there are few insertions and deletions, then it probably won't matter too much, but for larger arrays with many insertions and deletions to cater for, a far better approach would be to use a linked list. This makes better use of memory, as the list can expand and contract dynamically without the need to copy any elements (or nodes) whatsoever. Some additional memory is required to maintain the links between the nodes but, unlike an array, no memory is ever wasted.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you insert and delete elements from an array in dynamic memory allocation?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Difference between simple queue and circular queue?

Simple queue is a linear queue having front & rear var to insert & delete elements from the list.But there is a boundary that we have to insert at rear & have delete from front.For this reason instead of having space in the queue if there is a single element in the rear,the queue is full.the other space is wasted.To utilize space properly,circular queue is derived.In this queue the elements are inserted in circular manner.So that no space is wasted at all.


What are the applications of double pointer in c?

insert or delete values both side.so use double pointer


Can you change the size of an array dynamically in Java?

No, you can't change the size of an array dynamically. If you are needing to change the size of an array dynamically use an ArrayList, LinkedList, ConcurrrentHashMap, or another class that meets your needs.


Write an algorithm to insert an item to a given location in sorted array?

To insert a number N into array A at index I: // Resize A if necessary If A is too small to add a new element then resize A // Right-shift all elements starting from position I For i = A.length to I A[i] = A[i - 1] // Insert new item A[I] = N


What are dynamic memory allocation advantages?

Often an application needs additional memory for the temporary storage of data. For example, the C programming language allows the programmer to use the MALLOC (memory allocate) function to grab a chunk of memory suitable for the applications needs. Failure to release the memory after it is used by using the FREE function can result in problems. It is called dynamic memory allocation because the memory is allocated at run-time, as needed. Unlike variables created within functions - the memory is not allocated on the processor stack, instead, when using MALLOC or the 'new' keyword, memory is allocated in the applications virtual address space.

Related questions

What are the database transactions types?

Insert, Update, Delete


To insert delete or replace text is to the document?

It is to Edit


Can we update and delete any of the elements in a list?

Yes. List elements are stored in nodes and we can insert and extract nodes at will. Updating an element is simply a matter of holding a reference to the data held by a particular node. So long as the data is mutable we can change it.


Difference between simple queue and circular queue?

Simple queue is a linear queue having front & rear var to insert & delete elements from the list.But there is a boundary that we have to insert at rear & have delete from front.For this reason instead of having space in the queue if there is a single element in the rear,the queue is full.the other space is wasted.To utilize space properly,circular queue is derived.In this queue the elements are inserted in circular manner.So that no space is wasted at all.


What is the SQL data base material used for?

The SQL data base is programming language used to manipulate and retrieve data. Included in the language are elements such as data insert, query, update and delete.


How do you delete a Pokemon on a pokewalker?

You don't delete. You can switch, though. Easily insert the pokewalker into the DS and change the settings.


How do you delete your freewebsite?

You may not be able to delete it. Rather try making changes that delete everything. Change the info to insert garbage text and bogus photos.


Which control allows you to edit delete and insert records?

Grid Control


What is the importance of database normalization and why they use it?

Reads , insert , update , and delete . . .


Which key deletes character to get the right of the insertion point?

While holding down SHIFT, press END (near home, insert, delete, etc.). Then press DELETE or BACKSPACE.


How do you delete an ad hoc network?

You can delete an ad hoc network from the command prompt window. Here you type: netsh wlan delete profile name="[Insert network name]"


What are the different operations in working with relational database management system?

-Insert -Update -Delete