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;
}
4

4 回答 4

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指针。

于 2013-03-19T06:42:22.827 回答
0

您的问题很可能来自您的显示功能。您可能暂时忘记了括号。

要获得有关内存分配错误的一些非常好的提示,以及更一般地说您对内存做错的任何事情,您应该使用valgrind(如果您使用的是 linux 发行版)。

于 2013-03-19T06:44:08.343 回答
0

在这个函数中

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

当循环结束时 p 将为 NULL。所以你得到分段错误。

将其更改为

while(p->link!=NULL){
   ...
}
于 2013-03-19T06:48:04.283 回答
0

第 20-22 行:

您正在将空指针的地址传递给append,从而导致未定义的行为。

可能的修复:

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

您的代码似乎还有其他问题,因此请检查其他答案和评论。

于 2013-03-19T06:48:15.683 回答