簡體   English   中英

從鏈表中刪除用戶輸入的字符串節點

[英]Deleting a user-entered string node from a linked list

我正在編寫一個 function,它應該從學生的鏈接列表中刪除用戶選擇的節點。 該程序將通過 read_line function 獲取用戶輸入的學生姓名、姓氏和 email,然后 1) 定位具有這些元素的節點,2) 繞過該節點,以及 3) 釋放該節點占用的 memory。 我的function會刪除除頭節點以外的任何節點,我無法理解我的邏輯是否有錯誤。

這是我的 function 代碼:

struct student* remove_from_list(struct student *list){
  struct student *p;
  struct student *cur;
  struct student *delete = malloc(sizeof(struct student));

  if(delete == NULL){
    printf("\nMalloc failed. \n");
  }
   if(list==NULL){
      printf("\nRoster empty. Nothing to remove.\n");
      return list;
    }

  printf("Enter the last name of the student to be removed: ");
  read_line(delete->last, NAME_LEN);
  printf("Enter the first name of the student to be removed: ");
  read_line(delete->first, NAME_LEN);
  printf("Enter the email of the student to be removed: ");
  read_line(delete->email, EMAIL_LEN);

//to check if student is in the list
  for(p=list; p!=NULL; p=p->next){
    if(((strcmp(delete->last, p->last))!=0) || (strcmp(delete->first,p->first)!=0)
    || (strcmp(delete->email, p->email)!=0)){
      continue;
    }
    else{
      break;
    }
      printf("\nThis student does not exist.\n");
      return list;
    }
  
    //to remove any element other than first
  for(cur=list; cur->next!= NULL; cur=cur->next){ 
    if(strcmp(cur->next->last, delete->last)==0 && 
    strcmp(cur->next->email,delete->email)==0 && 
    strcmp(cur->next->first, delete->first)==0){
      delete=cur->next;
      cur->next = cur->next->next;
      free(delete);
      printf("\nStudent has been removed from the list.\n");
      return list;
    }
  }

cur=list; //to remove first element 
if(cur->next == NULL){
    cur=cur->next;
    free(delete);
    printf("\nStudent has been removed from the list.\n");
    return list;
  }

}

我的 function 沒有刪除頭節點,我不確定這是一個小修復,還是我的邏輯有根本性的錯誤。 我已經看到使用 integer 輸入執行此操作的示例,但在我的案例中很難實現此操作。 任何幫助或建議表示贊賞。

我不確定這是否是你打算做的,但它在邏輯上看起來是錯誤的......

  for(p=list; p!=NULL; p=p->next){
    if(((strcmp(delete->last, p->last))!=0) || (strcmp(delete->first,p->first)!=0)
    || (strcmp(delete->email, p->email)!=0)){
      printf("\nThis student does not exist.\n");
      return list;
    }
  }

但是,除非第一個節點是您要刪除的節點,否則這不會失敗嗎? 你開始一個循環,查看第一個節點,如果不匹配,你返回列表?

我認為其目的不是尋找否定,而是遍歷節點,如果找到匹配項,則盡早結束循環 - 否則您想到達列表的末尾以檢查所有節點。

  for(p=list; p!=NULL; p=p->next){
    if(((strcmp(delete->last, p->last))==0) && (strcmp(delete->first,p->first)==0)
    || (strcmp(delete->email, p->email)==0)){
      printf("\nFound a match.\n");
      // delete this node and end this for loop early, probably better to use a while loop here, while(strcmp()!=0 etc);
    }
  }

編輯 既然您已經使用 for 循環來查找記錄,您可能想要進行另一次編輯。 您在第一個 for 循環中找到帶有 strcmp(),=0 集合的記錄,如果它找到了記錄。 跳出循環,此時可以刪除找到的記錄!

這意味着您不需要再次重復循環來刪除記錄,並且如果它是列表中的第一條記錄或后續記錄,您將不會有兩個不同的代碼塊。

  //to check if student is in the list
  for(p=list; p!=NULL; p=p->next){
    if(((strcmp(delete->last, p->last))!=0) || (strcmp(delete->first,p->first)!=0)
    || (strcmp(delete->email, p->email)!=0)){
      continue;
    }
    else {
      // we found a match! Delete it
      delete=p;
      p->next = p->next->next;
      free(delete);
      printf("Student has been removed from the list.\n");
      return list;
    }
    printf("\nThis student does not exist.\n");
    return list;
  }
// should never get here any more
} // end func.

如果你真的想保持你的代碼原樣,因為它對除第一條記錄之外的所有記錄都有效,那么你需要刪除第一條記錄才能工作。 == NULL 的檢查看起來不對,因為如果沒有其他鏈接到它,您只刪除第一條記錄? 我覺得這種邏輯是錯誤的。

  // if we get here, we can assume that the first element matched,
  // because otherwise we should never have gotten here
  cur=list; //to remove first element 
  if(cur->next != NULL){ // if there is a next element
    // set next to be the first
    cur=cur->next;
  }
  free(delete);
  printf("\nStudent has been removed from the list.\n");
  return list;
  
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM