C program to delete node from single linked list when just address of the previous node to be deleted is given
shiv@ubuntu:~/ds/list$ cat deln.c
/*
* C Program to delete node in the single linked list, when address of the
*previous node to be deleted is given
*/
#include <stdio.h>
#include <stdlib.h>
struct list {
int d;
struct list *next;
};
/*
* function to delete node when just address of the node to be
* deleted is given in the single linked list
*/
void deletenode(void **ptr)
{
struct list **temp, *del;
}
void display(struct list *temp)
{
printf("Elements in list are\n");
while(temp != NULL){
printf("ADDR %p data %d next %p\n", temp, temp->d, temp->next);
temp=temp->next;
}
}
int main()
{
struct list *head=NULL, *temp;
void *ptr;
/* testing program by creating list from below array */
int a[]={1 ,3 ,4,5,6,7,8,9};
int i;
for(i=0;i<8; i++)
{
temp=malloc(sizeof(struct list));
if(!temp){
return;
}
temp->d = a[i];
temp->next = NULL;
if(head == NULL){
head = temp;
} else {
temp->next = head;
head = temp;
}
}
display(head); /* display list before deleting */
printf("Enter address of previous node to be deleted\n");
scanf("%p", &ptr);
deletenode(&ptr);
display(head); /* display list after delete */
}
/* end of the program */
/*
* C Program to delete node in the single linked list, when address of the
*previous node to be deleted is given
*/
#include <stdio.h>
#include <stdlib.h>
struct list {
int d;
struct list *next;
};
/*
* function to delete node when just address of the node to be
* deleted is given in the single linked list
*/
void deletenode(void **ptr)
{
struct list **temp, *del;
if(*ptr == NULL) {
printf("Invalid pointer\n");
return;
}
temp= (struct list **)ptr;
printf("The address of previous node of the node to be deleted is %p data %d\n", *temp, (*temp)->d);
if((*temp)->next && (*temp)->next->next){
/* any node apart from node before last or last node in the list */
(*temp)->d = (*temp)->next->d;
del = (*temp)->next;
(*temp)->next = (*temp)->next->next;
free(del);
} else if((*temp)->next) {
/* This is node before last node */
(*temp)->d = (*temp)->next->d;
free((*temp)->next);
(*temp)->next = NULL;
} else {
/* this is last node we can't delete from it's previous node next pointer
* but we can free it and make it NULL, really we can't delete last node
* it is not possible to remove
*/
free(*temp);
(*temp)=NULL;
}}
void display(struct list *temp)
{
printf("Elements in list are\n");
while(temp != NULL){
printf("ADDR %p data %d next %p\n", temp, temp->d, temp->next);
temp=temp->next;
}
}
int main()
{
struct list *head=NULL, *temp;
void *ptr;
/* testing program by creating list from below array */
int a[]={1 ,3 ,4,5,6,7,8,9};
int i;
for(i=0;i<8; i++)
{
temp=malloc(sizeof(struct list));
if(!temp){
return;
}
temp->d = a[i];
temp->next = NULL;
if(head == NULL){
head = temp;
} else {
temp->next = head;
head = temp;
}
}
display(head); /* display list before deleting */
printf("Enter address of previous node to be deleted\n");
scanf("%p", &ptr);
deletenode(&ptr);
display(head); /* display list after delete */
}
/* end of the program */
Comments
Post a Comment