Insertion in Singly Linked List
Linked List
• What is a Linked List?
Linked List is a data structure.
It is collection of nodes.
Linked List is used widely because it do not need continue memory slot,
because the data stored in linked list is accessed through ‘Links’.
Singly Linked List
In a singly linked list node includes a ‘data’ part and a ‘link’ part.
Node :
Data: It includes any information that is to be stored.
Link: It have the address of the next consecutive node.
Data Link
Insertion
(1) Data Structures are used so that a data can be easily modified and
accessed.
(2) This modification includes: Insertion, Updating of Value, Delete and
some other special operations.
(3) Insertion is needed in a data structure for adding a value or
information while making it first time or it can also take place after it
has been made once completely. That is why insertion may happen in
any part of the data structure: At First, At Middle or At Last.
Insertion in Singly Linked List
• Insertion in Singly Linked list is quite convenient in compare to array
like structure.
• In a structure like array a number of shifts may take place which is not
at all acceptable.
• In a Singly Linked List only swap of two links are needed for single
insertion.
• We have discussed Insertion at first, at middle and at last in the singly
linked list.
Node creation
• For insertion first we have to create a new node and for creating a
new node we have to find whether required space is available or not.
• For doing so:
Say AVAIL=‘Pointer showing Available Space’
If(AVAIL==NULL)
Output: Space not available
Else : New node gets available space and AVAIL shifts to next
available free space if it exists.
‘Data’ and ‘Link’ part of New Node
• After space allocation to the New Node.
The ‘Data’ Part is given the desired value &
The ‘Link’ Part have initially NULL value.
After the insertion of node in the singly linked list the ‘Link’ part is
updated accordingly.
Insertion at First
For insertion at first the New Node should point to the first node
(before insertion) and start should point to the New Node.
For doing so:
New_Node -> Link = Start;
Start = New_node;
Insertion at Middle
For insertion at Middle the New Node should point to the node
next to it and the previous node should point to the New Node.
For doing so:
New_Node -> Link = Previous_node -> Link
Previous_node -> Link = New_node;
Insertion at Last
For Insertion at last the last node (before insertion) should point the
New Node and the ‘Link’ part of New Node must be NULL.
For doing so:
Last_Node -> Link = New_node;
Traversal
For Insertion at Middle and Insertion at Last we have to traverse till
previous and last node respectively.
To reach till desired Node we have to stop the traversal at some specific
point. To do this we have to ask user whether he wants insertion
before or after specific value OR before or after specific position.
Conditions
Condition to stop traversal at desired point
* Initialize Count with 1. # PTR =Pointer that is traversing.
Before given value (PTR -> Next) -> Data = ‘Value’ [#]
After given value PTR -> Data = ‘Value’ [#]
Before given position Count == ‘Position’ – 1 [*]
After given position Count == ‘Position’ [*]
Process Summary
(1) Create a New Node
(2) Apply values to the ‘Data’ and ‘Link’ part.
[Link part is NULL initially]
(3) Traverse in the linked list until we reach to the desired point.
(4) Insert Node using swaps of links.
Thank You.

Insertion in singly linked list

  • 1.
  • 2.
    Linked List • Whatis a Linked List? Linked List is a data structure. It is collection of nodes. Linked List is used widely because it do not need continue memory slot, because the data stored in linked list is accessed through ‘Links’.
  • 3.
    Singly Linked List Ina singly linked list node includes a ‘data’ part and a ‘link’ part. Node : Data: It includes any information that is to be stored. Link: It have the address of the next consecutive node. Data Link
  • 4.
    Insertion (1) Data Structuresare used so that a data can be easily modified and accessed. (2) This modification includes: Insertion, Updating of Value, Delete and some other special operations. (3) Insertion is needed in a data structure for adding a value or information while making it first time or it can also take place after it has been made once completely. That is why insertion may happen in any part of the data structure: At First, At Middle or At Last.
  • 5.
    Insertion in SinglyLinked List • Insertion in Singly Linked list is quite convenient in compare to array like structure. • In a structure like array a number of shifts may take place which is not at all acceptable. • In a Singly Linked List only swap of two links are needed for single insertion. • We have discussed Insertion at first, at middle and at last in the singly linked list.
  • 6.
    Node creation • Forinsertion first we have to create a new node and for creating a new node we have to find whether required space is available or not. • For doing so: Say AVAIL=‘Pointer showing Available Space’ If(AVAIL==NULL) Output: Space not available Else : New node gets available space and AVAIL shifts to next available free space if it exists.
  • 7.
    ‘Data’ and ‘Link’part of New Node • After space allocation to the New Node. The ‘Data’ Part is given the desired value & The ‘Link’ Part have initially NULL value. After the insertion of node in the singly linked list the ‘Link’ part is updated accordingly.
  • 8.
    Insertion at First Forinsertion at first the New Node should point to the first node (before insertion) and start should point to the New Node. For doing so: New_Node -> Link = Start; Start = New_node;
  • 9.
    Insertion at Middle Forinsertion at Middle the New Node should point to the node next to it and the previous node should point to the New Node. For doing so: New_Node -> Link = Previous_node -> Link Previous_node -> Link = New_node;
  • 10.
    Insertion at Last ForInsertion at last the last node (before insertion) should point the New Node and the ‘Link’ part of New Node must be NULL. For doing so: Last_Node -> Link = New_node;
  • 11.
    Traversal For Insertion atMiddle and Insertion at Last we have to traverse till previous and last node respectively. To reach till desired Node we have to stop the traversal at some specific point. To do this we have to ask user whether he wants insertion before or after specific value OR before or after specific position.
  • 12.
    Conditions Condition to stoptraversal at desired point * Initialize Count with 1. # PTR =Pointer that is traversing. Before given value (PTR -> Next) -> Data = ‘Value’ [#] After given value PTR -> Data = ‘Value’ [#] Before given position Count == ‘Position’ – 1 [*] After given position Count == ‘Position’ [*]
  • 13.
    Process Summary (1) Createa New Node (2) Apply values to the ‘Data’ and ‘Link’ part. [Link part is NULL initially] (3) Traverse in the linked list until we reach to the desired point. (4) Insert Node using swaps of links.
  • 14.