504 Programming and Data Structures
a) Creation of node: Before insertion, the node is created. Using m alloc () function memory
space is booked for the node.
b) Assignment of data: Once the node is created, data values are assigned to members.
c) Adjusting pointers: The insertion operation changes the sequence. Hence, according to the
sequence, the address of the next element is assigned to the inserted node. The address of the
current node (inserted) is assigned to the previous node.
The node can be inserted in the following positions in the list.
a) Insertion of the node at the starting: The created node is inserted before the first element.
After insertion, the newly inserted element will be the first element of the linked list. In this
insertion only the contents of the successive node's pointer are changed.
b) Insertion at the end of the list: A new element is appended at the end of the list. This operation
is easy as compared to other two (a) and (c) operations. In this insertion only the contents of
the previous node's pointer are changed.
c) Insertion at the given position in the list: In this operation, the user has to enter the position
number. The given position is counted and the element is inserted. In this insertion contents of
both the previous and next node's pointers are altered.
Insertion of the Node at the Starting
Inserting an element at the beginning involves updating links between link fields of two pointers.
After insertion of new node, the previously existing nodes are shifted ahead. Fig. 14.19(a) and
14.9 (b) illustrate the operation of insertion of a node.
Header
5 &
Node 1
8 &
3
X
Node 2
Node 3
9
&
Header
(a) Before insertion
► A
&
b
&
8 & 3
X
9
Node 1 Node 2
Node 2
Node 3
(b) After insertion
Fig. 14.19 Insertion at the beginning
The new node, which is to be inserted, is formed and the arrow indicates the position where it will
be inserted.