| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2447 人关注过本帖
标题:关于链表一直出现Segmentation fault错误的问题
只看楼主 加入收藏
lockeroots
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2016-4-14
结帖率:0
收藏
已结贴  问题点数:10 回复次数:5 
关于链表一直出现Segmentation fault错误的问题
程序代码:
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

typedef struct students{
        int s;
        struct students *n;
}stu;

typedef struct _list{
        struct students *head;
}List;

int add(List *ls,int number)
{
        stu *p,*l;
        p=(stu*)malloc(sizeof(stu));
        p->n=NULL;
        p->s=number;
        l=ls->head;
        if(l)
        {
                if(l->n)
                {
                        l=l->n;
                }
                l->n=p;
        }else{
                ls->head=p;
        }

}

int pin(List *ls)
{
        stu *head;
        head=ls->head;
        while(head)
        {
                printf("%d\n",head->s);
                head=head->n;
        }
}

int main()
{
        List *ls;
        ls->head=NULL;;
        int number=1;
        while(number!=0)
        {
                scanf("%d",&number);
                add(ls,number);
        }
        pin(ls);
        return 0;
}
~           
2016-05-09 22:08
吹水佬
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:451
帖 子:10566
专家分:43004
注 册:2014-5-20
收藏
得分:3 
最后一行有个“~”字符?
删除后未见有什么大问题
2016-05-10 08:35
lockeroots
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2016-4-14
收藏
得分:0 
回复 2楼 吹水佬
不是这个问题
2016-05-10 21:24
xiaomaoshi
Rank: 2
等 级:论坛游民
威 望:1
帖 子:10
专家分:28
注 册:2016-5-10
收藏
得分:3 
链表的add函数,第二个if改成while。
2016-05-10 21:50
alice_usnet
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:18
帖 子:370
专家分:2020
注 册:2016-3-7
收藏
得分:3 
程序代码:
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

typedef struct students{
        int s;
        struct students *n;
}stu;

typedef struct _list{
        struct students *head;
}List;

int add(List *ls,int number)
{
        stu *p,*l;
        p=(stu*)malloc(sizeof(stu));
        
        if(ls == NULL) {
           ls = (List*)malloc(sizeof(List));
           ls->head = NULL;
        }
       
        p->n=NULL;
        p->s=number;
        l=ls->head;
       
        if(l)
        {
                while(l)
                {
                        l=l->n;
                }
                l->n=p;
        }else{
                ls->head=p;
        }
        
}

int pin(List *ls)
{
        stu *head;
        head=ls->head;
        while(head)
        {
                printf("%d\n",head->s);
                head=head->n;
        }
}

int main()
{
        List *ls = NULL;
        /*ls->head=NULL;;*/
        int number=1;
        while(number!=0)
        {
                scanf("%d",&number);
                add(ls,number);
        }
        pin(ls);
        return 0;
}


[此贴子已经被作者于2016-5-11 09:41编辑过]


未佩好剑,转身便已是江湖
2016-05-11 09:39
grmmylbs
Rank: 14Rank: 14Rank: 14Rank: 14
等 级:贵宾
威 望:54
帖 子:1409
专家分:5845
注 册:2016-2-14
收藏
得分:3 
程序代码:
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

typedef struct students {
    int s;
    struct students *n;
}stu;

typedef struct _list {
    struct students *head;
}List;

int add(List **ls, int number)
{
    stu *p, *l;
    p = (stu*)malloc(sizeof(stu));

    if (*ls == NULL) {
        *ls = (List*)malloc(sizeof(List));
        (*ls)->head = NULL;
    }

    p->n = NULL;
    p->s = number;
    l = (*ls)->head;

    if (l)
    {
        while (l->n)
        {
            l = l->n;
        }
        l->n = p;
    }
    else {
        (*ls)->head = p;
    }

}

int pin(List *ls)
{
    stu *head;
    head = ls->head;
    while (head)
    {
        printf("%d\n", head->s);
        head = head->n;
    }
}

int main()
{
    List *ls = NULL;
    /*ls->head=NULL;;*/
    int number = 1;
    while (number != 0)
    {
        scanf("%d", &number);
        add(&ls, number);//输入地址才能被修改
    }
    pin(ls);
    return 0;
}
2016-05-11 10:03
快速回复:关于链表一直出现Segmentation fault错误的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.032720 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved