从链表中删除字符串 - C

Delete string from linked list - C

所以我 运行 遇到的问题是从充满字符串的链表中删除用户输入的字符串。

我在理解 just 链表的确切工作方式方面仍然存在一些问题,因此对于我做错了什么的任何解释将不胜感激! 此外,其他所有功能似乎都运行良好,只是 deleteItem 功能出现问题!

编辑 - 我在 运行 deleteItem 功能时遇到的问题只是我的终端 window 在挂机后崩溃。

这是我的代码:

#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 the number of the operation you'd like to 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 is 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 the name you wish to 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 the name you wish to 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 is 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 is the name you wish to 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 was deleted successfully\n", nameToDelete);
           }

        }

}

我看到以下问题:

  1. 你处理的不是要删除的项目在列表头部的情况。

  2. free包含该项的节点后,链表没有恢复到良好状态。

  3. 即使在 free 包含该项目的节点之后,您仍继续遍历列表。

这是一个应该可以使用的版本。

void deleteItem(){
   char nameToDelete[50];
   struct node *temp, *previous;
   temp = head;

   printf("What is the name you wish to delete?\n");
   scanf("%s", nameToDelete);

   // First, locate the node that contains the item.
   for ( ; temp->next != NULL; temp = temp->next )
   {
      if(strcmp(nameToDelete, temp->name)==0)
      {
         break;
      }
      prev = temp;
   }

   // Now take care of deleting it.
   if ( temp != NULL )
   {
      if ( temp == head )
      {
         head = temp->next;
      }
      else
      {
         prev->next = temp->next;
      }

      free(temp);
      printf("%s was deleted successfully\n", nameToDelete);
   }    
}