2013-07-09 106 views
0

我正在學習如何在C中建立鏈接列表。我的程序編譯,但由於某種原因,我不明白,我得到一個分段錯誤。我一直在試圖弄清楚這個問題,但我沒有任何運氣。下面是錯誤代碼:分段錯誤 - 鏈接列表

int len() 
{ 
    struct list * current = head; 
    int length = 0; 

    while (current != NULL) 
    { 
     length++; 
     current = current -> next; //move to next node 
    } 
    return length; 
} 


struct list * search (int key) 
{ 
    struct list * current = head; 

    while (current != NULL && current->data != key) 
     current = current -> next; 

    if (current != NULL && current -> data == key) 
     return current; 
    return NULL; 
} 



/* Insert a new data element with key d into the end of the list. */ 
void insert(int d) // at the end 
{ 
    struct list * current = head; 
    struct list * new; 
    while (current -> next != NULL) 
     current = current -> next; 
    new = (struct list *)malloc(sizeof(struct list)); 
    new -> data = d; 
    current -> next = new; 
    new -> next = NULL;  
} 


void insertAfter(int d, int where) // insert at the middle 
{ 
    struct list * marker = head; 
    struct list * new; 

    while(marker -> data != where) 
     marker = marker -> next; 
    new = (struct list*)malloc(sizeof(struct list)); 

    new -> next = marker -> next; 
    marker -> next = new; 
    new -> data = d; 
} 


/* Remove the node with value d from the list */ 
/* assume no duplicated keys in the list */ 
/* If the list is empty, call prtError() to display an error message and return -1. */ 

void delete(int d) 
{ 
    struct list * current1 = head; 
    struct list * current2; 

    if (len() == 0) 
    { //prtError("empty"); 
     exit(0); 
    } 
    if (head -> data == d) 
    { 
     head = head -> next; 
    } 

    //Check if last node contains element 
    while (current1->next->next != NULL) 
     current1 = current1->next; 

    if(current1->next->data == d) 
      current1->next == NULL; 


    current1 = head; //move current1 back to front */ 

    while(current1 -> next -> data != d) 
     current1 = current1 -> next; 

    current2 = current1 -> next; 
    current1 -> next = current2 -> next; 

} 

我在該行獲得一個分段故障的刪除方法:

while(current1 -> next -> data != d) 

爲什麼這是錯?

+0

在哪個函數中你得到了分段錯誤? – Jashaszun

+2

如果您在調試器中運行它,調試器會在哪裏說分段故障正在發生? – Simon

+1

我不認爲C中保留了'new',但你可能不應該將它用作變量名。你的'delete'函數看起來像是可能的候選者,你有'if(head - > data == d)'不檢查'NULL'和'current1-> next-> next'可能不好, insert'具有'current - > next' w/o'NULL'檢查。 –

回答

0

您發佈的代碼存在許多問題,但您在提到的註釋中提到insert()。它崩潰的原因是,如果調用insert()head爲NULL,則將取消引用NULL指針。

您將需要一個特殊的情況下,在head插入時,它是NULL:

if (head) { 
    while (current -> next != NULL) 
     current = current -> next; 
} 
new = (struct list *)malloc(sizeof(struct list)); 
new -> data = d; 
if (current) { 
    current -> next = new; 
} else { 
    head = new; 
} 
new -> next = NULL; 

你應該在你的其他功能檢查類似的問題。使用您的search()函數作爲避免在您的循環中取消引用NULL指針的示例。

0

你有幾個問題,insert

while (current -> next != NULL) 

你不檢查,如果currentNULL。有類似問題delete

if (head -> data == d) 

你需要在這裏檢查head和:

while (current1->next->next != NULL) 

也是一個問題。

+0

「current - > next = new;」還有另一個問題。在插入方法中。這條線有什麼問題?這是我在C中的鏈接列表的第一次嘗試,我仍在學習! – Sophie

+0

@Sophie這行'while(current-> next!= NULL)current = current-> next;'將確保'current'爲'NULL' –

+0

我在行的delete方法中遇到了段錯誤:while (當前1 - >下一個 - >數據!= d) – Sophie

0

插入錯誤之後,您將超出列表的末尾找不到元素。

0

insert

while(current->next != NULL) current = current->next; 

這將保證current == NULL

current->next = new; 

每次崩潰。

0

也許是在insertAfter

while(marker -> data != where) 
    marker = marker -> next; 

如果發現data == where沒有節點,將是一個NULL指針。
NULL指針在後面的代碼取消引用:

new = marker -> next; 

它使一個段錯誤。檢查是否marker->next != NULL應該避免:

while(marker->next != NULL && marker -> data != where) 
    marker = marker -> next; 

但是你應該嘗試編譯與調試符號程序(-g選項),並在諸如GDB調試器行由行運行它。