Singly Link List Insertion
Singly Link List Insertion
Creation of a node:
Creating a singly linked list starts with creating a node. Sufficient memory has to be allocated
for creating a node. The information is stored in the memory, allocated by using the malloc()
function. after allocating memory for the structure of type node, the information for the item
(i.e., data) has to be read from the user, set next field to NULL and finally returns the address
of the node.
Creating a Singly Linked List with ‘n’ number of nodes:
The following steps are to be followed to create ‘n’ number of nodes:
• Get the new node using malloc();
• If the list is empty, assign new node as start. start = newnode;
• If the list is not empty, follow the steps given below:
• The next field of the new node is made to point the first node (i.e. start node) in the list by
assigning the address of the first node.
• The start pointer is made to point the new node by assigning the address of the new node.
• Repeat the above steps ‘n’ times.
Figure shows 4 items in a single linked list stored at different locations in memory.
Insertion of a Node:
One of the most primitive operations that can be done in a singly linked list is the insertion of
a node. Memory is to be allocated for the new node (in a similar way that is done while creating
a list) before reading the data. The new node will contain empty data field and empty next field.
The data field of the new node is then stored with the information read from the user. The next
field of the new node is assigned to NULL. The new node can then be inserted at three different
places namely:
➢ Inserting a node at the beginning.
➢ Inserting a node at the end.
➢ Inserting a node at intermediate position.
Inserting a node at the beginning:
The following steps are to be followed to insert a new node at the beginning of the list:
➢ Get the new node using malloc().
➢ If the list is empty then start = newnode.
➢ If the list is not empty, follow the steps given below:
newnode -> next = start;
start = newnode;
Figure shows inserting a node into the single linked list at the beginning.
Algorithm: INSFIRSTBEG (INFO, NEXT, HEAD,ITEM)
This algorithm inserts ITEM as the first node in the list
Program
void beginsert()
1. {
2. struct node *ptr;
3. int item;
4. ptr = (struct node *) malloc(sizeof(struct node *));
5. if(ptr == NULL)
6. {
7. printf("\nOVERFLOW");
8. }
9. else
10. {
11. printf("\nEnter value\n");
12. scanf("%d",&item);
13. ptr->data = item;
14. ptr->next = head;
15. head = ptr;
16. printf("\nNode inserted");
17. } }
Figure shows inserting a node into the single linked list at the end.
Program:
void lastinsert()
1. {
2. struct node *ptr,*temp;
3. int item;
4. ptr = (struct node*)malloc(sizeof(struct node));
5. if(ptr == NULL)
6. {
7. printf("\nOVERFLOW");
8. }
9. else
10. {
11. printf("\nEnter value?\n");
12. scanf("%d",&item);
13. ptr->data = item;
14. if(head == NULL)
15. {
16. ptr -> next = NULL;
17. head = ptr;
18. printf("\nNode inserted");
19. }
20. else
21. {
22. temp = head;
23. while (temp -> next != NULL)
24. {
25. temp = temp -> next;
26. }
27. temp->next = ptr;
28. ptr->next = NULL;
29. printf("\nNode inserted");
30.
31. }
32. }
33. }
Program:
void randominsert()
1. {
2. int i,loc,item;
3. struct node *ptr, *temp;
4. ptr = (struct node *) malloc (sizeof(struct node));
5. if(ptr == NULL)
6. {
7. printf("\nOVERFLOW");
8. }
9. else
10. {
11. printf("\nEnter element value");
12. scanf("%d",&item);
13. ptr->data = item;
14. printf("\nEnter the location after which you want to insert ");
15. scanf("\n%d",&loc);
16. temp=head;
17. for(i=0;i<loc;i++)
18. {
19. temp = temp->next;
20. if(temp == NULL)
21. {
22. printf("\ncan't insert\n");
23. return;
24. }
25. }
26. ptr ->next = temp ->next;
27. temp ->next = ptr;
28. printf("\nNode inserted");
29. }
}