delete function in a singly linked list in c

Solutions on MaxInterview for delete function in a singly linked list in c by the best coders in the world

showing results for - "delete function in a singly linked list in c"
Eireann
14 Sep 2019
1// A complete working C program
2// to demonstrate deletion in
3// singly linked list
4#include <stdio.h>
5#include <stdlib.h>
6 
7// A linked list node
8struct Node {
9    int data;
10    struct Node* next;
11};
12 
13/* Given a reference (pointer to pointer) to the head of a
14   list and an int, inserts a new node on the front of the
15   list. */
16void push(struct Node** head_ref, int new_data)
17{
18    struct Node* new_node
19        = (struct Node*)malloc(sizeof(struct Node));
20    new_node->data = new_data;
21    new_node->next = (*head_ref);
22    (*head_ref) = new_node;
23}
24 
25/* Given a reference (pointer to pointer) to the head of a
26   list and a key, deletes the first occurrence of key in
27   linked list */
28void deleteNode(struct Node** head_ref, int key)
29{
30    // Store head node
31    struct Node *temp = *head_ref, *prev;
32 
33    // If head node itself holds the key to be deleted
34    if (temp != NULL && temp->data == key) {
35        *head_ref = temp->next; // Changed head
36        free(temp); // free old head
37        return;
38    }
39 
40    // Search for the key to be deleted, keep track of the
41    // previous node as we need to change 'prev->next'
42    while (temp != NULL && temp->data != key) {
43        prev = temp;
44        temp = temp->next;
45    }
46 
47    // If key was not present in linked list
48    if (temp == NULL)
49        return;
50 
51    // Unlink the node from linked list
52    prev->next = temp->next;
53 
54    free(temp); // Free memory
55}
56 
57// This function prints contents of linked list starting
58// from the given node
59void printList(struct Node* node)
60{
61    while (node != NULL) {
62        printf(" %d ", node->data);
63        node = node->next;
64    }
65}
66 
67// Driver code
68int main()
69{
70    /* Start with the empty list */
71    struct Node* head = NULL;
72 
73    push(&head, 7);
74    push(&head, 1);
75    push(&head, 3);
76    push(&head, 2);
77 
78    puts("Created Linked List: ");
79    printList(head);
80    deleteNode(&head, 1);
81    puts("\nLinked List after Deletion of 1: ");
82    printList(head);
83    return 0;
84}
Roderick
15 Jan 2018
1#include<conio.h>
2#include<stdio.h>
3
4struct node
5{
6    int i;
7    struct node *next;
8};
9
10void main()
11{
12
13    struct node *first;
14    struct node *last;
15
16    struct node *temp;
17    int ch,user,add,cnt=0,t=0;
18    struct node *p;
19
20    clrscr();
21
22    printf("\n\t 1.CREATION");
23    printf("\n\t 2.INSERT AT STARTING");
24    printf("\n\t 3.INSERT AT MIDDLE(USER'S CHOICE)");
25    printf("\n\t 4.INSERT AT END");
26    printf("\n\t 5.DELETE 1ST NODE");
27    printf("\n\t 6.DELETE LAST NODE");
28    printf("\n\t 7.DELETE MIDDLE NODE(USER'S CHOICE)");
29    printf("\n\t 8.DISPLAY");
30    printf("\n\t 10.EXIT");
31    scanf("%d",&user);
32
33    while(user!=10)
34    {
35      if(user==1)
36      {
37        printf("\n\t ENTER DATA  ::: ");
38        first=(struct node*)malloc(sizeof(struct node));
39        scanf("%d",&ch);
40
41        first->i=ch;
42        first->next=0;
43        p=first;
44        cnt=1;
45      }
46      if(user==2)
47      {
48            p=(struct node*)malloc(sizeof(struct node));
49            printf("\n\t ENTER DATA FOR 1ST NODE");
50            scanf("%d",&p->i);
51            p->next=first;
52            first=p;
53            cnt++;
54      }
55      if(user==4)
56      {
57            p=(struct node*)malloc(sizeof(struct node));
58            printf("\n\t ENTER DATA FOR LAST NODE");
59            scanf("%d",&p->i);
60            temp=first;
61            while(temp->next!=0)
62            {
63                temp=temp->next;
64            }
65            temp->next=p;
66            p->next=0;
67            cnt++;
68      }
69      if(user==3)
70      {
71            printf("\n\t ENTER ANY ADDRESS BETWEEN 1 and %d",cnt);
72            scanf("%d",&add);
73
74            t=1;
75            p=first;
76            while(t!=add)
77            {
78                p=p->next;
79                t++;
80            }
81            temp=(struct node*)malloc(sizeof(struct node));
82            printf("\n\t ENTER DATA FOR NODE");
83            scanf("%d",&temp->i);
84            temp->next=p->next;
85            p->next=temp;
86            cnt++;
87      }
88      if(user==5)
89      {
90            p=first;
91            first=p->next;
92            free(p);
93      }
94      if(user==6)
95      {
96        p=first;
97        while(p->next->next!=0)
98        {
99            p=p->next;
100        }
101        p->next=0;
102        free(p->next->next);
103      }
104      if(user==8)
105      {
106        p=first;
107        while(p!=0)
108        {
109            printf("\n\t %d",p->i);
110            p=p->next;
111        }
112      }
113      if(user==7)
114      {
115            printf("\n\t ENTER ANY ADDRESS BETWEEN 1 and %d",cnt);
116            scanf("%d",&add);
117
118            t=1;
119            p=first;
120            while(t<add-1)
121            {
122                p=p->next;
123                t++;
124            }
125            temp=p->next;
126            p->next=temp->next;
127            free(temp);
128            cnt--;
129
130      }
131      printf("\n\t 1.CREATION");
132      printf("\n\t 2.INSERT AT STARTING");
133      printf("\n\t 3.INSERT AT MIDDLE(USER'S CHOICE)");
134      printf("\n\t 4.INSERT AT END");
135      printf("\n\t 5.DELETE 1ST NODE");
136      printf("\n\t 6.DELETE LAST NODE");
137      printf("\n\t 7.DELETE MIDDLE NODE(USER'S CHOICE)");
138      printf("\n\t 8.DISPLAY");
139      printf("\n\t 10.EXIT");
140      scanf("%d",&user);
141       }
142       getch();
143}
Filippo
08 Aug 2016
1typedef struct node{
2    int value; //this is the value the node stores
3    struct node *next; //this is the node the current node points to. this is how the nodes link
4}node;
5
6node *rmvNode(node *head, int index){
7    node *tmp = head;
8    node *rmv;
9    int count = 0;
10
11    //base case for if the user enters a value greater then or equal to the length of the head
12    //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
13    if(index >= len(head) || len(head) == 1){
14        return NULL;
15    }
16  
17    //if you want to remove the first value
18    if(index == 0){
19        rmv = head; //stores the head at this given moment in time
20        head = tmp->next; //this jumps the position of the head making sure that the beginning is no longer part of the head
21        free(rmv); //this frees the memory given to the initial head
22        return head;
23    }
24
25    //if you want to remove index position 1
26    if(index == 1){
27        rmv = head->next;
28        head->next = tmp->next->next;
29        free(rmv);
30        return head;
31    }
32
33    //if you want to remove the last value
34    if(index == -1){
35        
36        while(count < len(head)-2){ //we do -2 because we want to access the node before the last one
37            tmp = tmp->next;
38            count += 1;
39        }
40        rmv = tmp->next;
41        tmp->next = NULL;
42        free(rmv);
43        return head;
44    }
45
46    //remove anything else
47    while(count < index-1){
48        tmp = tmp->next;
49        count += 1;
50    }
51    rmv = tmp->next;
52    tmp->next = tmp->next->next;
53    free(rmv);
54    return head;
55
56}
queries leading to this page
remove new node to linked list in chow to delete a node in a linked list c 2b 2bhow to delete linked list in chow to delete singly linked listdelete by value in linked listdelete node linked listdelete an element in linked listdelete node from linkedlist in c 2b 2bdeleting a node in a linked list in cdeleting a node in linkedlistremoving a specific element in singly linked list chow to delete a linked list in cdeleting a node in single linked list in chow to remove a member of a linked list cprogram to create insert 2c delete and display operations on singly linked list in c 2b 2bsteps to delete a node from linked list algorithmlinked list deletiion in clinked list delete node in cwe can easily remove elements from linked lists cpython remove nodehow to remove a node from a linked list in cfunction to delete a linked list in cdelete node in linked list c 2b 2bdeleting a node in linked list pythondelete node from linked listcode to delete a node in linked listvoid remove linked listremove one node and add one nodedelete array of nodes in linked list cdelete node in linked list in pythondelete node at beginning of linked list clinked list in c insert deletehow delete element from linked listhow to delete a nodespython delete node from listremove a node from a linked list cdelete operation in linkedlistsdelete an element from a linked listdelete a node in linked list c 2b 2bdelete a particular node in linked list in cfunction that deletes the head node c languagedeletion of nodes in linked listdelete node from linked list in chow to remove a node from a singly linked list in c 2b 2bdelete node in linked list clinked list deletion easyremove from singly linked listhow to delete node in singly linked listnode removal in cc linked list remove node delete from a linked list cinserting and deleting elements in a sorted singly linked list cdelete node linkedlistdeleting an element from a linked listdeletion opreration from singly linked list in cc how to remove an item from linked listdelete linked list cdeleting node from linklistdedelete node from linked list c 2b 2bremove a node from a linked list c 2b 2bprogram to delete a node from a linked list in cdeletion linked list in cremoving the head node from the linked listlinked list delete nodelinked list c delete listc program to delete element in linked listsingly linked list insertion and deletion program in cfonction to delete eleme 2cnt from linked listc remove specific element from linked listlinked list deletionhow to remove a node in linked listdelete node from linked list pythondelete a node in singly linked list in csingly linked list deletiondeleting in linked list c jennydeletion in singly linked listprogram for inserting 2fdeleting nodes into 2ffrom a linked list c how to remove elements from linked listdelete node from list which are also present in another give listprogram to create insert delete and display operations on singly linked list in c 2b 2bhow to delete a node from a linked listdelete a node from linked list in c 2b 2bhow to deletemostrecent node in a linked list in c 2b 2bremoving a node in cdelete a node in linked listhow to delete a linked list node in c 2b 2bdelete a node from linked list in c clean codehow to delete a node in a linked list in cremove a node in linked list cimplement linked list in c for insertion and deletionhow to delete a node in a linked list in c if you dont know hwere it isdelete element from linked list 2bdelete a node from linked list pythonlinked list removal edspresohow to delete a node in linked list in cremove a nodec program to delete a node in linked listremoving a node from a linked list c 2b 2bdelete function in linked listdelete for single link list javaremove element from linked list pythonfor delete elements from headdelete node in signly linked list pythonlist delete a nodefunction to delete a node in linked list in cdeleting an element from singly linked listdeleting a node in linked list in c 2b 2bc program to delete node in linked listlinked list delete node clinked list delete methodlinked list how to remove a node traversedelete the node from anywhere from the list in java delete node from list which occur in second list nodedelete the node with infocode to remove singly 3binked l 3bistdeletion in linked listdelete a node linked list after search in cdelete element from linked list c 2b 2b for geeksdelete a node from linked list in cdeleting a node of doubly linked cppdelete linked list node at index in cdelete node pythondeletion from linked listlinked list return front and removedeletion of linked listdelete a node at index linked list in cremove node inlisthow to delete a node in linked listremove a node and add it to a linked list c 2b 2bhow to delete a linked list cdelete after a node in linked list in cremove from linked list chow to delete the node in linked listdelete linked list element by valuehow to delete from linked list in cdelete a singly linked list in clinked list delete node c 2b 2bhow to delete a node in a linked list pythondeletion in singly linked list c 2b 2blinked list removalhow to delete nodein c how do you remove an element from a linked listhow to remove element from linked list pythondelete function in a singly linked list in csingly linked list deleteremove a node in linked list chow to delete from linked listbest way to remove a link from a linked list in cdelete a element from linked listcode for deleting a node in linked listdelete node in signly linked listdelete node from linked list cremove a node from linked list c 2b 2bfunction to remove any element in linked listdelete all from a linked list in clinked list remove element cremove a node from linked listhow to remove from a link listdelete item from linked listdelete a node from linked list cwhen some node is related with the linked list what happens with memorylink list addition deletionremove nodes from linked list that have values that have previously occured in linked list pythondelete a specific node in linked list javahow to remove a node from a linked list pythondelete a node in a doubly linked list c languagewhen some node is deleted from the linkedlist what happens with memory which was allocated to that node 3fdoubly linked list in c delete nodelinked list program in c for insertion and deletiondelete node from link list in chow to remove a node from a linked listsimple linked list delete in chow to delete linked list node in cintnode delete numberdelete a node in a doubly linked list in cnode key and linkedlistlinked list delete javadelete elements from linked list until sorteddelete node in a linked list 27delete node from doubly linked list in chow to delete a node in linked list cpython delete linked listdelete entire linked list in cdeleting a node in linked listremoving an element from a linked list clinked lists deleteing nodeshow to remove 28 29 from a link listwhere can we remove a node from a linked listwrite a c program to delete a node at middle from a singly linked listhow to remove a node from a linked list in c exampledelete nodehow to delete a node in linked list c 2b 2bdelete node in linked list javaremove a node at a particular node in a linked list c 2b 2bdeleting a node in linked list in ctwo way linked list with insert and delete in cdelete before a node from linked listdelete node javadelete a node after a given node in linked list queuedelete a given node in linked listvoid delete for single linked listhow to delete the main node in linked list cdeleting a node in ll in pythondelete a given node when a node is given pythondelete node in a linked listdelete element from linked list in cdelete function of linked list where do we remove a node from a linked lsit c 2b 2bdeleting a node in a linked list cdelete node in a linked list chow to delete a node from doubly linked list in c stackhow to delete linked listelanguage cdelete node in linked list program in clinked list delete programhow to delete a node in a linked list clinked list in c remove noderemoving a element from a linked listsingly linked list remove nodelinked list to add and delete nodehow to remove item from linked list in chow to delete node in linked list in c 2b 2bdelete in doubly linked list cdeleting a node in linked list c 2b 2bwrite a function to delete the first instance of a node in a singly linked list delete a node in linkremove specific node from linked list c 2b 2bc linked list delete nodehow to remove an index of a linked list c 2b 2bc function that removers teh kth node in linked list using cdeleting a node in linked list tutorialhow to delete a node from a linked list in cdeleted element from linked listc delete element from linked listhow to delete a node from a linked list c 2b 2bdeleting node with value from doubly linked list in cdelete node in linked listdelete node in linked list program in c faceprepdelete node from linked list with specific valuedeletion of node in doubly linked list in cc program to delete an element in a linked listdelete a node in a singly linked listlinked list deletehow to remove a node in a linked list cdelete fornode from linked listremove node from linked listdelete a node from linked list c 2b 2b programin c how do you remove an element from a linked list at any given indexdelete from linked list pythonhow to delete a node in linked list in c 2b 2bhow to delete a node a in singly linked list without head pointerhow to remove a node from a linked list cdeleting a linked list in clink list add and removedelete after a given node in linked listlinked list delete by valuelinkedlist remove nodedelete node in a linked list pythondelete a node from linked list c 2b 2bdelete after in doubly linked list in chow to delete a node from linked list pythondelete node from llhow to remove 28 29 from a linklisthow to remove from a linked list in chow to delete a value from a linked list in cdelete linked list code javahow to dlete a linked list node in cremove node from linked list javadelete given node from linked listdoubly linked list insert and delete in chow to delete a node from doubly linked list in cc remove element from linked listdeleting nodes in a linked listlinkedlist remove node cwhere deleted node go in a linked listdeletioon of any node in the linked listdelete node from linked list o 281 29delete node in a linked list solutionhow to remove a node from a linked list pythindeleting node from linked list in chow to delete node in linked list chow to remove a node in a doubly linked list chow to delete a node in linked list 3fdelete value in linked list pythonhow to remove from a linked listremove a node in linked listpop element from list cdelete linked list nodedremove an element from linked list cdelete linked list noderemove a node from a linked listremove node from linked list cc program to delete linked list in cdeleting a node in single linked list in c naresh technologiesremove node from linked lsit in cremove node linked listdelete element linked listdelete from linked list c 2b 2bremove leement linked list clinked list insertion deletion program in cdelete node in linked list in cwhen you delete a node from anremove from a linked list cdelete 3a this method deletes a node from the linked list if an index is passed as a parameter 2c then the method should delete the node at this index if no index is passed 2c then delete the first item in the listhow to remove a node in a linked listc remove from linked listremove node in linked list cremove first in linked list in pythonlinked list deletion program in cnode 3ct 3e deletedelete a node from linked listdelete a nodedelete a node in a linked list c 2b 2bdelete from linked listhow to remove a node from a linked list c languagelinked list remove nodedelete the node from anywhere from the listdelete a node in linked list geeksforgeeksdelete node in linked list pythonhow to delete a singly linked list first node using c programremove element from linked listhow to delete node in linked listc program for insertion and deletion in linked listhow to delete nodes from linked list in cc delete node from linked listc 2b 2b remove node from linked listlinkedlist deletesingle linked list delete atction to delete a node in a singldeleting a node from linked listc program to remove even nodes in a linked listc program delete linked listremove node in linked list javadelete node in linklist pythoninsertion and deletion in singly linked list in c programdeleting a linkedd list in cdelete element from linked list pythonremove the nodes in linked listhow to delete a node in c 2b 2bdelete nodes in linked list chow to delete node from linked listhow to delete a node javahow to free node in javalinked list in c delete nodedelete an element in a linked list in cprogram to insert 2cdelete a node in linked list in cdelete a node in linked list gfgc function that removers teh kth node in linked list how to delete elements from linked list in cdelete a node in singly linked list in cremove node from a linked listremove node linked list cdelete node betweengeeks for geeks delete node in singly linked listcode to delete a node from linked listhow to delete a node in a linked listhow to delete node in linked list c 2b 2blinked list delete a node c 2b 2bdeleting node from linked listdelete node linked list c 2b 2bdeleting for singly linked listcreate a doubly linked list in c to create a list 2c insert 2c display 2c search and deletehow to delete a given node in linked listhow to delete values in a node in cdeletion following a given nodedeletign from singly linked listdelete linkedlist node in cdelete all values from linked list in cdelete node at given position in a linked list in cremove between linked listhow to delete a node in a linked list 3fdelete in linked listlinked list c remove nodedelete a node at the end of linked list in cprogram to create insert 2c delete and display operations on singly linked list in chow to delete a middle node of singly linked list in cdelete a node in linked list pythonremove node linked list pythonprogram to delete an element in linked listpython delete nodehow to delete a complex linked list cdelete element from linked listdeleting a node in linked list in pythonvoid delete for singly linked listwrite a function for the deletion of one node before and after a given node in a linked list in cdelete elements from a linked list until ordereddelete and search in linked list in cdelete node in a linked list c 2b 2bdelete node in pythonsingly linked list delete specifichow to unlink node linked lists in chow to delete a node from the a linked listdelete all of a valuefrom a linked list in cdelete doubly linked list in cdelete the next node in linked listdeletion of node in linked listdeleting node in a linked listhow to remove a value from a linked list cremove negative nodes from a linked list pythondeletion linked listdelete first element of linked list cdelete a node in singly linked listhow to remove a node from a linked list c 2b 2bdelete middle node of a linked list cdelete an element from a linked list in cdelete function in c program for linked list structureremove a node in a linked listhow to delete a node in linked list in javaremove a node from the linkedlist c 2b 2bremove node in linked list c 2b 2bremove specific node form linked list in chow do i delete a value from my linked list cremove linked list elements cunlinking list elements cremove node beggining from linked list cremove element from linked list chow to delete an element from linked list in cremove a given node from the queue 2m 1program to create insert 2c delete and display operations on singly linked list in c with gpx datahow to remove node from list in cdoubly linked list to create and display insert delete in cdelete a node at the start of the listhow to delete a node in a singly linked listremove node in a linked listhow to remove nodesdelete a node in single linked listto delete a node in linked listalgorithm for deletion in linked list anyposdelete node linked list cdeleting node in a linked list in pythonhow to delete a node from linked list1 develop a menu riven program to do the following a create single linked list b count the number of nodes in the list c delete a node ate head of sll d delete a node at the end of sll e display all the nodes in the listdel python linked listhow do i remove elements in a linked list cdelete after in linked list in ccan you insert and remove anywhere with a singly linked listlinked list delete in cc 2b 2b program to insert and delete a node in a linked listhow to remove element from linked list cdeleting in linked list clinked list remove node cdelete function in a singly linked list in c