2013-03-19 74 views
0

該程序在第一個printf語句後導致分段錯誤。 據我所知,在內存堆棧滿的情況下出現分段錯誤。但在我的情況下,沒有遞歸程序,只有在調用一個程序4次之後。鏈接列表程序中的分段錯誤

請幫我理解爲什麼會發生這種情況。

下面是代碼:

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

struct node 
{ 
    int data; 
    struct node *link; 
}; 

void append(struct node **, int); 
void addatbeg(struct node **,int); 
int count(struct node *); 
void display(struct node *); 
void addafter(struct node **, int,int); 

int main() 
{ 
    struct node *q; 
    q= NULL; //list is empty 

    append(&q,10); 
    append(&q,20); 
    append(&q,30); 
    append(&q,40); 

    printf("Now display the contents of the linked list:\n"); 
    display(q); 

    addatbeg(&q,17); 
    addatbeg(&q,59); 

    printf("after adding the elements in the beginning, new linked list contents are\n"); 

    display(q); 

    addafter(&q,4, 15); 
    addafter(&q,7, 25); 
    printf("after adding the elements at specified location, list elements are: \n"); 
    display(q); 

    printf("\n\nCounting of list elements, list has %d elements", count(q)); 
    return 0; 
} 

void append(struct node **p, int num) 
{ 
    struct node *temp, *r; 
    temp=*p; 
    if(*p==NULL) 
    //Linked list is empty and the node to be added is the first node 
    { 
     temp=(struct node *)malloc(sizeof(struct node)); 
     temp->data=num; 
     temp->link=NULL; 
     *p=temp; 
    } 
    else 
    { 
     while(temp->link!=NULL) 
     { 
      temp=temp->link; 

      r=(struct node*)malloc(sizeof(struct node)); 
      r->data=num; 
      r->link=NULL; 
      temp->link=r; 
     } 
    } 
} 

void addatbeg(struct node **p, int num) 
{ 
    struct node *temp, *r; 
    temp=*p; 

    r=(struct node *)malloc(sizeof(struct node)); 
    r->data=num; 
    r->link=temp->link; 
    *p=r; 
} 

void addafter(struct node **p, int loc, int num) 
{ 
    int i; 
    struct node *temp, *r; 
    temp=*p; 
    //first we will find out the desired loc 
    for(i=0; i<loc; i++) 
     temp=temp->link; 
    //now need to create a new node 

    r=(struct node*)malloc(sizeof(struct node)); 
    r->data=num; 
    r->link=temp->link; 
    temp->link=r; 
} 

void display(struct node *p) 
{ 
    while(p!=NULL) 
    p=p->link; 
    printf("\n%d\t",p->data); 
} 

int count(struct node *p) 
{ 
    int count=0; 
    while(p!=NULL) 
    { 
     p=p->link; 
     count++; 
    } 
    return count; 
} 
+2

並在下次發佈問題代碼時刪除行號。 – WhozCraig 2013-03-19 06:42:29

回答

2

的一個問題是,在append(),你是在一個循環中創建新的元素,而不是隻創建一個:

63 while(temp->link!=NULL) 
64 { 
65  temp=temp->link; 

r=(struct node*)malloc(sizeof(struct node)); 
68  r->data=num; 
69  r->link=NULL; 
70  temp->link=r; 
71 } 

然而,段錯誤的直接原因可能是以下幾種:

105 void display(struct node *p) 
106 { 
107 while(p!=NULL) 
108 p=p->link; 
109 printf("\n%d\t",p->data); 
110 } 

在這裏,printf()需要在循環內。您當前的代碼實際上不會打印元素,並嘗試取消引用NULL指針。

0

您的問題很可能來自您的顯示功能。這段時間你可能已經忘記了括號。

要獲得關於內存分配錯誤的一些非常好的提示,以及更一般的任何你的內存錯誤,你應該使用valgrind(如果你在linux發行版中)。

0

在此功能

void display(struct node *p) 
{ 
    while(p!=NULL) 
    p=p->link; 
    printf("\n%d\t",p->data); 
} 
當循環結束p將爲NULL

。 所以你得到分段錯誤。

將其更改爲

while(p->link!=NULL){ 
    ... 
} 
+0

C區分大小寫。 – Lundin 2013-03-19 07:17:22

+0

這是一個錯字。我現在修好了。 – Kishore 2013-03-19 07:31:53

0

20-22行:

你傳遞一個空指針的ADRESS到append,導致未定義的行爲。

可能的解決辦法:

20 q = malloc(sizeof q); 
    *q = NULL; 

看起來也似乎是其他問題與您的代碼,因此檢查其他的答案和評論,以及。