delete function in a singly linked list in c code example
Example 1: delete function in a singly linked list in c
// A complete working C program
// to demonstrate deletion in
// singly linked list
#include <stdio.h>
#include <stdlib.h>
// A linked list node
struct Node {
int data;
struct Node* next;
};
/* Given a reference (pointer to pointer) to the head of a
list and an int, inserts a new node on the front of the
list. */
void push(struct Node** head_ref, int new_data)
{
struct Node* new_node
= (struct Node*)malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
/* Given a reference (pointer to pointer) to the head of a
list and a key, deletes the first occurrence of key in
linked list */
void deleteNode(struct Node** head_ref, int key)
{
// Store head node
struct Node *temp = *head_ref, *prev;
// If head node itself holds the key to be deleted
if (temp != NULL && temp->data == key) {
*head_ref = temp->next; // Changed head
free(temp); // free old head
return;
}
// Search for the key to be deleted, keep track of the
// previous node as we need to change 'prev->next'
while (temp != NULL && temp->data != key) {
prev = temp;
temp = temp->next;
}
// If key was not present in linked list
if (temp == NULL)
return;
// Unlink the node from linked list
prev->next = temp->next;
free(temp); // Free memory
}
// This function prints contents of linked list starting
// from the given node
void printList(struct Node* node)
{
while (node != NULL) {
printf(" %d ", node->data);
node = node->next;
}
}
// Driver code
int main()
{
/* Start with the empty list */
struct Node* head = NULL;
push(&head, 7);
push(&head, 1);
push(&head, 3);
push(&head, 2);
puts("Created Linked List: ");
printList(head);
deleteNode(&head, 1);
puts("\nLinked List after Deletion of 1: ");
printList(head);
return 0;
}
Example 2: how to remove a node from a linked list in c
typedef struct node{
int value; //this is the value the node stores
struct node *next; //this is the node the current node points to. this is how the nodes link
}node;
node *rmvNode(node *head, int index){
node *tmp = head;
node *rmv;
int count = 0;
//base case for if the user enters a value greater then or equal to the length of the head
//base case for if the user enters a value with a list of length 1. because in this library a list MUST contain one value minimum
if(index >= len(head) || len(head) == 1){
return NULL;
}
//if you want to remove the first value
if(index == 0){
rmv = head; //stores the head at this given moment in time
head = tmp->next; //this jumps the position of the head making sure that the beginning is no longer part of the head
free(rmv); //this frees the memory given to the initial head
return head;
}
//if you want to remove index position 1
if(index == 1){
rmv = head->next;
head->next = tmp->next->next;
free(rmv);
return head;
}
//if you want to remove the last value
if(index == -1){
while(count < len(head)-2){ //we do -2 because we want to access the node before the last one
tmp = tmp->next;
count += 1;
}
rmv = tmp->next;
tmp->next = NULL;
free(rmv);
return head;
}
//remove anything else
while(count < index-1){
tmp = tmp->next;
count += 1;
}
rmv = tmp->next;
tmp->next = tmp->next->next;
free(rmv);
return head;
}
Example 3: program to create insert, delete and display operations on singly linked list in c
#include<conio.h>
#include<stdio.h>
struct node
{
int i;
struct node *next;
};
void main()
{
struct node *first;
struct node *last;
struct node *temp;
int ch,user,add,cnt=0,t=0;
struct node *p;
clrscr();
printf("\n\t 1.CREATION");
printf("\n\t 2.INSERT AT STARTING");
printf("\n\t 3.INSERT AT MIDDLE(USER'S CHOICE)");
printf("\n\t 4.INSERT AT END");
printf("\n\t 5.DELETE 1ST NODE");
printf("\n\t 6.DELETE LAST NODE");
printf("\n\t 7.DELETE MIDDLE NODE(USER'S CHOICE)");
printf("\n\t 8.DISPLAY");
printf("\n\t 10.EXIT");
scanf("%d",&user);
while(user!=10)
{
if(user==1)
{
printf("\n\t ENTER DATA ::: ");
first=(struct node*)malloc(sizeof(struct node));
scanf("%d",&ch);
first->i=ch;
first->next=0;
p=first;
cnt=1;
}
if(user==2)
{
p=(struct node*)malloc(sizeof(struct node));
printf("\n\t ENTER DATA FOR 1ST NODE");
scanf("%d",&p->i);
p->next=first;
first=p;
cnt++;
}
if(user==4)
{
p=(struct node*)malloc(sizeof(struct node));
printf("\n\t ENTER DATA FOR LAST NODE");
scanf("%d",&p->i);
temp=first;
while(temp->next!=0)
{
temp=temp->next;
}
temp->next=p;
p->next=0;
cnt++;
}
if(user==3)
{
printf("\n\t ENTER ANY ADDRESS BETWEEN 1 and %d",cnt);
scanf("%d",&add);
t=1;
p=first;
while(t!=add)
{
p=p->next;
t++;
}
temp=(struct node*)malloc(sizeof(struct node));
printf("\n\t ENTER DATA FOR NODE");
scanf("%d",&temp->i);
temp->next=p->next;
p->next=temp;
cnt++;
}
if(user==5)
{
p=first;
first=p->next;
free(p);
}
if(user==6)
{
p=first;
while(p->next->next!=0)
{
p=p->next;
}
p->next=0;
free(p->next->next);
}
if(user==8)
{
p=first;
while(p!=0)
{
printf("\n\t %d",p->i);
p=p->next;
}
}
if(user==7)
{
printf("\n\t ENTER ANY ADDRESS BETWEEN 1 and %d",cnt);
scanf("%d",&add);
t=1;
p=first;
while(t<add-1)
{
p=p->next;
t++;
}
temp=p->next;
p->next=temp->next;
free(temp);
cnt--;
}
printf("\n\t 1.CREATION");
printf("\n\t 2.INSERT AT STARTING");
printf("\n\t 3.INSERT AT MIDDLE(USER'S CHOICE)");
printf("\n\t 4.INSERT AT END");
printf("\n\t 5.DELETE 1ST NODE");
printf("\n\t 6.DELETE LAST NODE");
printf("\n\t 7.DELETE MIDDLE NODE(USER'S CHOICE)");
printf("\n\t 8.DISPLAY");
printf("\n\t 10.EXIT");
scanf("%d",&user);
}
getch();
}