answersLogoWhite

0

Insertion is an operation that inserts a new datum (an object or data element) into a data container. How this is achieved depends on data container's type.

The simplest container is an array. In order to insert a new element into an array there has to be space to accommodate the new element. Free space is always allocated at the end of the array as it is easier to keep track of which elements are in use when all the unused elements are at the end of the array. If there is no free space, the array must be reallocated to make space. Reallocation can be an expensive operation so we typically double the allocation on each reallocation. Since all the free space is at the end of the array, it is trivial to push new elements onto the end of the array in the first unused space. If we need to insert anywhere else in the array, we use a single-pass insertion sort algorithm to move the unsorted element (the new element) into its correct position within the sorted set (the used elements). This essentially moves every element that should come after the new element one position to the right, towards the end of the array, starting with the last element. Once all elements are moved we can insert the new element into the gap we created.

With a singly-linked list, the most efficient insertion is to insert at the head of the list. This is achieved by pointing its node at the head node and making the new node the new head of the list. If we need to insert elsewhere, we must traverse the nodes to locate the node that comes before the new node. We then point the new node at that previous node's next node and point the previous node at the new node.

With a doubly-linked list we can insert at either the head or the tail and can traverse in either direction to locate any other insertion point. Again, adjusting the affected node pointers completes the insertion.

Stacks always insert on the top of the stack (typically implemented as singly-linked list where all insertions occur at the head). Queues always insert at the end of the queue (typically implemented as a circular singly-linked list where we keep track of the tail which points back at the head node for extractions).

User Avatar

Wiki User

9y ago

What else can I help you with?

Related Questions

Using doublelinked list insertion sort in c language?

using doublelinked list insertion sort in c language


What is c and c in computer programming?

C and C++ are both high-level programming languages.


Create a spiral in C programming?

create spiral in c programming?


When was C - programming language - created?

C - programming language - was created in 1972.


What has the author Robert Lafore written?

Robert Lafore has written: 'Object-oriented programming in Microsoft C++' -- subject(s): Object-oriented programming (Computer science), C++ (Computer program language), C. 'Microsoft C. Programming for the I.B.M.Personal Computer' 'Object-oriented programming in C++' 'Windows Programming Made Ridiculously Easy Book' 'The Waite Group's C Programming Using Turbo C++ (The Waite Group)' 'Microsoft C programming for the IBM' 'The Waite Group's Microsoft C programming for the PC' -- subject(s): C (Computer program language), IBM microcomputers, Microsoft C (Computer program), Microsoft C., Programming


What is a C compiler as used in C programming?

In C programming, C compiler is user to translate C source into C object module.


What is the benefit of the C programming languages?

C language is better for hardware programming .Most of the programming for hardware are written in C language so it is beneficial for hardware programming is not efficient for application programming due to drawback like in C data moves around the system.


Name of object oriented programming language?

C++ is the name of a programming language.


Where can one find C programming tutorials?

You can find C programming tutorials online at the C programming website. They provide both free and paid tutorials for many aspects of the C and C++ code.


Websites to download c C programming language books?

Download 1000s of C C C++ Programming Language. http://www.guruengineers.com


What programming language is Android written in?

Android is programmed in the C and C++ programming language.


Why dafault constructor is necessary in c programming?

It is not necessary (nor possible) in C programming.