Delete string from linked list - C -
this question has answer here:
so problem i'm running deleting user-inputted string linked list full of strings.
i'm still having few issues in understanding just precisely how linked lists work, explanation doing wrong appreciated! also, every other function seems working fine, having issues deleteitem
function!
edit - problem i'm getting when run deleteitem
function terminal window crashing after getting hung bit.
here's code:
#include<stdio.h> #include<stdlib.h> #include<string.h> struct node { char name[50]; struct node *next; }*head; void display(); void insert(); void count(); void deleteitem(); int main(){ int choice = 1; char name[50]; struct node *first; head = null; while(1){ printf("menu options\n"); printf("----------------\n"); printf("please enter number of operation you'd do: \n"); printf("----------------\n"); printf("1. insert\n"); printf("2. display\n"); printf("3. count\n"); printf("4. delete\n"); printf("5. exit\n"); printf("----------------\n"); scanf("%d",&choice); switch(choice) { case 1: insert(); break; case 2: display(); break; case 3: count(); break; case 4: if(head=null) printf("the list blank"); else deleteitem(); break; case 5: return 0; default: printf("invalid option"); } } system("pause"); return 0; } void insert(){ char nametoinsert[50]; struct node *temp; temp = head; if(head == null){ head = (struct node *)malloc(sizeof(struct node)); printf("what's name wish insert?\n"); scanf("%s", &nametoinsert); strcpy(head->name, nametoinsert); head->next = null; } else{ while(temp->next !=null){ temp = temp->next; } temp->next = malloc(sizeof(struct node)); temp = temp->next; printf("what's name wish insert?\n"); scanf("%s", &nametoinsert); strcpy(temp->name, nametoinsert); temp->next = null; } } void display(){ struct node *temp; temp = (struct node *)malloc(sizeof(struct node)); temp = head; if(temp == null) printf("the list empty\n"); else{ printf("%s\n", temp->name); while(temp->next != null){ temp = temp->next; printf("%s\n", temp->name); } } } void count(){ struct node *temp; int c =0; temp = head; while(temp!=null){ temp=temp->next; c++; } printf("\n%d", c); } void deleteitem(){ char nametodelete[50]; struct node *temp, *previous; temp = head; printf("what name wish delete?\n"); scanf("%s", nametodelete); while(temp->next != null){ temp = temp->next; if(strcmp(nametodelete, temp->name)==0){ previous = temp->next; free(temp); printf("%s deleted successfully\n", nametodelete); } } }
i see following issues:
you not dealing case item delete @ head of list.
the linked list not restored state after
free
node contains item.you continue iterate on list after
free
node contains item.
here's version should work.
void deleteitem(){ char nametodelete[50]; struct node *temp, *previous; temp = head; printf("what name wish delete?\n"); scanf("%s", nametodelete); // first, locate node contains item. ( ; temp->next != null; temp = temp->next ) { if(strcmp(nametodelete, temp->name)==0) { break; } prev = temp; } // take care of deleting it. if ( temp != null ) { if ( temp == head ) { head = temp->next; } else { prev->next = temp->next; } free(temp); printf("%s deleted successfully\n", nametodelete); } }
Comments
Post a Comment