【问题标题】:Delete string from linked list - C [duplicate]从链表中删除字符串 - C [重复]
【发布时间】:2015-06-02 23:44:22
【问题描述】:

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

我在理解只是准确地理解链接列表如何工作方面仍然存在一些问题,所以任何关于我做错了什么的解释将不胜感激! 此外,其他所有功能似乎都运行良好,只是 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);
           }

        }

}

【问题讨论】:

  • 0) if(head=NULL) 应该是 if(head==NULL)
  • @BLUEPIXY 谢谢!这解决了其中一个问题!尝试删除字符串时仍然崩溃。
  • 废话,我没有尝试使用该术语进行搜索...谢谢,@BLUEPIXY

标签: c string linked-list


【解决方案1】:

我看到以下问题:

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

【讨论】:

  • 非常感谢您的回答和解释!但问题是:为什么在第一个 for 循环的开头有一个“浮动”;? @R萨胡
  • Herp,忽略之前的评论,我才意识到。再次感谢您!
  • @bravesaint,这是for 循环的语法要求。
  • @R Sahu 我刚刚意识到......这是在下课(遗憾地远离编程)一个月左右后第一次分配回来的情况......
猜你喜欢
  • 2021-07-02
  • 1970-01-01
  • 2011-08-08
  • 2014-08-02
  • 1970-01-01
  • 2014-05-20
  • 1970-01-01
  • 2018-06-09
  • 1970-01-01
相关资源
最近更新 更多