从链表中删除字符串 - C.

时间:2015-06-02 23:44:22

标签: c string linked-list

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

我仍然在理解只是精确链接列表如何工作方面存在一些问题,因此对我所做错的任何解释都将不胜感激! 此外,其他所有功能似乎都运行得很好,只是遇到deleteItem功能问题!

编辑 - 我运行deleteItem功能时遇到的问题只是我的终端窗口在挂了一会儿后崩溃。

这是我的代码:

#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 个答案:

答案 0 :(得分:1)

我看到以下问题:

  1. 您没有处理要删除的项目位于列表首部的情况。

  2. free包含该项目的节点后,链接列表不会恢复到良好状态。

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

  4. 这是一个应该有效的版本。

    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);
       }    
    }