This is a program to delete a particular node in single linked list without knowing the previous node. The logic for deleting a node in a single linked list is very simple. Iterate till the previous node of the target node. So we will break the loop when node->next = target node. Now get the next node to the target and update the next pointer.

 void delete_nod(node *target)
{
  if(target == NULL || head == NULL) return;
  prev = nULL:
  while (node && node != target) {
    prev = node;
    node = node->next;
  }
  if(prev) {
10      prev->next = target->next;
11    } else {
12    head = head->next;
13    }
14    free(target);
15  }
16 

But the case is different here. We have no way to know the previous node as this is a single linked list. So we used indirect technique. We take a node and goto the next. We have two pointer current and next. We can copy content of next to current. Actually all the content of next node copied to current and thus next node becomes a redundant one. So we can delete it. Below diagram shows how it is happening.

delete a particular-node of single linked list

Source Code

#include <stdio.h>
#include <stdlib.h>
typedef struct _node {
  int value;
  struct node *next;
}node;
int main(int argc, char *argv[])
10  {
11    int i;
12    node *n[10];
13    node *tmp, *del;
14    /* Construct a linked list of 10 nodes */
15    for (= 0; i < 10; i++)
16    {
17      n[i] = (node *) malloc (sizeof(node));
18      n[i]->value = i + 1;
19      n[i]->next = NULL;
20      if (> 0) {
21            n[-1]->next = n[i];
22      }    
23    }
24    tmp = n[0];
25    printf ("Linked List\n");
26    while (tmp)
27    {
28      printf ("Node %p Value %d\n", tmp, tmp->value);
29      tmp = (node *)tmp->next;
30    }
31    printf ("Enter a node to delete :");
32    scanf ("%p", &tmp);
33    if (tmp && tmp->next) {
34      printf ("Deleting %p, value %d\n", tmp, tmp->value);
35      del = (node *)tmp->next;
36      *tmp = *del;
37      free(del);
38    } else {
39      printf ("Either last node or value is NULL, not deleted!\n"); 
40    }
41    printf ("Linked List\n");
42    tmp = n[0];
43    while(tmp)
44    {
45      printf ("Node %p Value %d\n", tmp, tmp->value);
46      tmp = tmp->next;
47    }
48    return 0;
49  }

Output

Linked List
Node 00471880 Value 1
Node 004718A0 Value 2
Node 004718B0 Value 3
Node 00478DB8 Value 4
Node 00478DC8 Value 5
Node 00478DD8 Value 6
Node 00478DE8 Value 7
Node 00478DF8 Value 8
Node 00478E08 Value 9
Node 00478E18 Value 10
Enter a node to delete :00478DE8
Deleting 00478DE8, value 7
Linked List
Node 00471880 Value 1
Node 004718A0 Value 2
Node 004718B0 Value 3
Node 00478DB8 Value 4
Node 00478DC8 Value 5
Node 00478DD8 Value 6
Node 00478DE8 Value 8
Node 00478E08 Value 9
Node 00478E18 Value 10

Linked List
Node 00031880 Value 1
Node 000318A0 Value 2
Node 000318B0 Value 3
Node 00038DB8 Value 4
Node 00038DC8 Value 5
Node 00038DD8 Value 6
Node 00038DE8 Value 7
Node 00038DF8 Value 8
Node 00038E08 Value 9
Node 00038E18 Value 10
Enter a node to delete :00038E18
Either last node or value is NULL, not deleted!
Linked List
Node 00031880 Value 1
Node 000318A0 Value 2
Node 000318B0 Value 3
Node 00038DB8 Value 4
Node 00038DC8 Value 5
Node 00038DD8 Value 6
Node 00038DE8 Value 7
Node 00038DF8 Value 8
Node 00038E08 Value 9
Node 00038E18 Value 10

You have viewed 1 page out of 248. Your C learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

Doubly Linked list, Circular Linked list, Doubly Circular Linked list, Queue, reverse single linked list, delete single linked list, Stack, Tree and Binary tree, Binary tree,

# C Programming Language (Prentice Hall Software)
# Let Us C Paperback - 2006 by Yashavant Kanetkar
# Understanding and Using C Pointers Core techniques for memory management
# Data Structures Using C and C++ Paperback - 1998
# Data Structures In C Paperback - August 11, 2008 by Noel Kalicharan