0% found this document useful (0 votes)
13 views

Insertion and Deletion Linked List

The document describes how to insert and delete nodes from a singly linked list in different positions: To insert at the beginning, allocate a new node, set its next pointer to the current head, and change the head to point to the new node. To insert in the middle, traverse to the node before the target position, allocate a new node, and adjust next pointers to include it between nodes. To insert at the end, traverse to the last node, allocate a new node, set the last node's next pointer to the new node, and set the new node's next pointer to null. To delete from the beginning, point head to the second node and free the first. To delete

Uploaded by

muzzammil4422
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Insertion and Deletion Linked List

The document describes how to insert and delete nodes from a singly linked list in different positions: To insert at the beginning, allocate a new node, set its next pointer to the current head, and change the head to point to the new node. To insert in the middle, traverse to the node before the target position, allocate a new node, and adjust next pointers to include it between nodes. To insert at the end, traverse to the last node, allocate a new node, set the last node's next pointer to the new node, and set the new node's next pointer to null. To delete from the beginning, point head to the second node and free the first. To delete

Uploaded by

muzzammil4422
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

creation struct node {

int data;
struct node *next;
}
Insert at the beginning struct node {
int data;
struct node *next;
 Allocate memory for new node
}
 Store data Struct node *head,*newnode;
 Change next of new node to Newnode=(structnode *)malloc(size of(structnode))
Printf(“Enter the data to store”);
point to head Scanf(“%d”,&newnode->data);
 Change head to point to recently Newnode->next=head;
head=newnode;
created node

Insert at the Middle Int pos;


struct node {
int data;
 Allocate memory and store data
struct node *next;
for new node }
 Traverse to node just before the struct node *head,*newnode,*temp;
newnode=(structnode *)malloc(size of(struct node))
required position of new node Printf(“Enter the pos”);
 Change next pointers to include Scanf(“%d”,&pos);
While(i<pos)
new node in between {
temp=temp->next;
i++;
}
Insert at the End struct node {
int data;
struct node *next;
 Allocate memory for new node
}
 Store data struct node *head,*newnode,*temp;
 Traverse to last node newnode=(structnode *)malloc(size of(struct node))
Printf(“Enter the data to store”);
 Change next of last node to Scanf(“%d”,&newnode->data);
recently created node newnode->next=0;
temp=head;
while(temp->next!0)
{
temp=temp->next;
}
Temp->next=newnode;
Delete from beginning Point head to the next node i.e. second node
temp = head
Point head to the second node head = head->next

Make sure to free unused memory


free(temp); or delete temp;
Delete from middle Keeps track of pointer before node to delete and pointer
to node to delete
 Traverse to element before
temp = head;
the element to be deleted prev = head;
 Change next pointers to for(int i = 0; i < position; i++)
{
exclude the node from the if(i == 0 && position == 1)
chain head = head->next;
free(temp)
else
{
if (i == position - 1 && temp)
{
prev->next = temp->next;
free(temp);
}
else
{
prev = temp;
if(prev == NULL) // position was greater than
number of nodes in the list
break;
temp = temp->next;
}}}
Delete from end Point head to the previous element i.e. last second
element
Change next pointer to null
 Traverse to second last
struct node *end = head;
element struct node *prev = NULL;
 Change its next pointer to null while(end->next)
{
prev = end;
end = end->next;
}
prev->next = NULL;
Make sure to free unused memory
free(end); or delete end;

You might also like