2013-07-20 42 views
1

我一直在研究一個函數searchn(),它需要鏈接列表list和字符串lname通過C中的鏈接列表進行搜索

它編譯得非常好,但當我嘗試運行該函數時,出現了段錯誤(核心轉儲)。我跑過Valgrind,它告訴我我錯誤地使用了strcpystrcmp。我的player結構也包含在內供參考。任何人都可以看到我在做什麼錯了嗎?對不起,我不是最擅長編碼的人。

任何幫助將是偉大的。謝謝。

struct player { 
    char* fname; 
    char* lname; 
    char pos; 
    int val; 
    int rank; 
    struct player* next; 
}; 

void searchn(struct player* list, char* lname){ 

    while (list!=NULL && strcmp(list->lname, lname) != 0){ 
     list = list->next; 
    } 

    if (list != NULL && strcmp(list->lname, lname)==0) { 
     printf("%s found! \n", lname); 
     printf("%s \n", list->lname); 
     printf("%s \n", list->fname); 
     printf("%c \n", list->pos); 
     printf("%d \n", list->val); 
     printf("\n"); 
    } 
} 

以下是如何填充鏈表的方法。

void addp (struct player* newnode, struct player* list){ 
    struct player* templist1; 
    // if the list is non empty. 
    if (list !=NULL){ 
     if(newnode->pos == GOALKEEPER){ //insert if G. 
      while (list->next != NULL && (list->next)->rank < 1){ 
       list = list->next; 
      } 
      templist1 = list->next; 
      list->next = newnode; 
      newnode->next = templist1; 
     } 
     if(newnode->pos == DEFENDER){// after G bef M. 
      // iterate through templist. 
      while (list->next != NULL && (list->next)->rank < 2) { // go to end of G. 
       // when the list isn't empty next node rank is less than one, keep going 
       list = list -> next; 
      } 
      // when finally rank == or > 1, then add newnode. 
      templist1 = list->next; 
      list->next = newnode; 
      newnode->next = templist1; 
     } 
     if(newnode->pos == MIDFIELDER){ //after G and M but before S 
      while (list->next != NULL && (list->next)->rank < 3) { 
       list = list -> next; 
      } 
      // when stopped, then add newnode. 
      templist1 = list->next; 
      list->next = newnode; 
      newnode->next = templist1; 
     } 
     if(newnode->pos == STRIKER){ // at the end. 
      while (list->next != NULL && (list->next)->rank < 4){ 
       list = list -> next; 
      } 
      templist1 = list->next; 
      list->next = newnode; 
      newnode->next = templist1; 
     } 
     printf("player added"); 
    } 
} 
+0

您將需要展示如何在鏈接列表元素中分配和分配值。這個邏輯有可能是你的分段錯誤。 – lurker

+0

我想是的,但事情是,填充鏈表的方法到目前爲止工作得很好。我有另一種搜索方法,通過搜索每個節點的'int val',這恰好工作正常。 –

+0

您仍然需要展示字符串和結構如何分配以瞭解發生了什麼。以及如何將字符串複製到結構中。其中任何一個都可能產生分段錯誤。 – lurker

回答

2

while循環保持比較相同的兩個字符串,因爲它不會加載列表中的元素放入臨時新的。無論如何,你並不需要臨時變量。

在當前的邏輯,你可以只移動strcpy只是while循環中:

while (list!=NULL && strcmp(temp, lname) != 0){ 
    strcpy(temp, list->lname); 
    list = list->next; 
} 

但你可以完全擺脫臨時的。更改此:

strcpy(temp, list->lname); 

while (list != NULL && strcmp(temp, lname) != 0) { 
    list = list->next; 
} 

if (strcmp(temp, lname) == 0) { 

要這樣:

while (list != NULL && strcmp(list->lname, lname) != 0) { 
    list = list->next; 
} 

if (list != NULL) { 
+0

我發佈這個問題後,我確實想到了這個問題,但即使進行了更正,我仍然得到了完全相同的錯誤。嗯。 –

+1

@elHO我沒有你的代碼的完整上下文,所以我假設你設置並正確分配了鏈接列表的內存。您不會顯示鏈接列表的加載,也許您應該在您的問題陳述中執行此操作。 – lurker

1

因爲它是回答,你不更新溫度。 你也不需要它。此外,如果字符串超過1024個字節,則可能會出現漏洞。 試試這個:

while (list != NULL && strcmp(list->lname, lname) != 0) { 
    list = list->next; 
} 

if (list != NULL) { 

STRCMP在最後一個條件是不必要的。