0% found this document useful (0 votes)
30 views18 pages

Delete, Search, Count A Node in Linked List

The document describes how to delete a node from a linked list. It discusses deleting the head node by making the next node the new head and freeing the memory of the old head. It also describes deleting non-head nodes by traversing the list to find the node before the target node, then adjusting its next pointer and freeing the target node's memory. Pseudocode and C code implementations are provided for deleting nodes from a linked list.

Uploaded by

TAMILSELVI T ECE
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)
30 views18 pages

Delete, Search, Count A Node in Linked List

The document describes how to delete a node from a linked list. It discusses deleting the head node by making the next node the new head and freeing the memory of the old head. It also describes deleting non-head nodes by traversing the list to find the node before the target node, then adjusting its next pointer and freeing the target node's memory. Pseudocode and C code implementations are provided for deleting nodes from a linked list.

Uploaded by

TAMILSELVI T ECE
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/ 18

Deleting a node in linked

list
Delete a node
 Delete a given node from the linked list.
 Example:

Linked List : 10 -> 20->  30 -> NULL

Input
Enter node to delete :20
Output
10->  30  NULL
Algorithm

1.If we want to delete the head node(i.e) first node


then the following steps to be follows
2.Enter the node (i.e) key to be delete
3. If the head node has the given key then
make the head node points to the second

node
free its memory.
1. Delete the Head node has the given key

void deleteNode(struct node **head, int key)


{
struct node *temp;
if(*head->data == key)
{
temp = *head;
head = (*head)->next;
free(temp);
}
}
Visual Representation
Let's delete data 10 (head node).
Explanation
 1. Make the head points to the next node.
 2. Free the head node's memory.
 3. Finally, the new linked list.
2. Delete the node which is present in
between or last
 Delete the node which is present in second or
between or in last of the linked list ,then the
following steps to be follows
 From the current node, check whether the

next node has the given key


     if yes, make the
 current->next = current->next->next
 free the memory.
     else
 update the current node to the next and do the above
process (from step 2) till the last node.
2. For other nodes (Non-Head)
Visual Representation
Let's delete data 20.
Explanation
 1. Make the current node points to the head node.
(current => data = 10).
 2. current => next. (current=>next=>data = 20).
 3. current => next => next.
(current=>next=>next=>data = 30).
 4. We have to remove the node 20.
Since current => next = 20 we can remove the
node by setting
current => next = current =>next => next. And
then free the node.
 5. Finally, the new linked list.
//Program to delete the node in the linked list
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/*create a structure named as Node */

struct Node
{
int data;
struct Node *next;

}*head=NULL;
int main() {
/* insert your code here */
int data,n,key;
void append(struct Node**,int);
void display(struct Node*);
void deleteNode(struct Node **,int);

printf("Enter the number of nodes:\n");


scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&data);
append(&head,data);
}
display(head);
printf("\nEnter the value to delete :");
scanf("%d",&key);
deleteNode(&head,key);
printf("After deletion the nodes are \n");
display(head);
return 0;
}
/* adds a node at the end of a linked list */
void append ( struct Node **head, int data )
{
//Fill in the code here
struct Node *newNode=malloc(sizeof(struct Node));
newNode->data=data;
newNode->next=NULL;
if(*head==NULL)
*head=newNode;
else
{
struct Node *lastNode=*head;
while(lastNode->next
!=NULL)
{
lastNode=lastNode->next;
}
lastNode->next=newNode;
}
}
void display ( struct Node *head )
{
//Fill in the code here
struct Node *temp=head;
printf("The elements in the linked list are ");
while(temp!=NULL)
{
printf("%d->",temp->data);
temp=temp->next;
}
printf("NULL\n");

}
void deleteNode(struct Node **head,int key)
{
struct Node *temp;
if((*head)->data ==key)
{
temp=*head;
*head=(*head)->next;
free(temp);
}
else
{
struct Node *current=*head;
while(current->next!=NULL)
{
if(current->next->data==key)
{
temp=current->next;
current->next=current->next->next;
free(temp);
break;
}
else
current=current->next;
}
}
Output
Enter the number of nodes:                                                                                      
                
4                                                                                                                              
 
10 20 30 40                                                                                                             
        
The elements in the linked list are 
10->20->30->40->NULL                                                                        
                                                                                                                                
Enter the value to delete :20                                                                                    
               
After deletion the nodes are                                                                                     
               
The elements in the linked list are 
10->30->40->NULL                                                                            
                                                     
Count the number of nodes in the
linked list
int count ( struct Node * head )
{
int count=0;
struct Node *temp=head;
while(temp!=NULL)
{
count+=1;
temp=temp->next;
}
return count;
}
Search a node in the linked list
int search ( struct Node *head, int key )
{
struct Node *temp=head;
int flag=0;
while(temp!=NULL)
{
if(temp->data==key)
{
flag=1;
break;
}
else
flag=0;
temp=temp->next;
}
return flag;
}

You might also like