0% found this document useful (0 votes)
135 views34 pages

Linkedlist New

The document discusses various operations on a singly linked list including inserting nodes at the beginning, end, and middle of the list as well as deleting nodes from the beginning, end, and middle of the list. It provides step-by-step explanations and algorithms for each operation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
135 views34 pages

Linkedlist New

The document discusses various operations on a singly linked list including inserting nodes at the beginning, end, and middle of the list as well as deleting nodes from the beginning, end, and middle of the list. It provides step-by-step explanations and algorithms for each operation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

Single Linked List

Insertion and Deletion


Insert a node at the beginning of
Singly linked list
Steps to insert node at the beginning
of singly linked list
Step 1: Create a new node, say newNode points to the newly
created node.
Steps to insert node at the beginning
of singly linked list
Step 2: Link the newly created node with the head node, i.e.
the newNode will now point to head node.
Steps to insert node at the beginning
of singly linked list
Step 3: Make the new node as the head node, i.e. now head
node will point to newNode.
Algorithm to insert node at the beginning of singly linked list

void insertNodeAtBeginning(int data)


{
struct node *newNode;
newNode = (struct node*)malloc(sizeof(struct node));
if(newNode == NULL)
{
printf("Unable to allocate memory.");
}
else
{
newNode->data = data; // Link data part
newNode->next = head; // Link address part
head = newNode; // Make newNode as first node
printf("DATA INSERTED SUCCESSFULLY\n");
}
}
Steps to insert node at the end of
Singly linked list
Step 1: Create a new node and make sure that the address part of the new node
points to NULL i.e. newNode->next=NULL
Steps to insert node at the end of
Singly linked list
Step 2: Traverse to the last node of the linked list and connect the last node of the
list with the new node, i.e. last node will now point to new node. (lastNode->next =
newNode).
Algorithm to insert node at the end of Singly linked list
void insertNodeAtEnd(int data)
{
struct node *newNode, *temp;
newNode = (struct node*)malloc(sizeof(struct node));
if(newNode == NULL)
{
printf("Unable to allocate memory.");
}
else
{
newNode->data = data;
newNode->next = NULL; // Link the data part
temp = head;
while(temp->next != NULL) // Traverse to the last node
{
temp = temp->next;
}
temp->next = newNode; // Link address part
printf("DATA INSERTED SUCCESSFULLY\n");
}
}
Steps to insert node at the middle of
Singly Linked List

Step 1: Create a new node.


Steps to insert node at the middle of
Singly Linked List

Step 2: Traverse to the n-1th position of the linked list and connect the new
node with the n+1th node. Means the new node should also point to the same
node that the n-1th node is pointing to. (newNode->next = temp-
>next where temp is the n-1th node).
Steps to insert node at the middle of
Singly Linked List

Step 3: Now at last connect the n-1th node with the new node i.e. the n-
1th node will now point to new node. (temp->next = newNode where temp is
the n-1th node).
Algorithm to insert node at the middle of Singly linked list

void insertNodeAtMiddle(int data, int position)


{
int i;
struct node *newNode, *temp;
newNode = (struct node*)malloc(sizeof(struct node));
if(newNode == NULL)
{
printf("Unable to allocate memory.");
}
else
{
newNode->data = data;
newNode->next = NULL;
temp = head;
Algorithm to insert node at the middle of Singly linked list
for(i=2; i<=position-1; i++)
{
temp = temp->next;
if(temp == NULL)
break;
}
if(temp != NULL)
{
newNode->next = temp->next;
temp->next = newNode;
printf("DATA INSERTED SUCCESSFULLY\n");
}
else
{
printf("UNABLE TO INSERT DATA AT THE GIVEN POSITION\n");
}}}
Delete first node of Singly Linked List
Steps to delete first node from Singly
Linked List
Step 1: Copy the address of first node i.e. head node to some temp variable
say toDelete.
Steps to delete first node from Singly
Linked List
Step 2: Move the head to the second node of the linked list i.e. head = head->next.
Steps to delete first node from Singly
Linked List
Step 3: Disconnect the connection of first node to second node.
Steps to delete first node from Singly
Linked List
Step 4: Free the memory occupied by the first node.
Algorithm to delete first node of Singly Linked List
void deleteFirstNode()
{
struct node *toDelete;
if(head == NULL)
{
printf("List is already empty.");
}
else
{
toDelete = head;
head = head->next;
printf("\nData deleted = %d\n", toDelete->data);
free(toDelete);
printf("SUCCESSFULLY DELETED FIRST NODE FROM LIST\n");
}
}
Delete last node of Singly Linked List
Steps to delete last node of a Singly
Linked List
Step 1: Traverse to the last node of the linked list keeping track of the second last
node in some temp variable say secondLastNode.
Steps to delete last node of a Singly
Linked List
Step 2: If the last node is the head node then make the head node as NULL else
disconnect the second last node with the last node i.e. secondLastNode->next =
NULL.
Steps to delete last node of a Singly
Linked List
Step 3: Free the memory occupied by the last node.
Algorithm to delete last node of Singly Linked List
void deleteLastNode()
{
struct node *toDelete, *secondLastNode;
if(head == NULL)
{
printf("List is already empty.");
}
else
{
toDelete = head;
secondLastNode = head;
while(toDelete->next != NULL)
{
secondLastNode = toDelete;
toDelete = toDelete->next;
}
Algorithm to delete last node of Singly Linked List
if(toDelete == head)
{
head = NULL;
}
else
{
secondLastNode->next = NULL;
}
free(toDelete);
printf("SUCCESSFULLY DELETED LAST NODE OF LIST\n");
}
}
Delete middle node of Singly Linked
List
Steps to delete middle node of Singly
Linked List
Step 1: Traverse to the nth node of the singly linked list and also keep reference
of n-1th node in some temp variable say prevnode.
Steps to delete middle node of Singly
Linked List
Step 2: Reconnect the n-1th node with the n+1th node i.e. prevNode->next =
toDelete->next (Where prevNode is n-1th node and toDelete node is the nth node
and toDelete->next is the n+1th node).
Steps to delete middle node of Singly
Linked List
Step 3: Free the memory occupied by the nth node i.e. toDelete node.
Algorithm to delete middle node of Singly Linked List
void deleteMiddleNode(int position) for(i=2; i<=position; i++)
{
int i; {
struct node *toDelete, *prevNode; prevNode = toDelete;
if(head == NULL)
toDelete = toDelete-
{
printf("List is already empty.");
>next;
} if(toDelete == NULL)
else
break;
{
toDelete = head; }
prevNode = head;
Algorithm to delete middle node of Singly Linked List
if(toDelete != NULL)
{
if(toDelete == head)
head = head->next;
prevNode->next = toDelete->next;
toDelete->next = NULL;
free(toDelete);
printf("SUCCESSFULLY DELETED NODE FROM MIDDLE OF
LIST\n");
}
else
{
printf("Invalid position unable to delete.");
}}}
Delete all nodes of Singly Linked List
Algorithm to delete all nodes of Singly
Linked List
void deleteList()
{
struct node *temp;
while(head != NULL)
{
temp = head;
head = head->next;
free(temp);
}
printf("SUCCESSFULLY DELETED ALL NODES OF LINKED
LIST\n");
}

You might also like