| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 480 人关注过本帖
标题:链表在输出的时候系统要求停止
只看楼主 加入收藏
yuanhaitaozz
Rank: 1
来 自:江苏
等 级:新手上路
帖 子:32
专家分:9
注 册:2013-8-22
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:5 
链表在输出的时候系统要求停止
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct S
{
    char name[20];
    int number;
    struct S *next;
};
int main()
{
    struct S *head=NULL;
    struct S *current,*prev;
    char input[20];
    puts("Enter your name");
    while(gets(input)!=NULL && input[0]!='\0')
    {
        current=(struct S *)malloc(sizeof(struct S));
        if(head==NULL)
            head=current;
        else
            prev->next=current;
        strcpy(current->name,input);
        puts("Enter your number");
        scanf("%d",&current->number);
        while(getchar()!='\n')
            continue;
        puts("Enter next student");
        prev=current;
    }
    if(head==NULL)
        puts("no date");
    else
        puts("now output the contents");
    current=head;
    while(current!=NULL)
    {
        printf("name : %s  number : %d\n",current->name,current->number);
        current=current->next;
    }
    current=head;
    while(current!=NULL)
    {
        free(current);
        current=current->next;
    }
    return 0;
}
为什么在输出的时候系统要求停止呢  我有尝试把输出的语句删去就不会提示  
我就想问下是什么原因
在用vc6.0
搜索更多相关主题的帖子: current include number Enter 
2013-08-26 16:53
liufashuai
Rank: 9Rank: 9Rank: 9
来 自:冥界-魔域-魂殿
等 级:蜘蛛侠
威 望:1
帖 子:370
专家分:1374
注 册:2012-6-22
收藏
得分:10 
  

 while(current!=NULL)
    {
        free(current);
        current=current->next;
    }
我勒个去。。

大哥,哪有那样释放内存的,你把内存释放了,current还有next可用么
问题就在这(当然不确定你其他地方还有错误不)

换一种释放方法,或者用指针替换:
在上面加一个同类型指针p  

 while(current!=NULL)
    {
        p = current->next;
        free(current);
        current=p;
    }



有一种落差是,你配不上自己的野心,也辜负了所受的苦难。






2013-08-26 18:08
yuanhaitaozz
Rank: 1
来 自:江苏
等 级:新手上路
帖 子:32
专家分:9
注 册:2013-8-22
收藏
得分:0 
回复 2楼 liufashuai
我试了一下你说的 还是提示停止工作  然后我干脆不释放删了那句话。。。还是不行
2013-08-26 20:01
liufashuai
Rank: 9Rank: 9Rank: 9
来 自:冥界-魔域-魂殿
等 级:蜘蛛侠
威 望:1
帖 子:370
专家分:1374
注 册:2012-6-22
收藏
得分:0 
那就是其他地方的问题呗,但是你的删除错误很明显

看一下,你链表最后一个元素创建的时候有没有把尾指针赋值为NULL,不然怎么判断它结束。
创建链表最后一步加上next域赋值为Null试试

有一种落差是,你配不上自己的野心,也辜负了所受的苦难。






2013-08-27 00:00
yuanhaitaozz
Rank: 1
来 自:江苏
等 级:新手上路
帖 子:32
专家分:9
注 册:2013-8-22
收藏
得分:0 
回复 4楼 liufashuai
  谢了  正如你所说的
2013-08-27 14:39
jay1234567
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2013-8-27
收藏
得分:0 
我好想也碰到过
2013-08-27 16:29
快速回复:链表在输出的时候系统要求停止
数据加载中...
 
   



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

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