2014-06-05 142 views
-3

我正在嘗試一個示例鏈接列表程序。下面的代碼有什麼問題?當我嘗試訪問值時出現分段錯誤。我無法訪問超出root的值。以下哪項錯誤?鏈接列表

#include<stdio.h> 
#include<stdlib.h> 

struct node{ 
    int val; 
    struct node* next; 
}; 

void create(int n,struct node** ref){ 
    struct node *temp,*newnode; 
    newnode=(struct node*)calloc(1,sizeof(struct node)); 
    newnode->val=n; 
    newnode->next=NULL; 
    if(*ref==NULL){ 
     *ref=newnode; 
     temp=newnode; 
    } 
    else{ 
     temp->next=newnode; 
     temp=newnode; 
    } 
    return; 
} 

int main(){ 
    struct node *root=NULL,*p; 
    int n,i,j=1; 
    while(j==1){ 
     printf("enter the value...\n"); 
     scanf("%d",&n); 
     create(n,&root); 
     //printf("%d",root->val); 
     printf("Press 1 to continue..\n"); 
     scanf("%d",&j); 
    } 
    p=root; 
    while(p!=NULL){ 
     printf("%d-",p->val); 
     p=p->next; 
    } 
    printf("\n"); 
    return 0; 
} 
+0

在'temp-> next = newnode'行中,您可以在不初始化的情況下訪問'temp'。 – mafso

回答

0

您的鏈接列表創建方法不正確。您沒有將內存分配給temp,並嘗試將newnode分配給temp-> next。 正確的是如下:

struct node{ 
    int val; 
    struct node* next; 
}; 

node *lastnode = NULL; 

void create(int n,struct node** ref){ 
    struct node *temp,*newnode; 
    newnode=(struct node*)calloc(1,sizeof(struct node)); 
    newnode->val=n; 
    newnode->next=NULL; 
    if(*ref==NULL){ 
     *ref=newnode; 
     // temp=newnode; 
    } 
    else{ 
     // temp->next=newnode; 
     // temp=newnode; 
     lastnode->next = newnode; 
    } 
    lastnode = newnode; 
    return; 
} 
+0

I我不寫一份生產就緒代碼。內容就是強調問題代碼中的問題。進一步的優化可以達到'n'級。沒有全局的,沒有靜態的,沒有未使用的局部變量。 – Anil8753

0

您還沒有爲temp分配內存。添加下面的第二行。

newnode=(struct node*)calloc(1,sizeof(struct node)); 
temp=(struct node*)calloc(1,sizeof(struct node)); 
+0

她不想爲'temp'分配內存。她根本不需要'臨時'。 – mafso

+0

是的,你是對的,但是當臨時被分配一些價值時,seg故障正在發生。從邏輯上說,我認同溫度是不必要的。似乎她是一個初學者...我也開始在C面臨這些問題:) – Vijay

+0

是的,但它不是一個很好的建議,以解決已經泄漏的代碼中的段錯誤,並帶有新的內存泄漏... – mafso

1

我認爲你只需要聲明temp爲靜態。至少,這對我的作品:

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <math.h> 

struct node{ 
    int val; 
    struct node* next; 
}; 

void create(int n,struct node** ref){ 
    static struct node *temp; 
    struct node *newnode; 

    newnode=(struct node*)calloc(1,sizeof(struct node)); 
    newnode->val=n; 
    newnode->next=NULL; 
    if(*ref==NULL){ 
    *ref=newnode; 
    temp=newnode; 
    } 
    else{ 
    temp->next=newnode; 
    temp=newnode; 
    } 

    return; 
} 

輸出:

enter the value... 
1 
Press 1 to continue.. 
1 
enter the value... 
2 
Press 1 to continue.. 
1 
enter the value... 
3 
Press 1 to continue.. 
0 
1-2-3- 
+0

這隻適用於一個列表... – mafso

0

您的代碼有兩個問題:

當您嘗試訪問temp未initilized的段錯誤發生。而且你也不需要這個變量,並且這兩條線與

temp=newnode; 

什麼都不做;改變

temp->next=newnode; 

(*ref)->next=newnode; 

和刪除temp完全。

的第二個問題是由

 create(n,&root); 

每次用(您的列表目前從未得到兩個以上的元素)相同的根值的存儲器泄漏。

int main(){ 
    struct node *root=NULL; 
    struct node **current = &root; 
    int n,i,j=1; 
    while(j==1){ 
     printf("enter the value...\n"); 
     scanf("%d",&n); 
     /*create(n,&root);*/ 
     create(n, current); 
     current = &(*current)->next; 
     //printf("%d",root->val); 
     printf("Press 1 to continue..\n"); 
     scanf("%d",&j); 
    } 
    struct node *p; 
    p=root; 
    while(p!=NULL){ 
     printf("%d-",p->val); 
     p=p->next; 
    } 
    printf("\n"); 
    return 0; 
}