0% found this document useful (0 votes)
5 views5 pages

Singly Link List Insertion

Ds aktu unit 1

Uploaded by

championroyal361
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views5 pages

Singly Link List Insertion

Ds aktu unit 1

Uploaded by

championroyal361
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

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

Step 1: IF PTR = NULL


Write OVERFLOW
Go to Step 7
[END OF IF]
Step 3: SET NEW_NODE = PTR
Step 4 NEW_NODE → INFO = ITEM
Step 5: : SET NEW_NODE → NEXT = HEAD
Step 6: SET HEAD = NEW_NODE
Step 7: EXIT

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. } }

Inserting a node at the end:


The following steps are followed to insert a new node at the end 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:
temp = star t ;
while(temp -> next != NULL)
temp = temp -> next;
temp -> next = newnode;

INSERTEND(INFO, NEXT, HEAD,ITEM)

Step 1: IF PTR = NULL


Write OVERFLOW
Go to Step 7
[END OF IF]
Step 2: SET NEW_NODE = PTR
Step 3 NEW_NODE → INFO = ITEM
Step 4: : SET NEW_NODE → NEXT = NULL
Step 5: SET P =HEAD
Step 6: Repeat step 7 while P→NEXT!= NULL
Step 7: SET P=P→ NEXT
[END OF LOOP]
Step 8: SET P - > NEXT = NEW_NODE
Step 9: EXIT

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. }

Inserting a node at ANY position:


The following steps are followed, to insert a new node in an intermediate position in the list:
➢ Get the new node using malloc().
➢ Ensure that the specified position is in between first node and last node. If not,
specified position is invalid..
➢ Store the starting address (which is in start pointer ) in temp and prev pointers . Then
traverse the temp pointer upto the specified position followed by prev pointer .
➢ After reaching the specified position, follow the steps given below:
prev -> next = newnode;
newnode -> next = temp;
➢ Let the intermediate position be 3.
.
Figure shows inserting a node into the single linked list at a specified intermediate position
other than beginning and end.

INSERTATLOC(INFO, NEXT, HEAD,ITEM, LOC)

STEP 1: IF PTR = NULL


WRITE OVERFLOW
GOTO STEP 12
[ END OF IF]
STEP 2: SET NEW_NODE = PTR
STEP 3: NEW_NODE → INFO = ITEM
STEP 4: SET TEMP = HEAD
STEP 5: SET I = 0
STEP 6: REPEAT STEP 5 AND 6 UNTIL I<loc
STEP 7: TEMP = TEMP → NEXT
STEP 8: IF TEMP = NULL
WRITE "DESIRED NODE NOT PRESENT"
GOTO STEP 12
END OF IF
END OF LOOP
STEP 9: NEW_NODE → NEXT = TEMP → NEXT
STEP 10: TEMP → NEXT = NEW_NODE
STEP 11: EXIT

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. }
}

You might also like