| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 428 人关注过本帖
标题:创建链表后不能结束???求解
只看楼主 加入收藏
青山细雨
Rank: 2
等 级:论坛游民
帖 子:25
专家分:16
注 册:2011-10-18
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:2 
创建链表后不能结束???求解
#include<stdio.h>
#include<stdlib.h>
struct student
{
    int num;
    char s;
    student *next;
};
student * creat()

{
    student *pnew,*pend;
    student *phead ;   
    phead=NULL;
    do
    {
        pnew=new student;
        printf("please input :\n");
        printf("num:");
        scanf("%d",&pnew->num);
        char ch=getchar();
        if(pnew->num==0) break;
        printf("s:");
        scanf("%c",&pnew->s);
        if(phead==NULL) phead=pnew;
        else pend->next=pnew;
        pend=pnew;
    }while(pnew->num!=0);
   pend->next=0;
    free(pnew);
    return phead;
}
student * save(student *phead)
{
    FILE *fp;
    student *pn;
    pn=phead;
    if((fp=fopen("stud","w"))==NULL)
    {
        printf("cannot open file!\n");
        exit(0);
    }
    do
    {
        fwrite(pn,sizeof(student),1,fp);
        pn=pn->next;
    }while(pn->next!=NULL);
    return phead;
}
void main()
{
    student *head;
   head =creat();
    save(head);
}
在创建中出现了问题,不能正常运行。。。。。
搜索更多相关主题的帖子: include please 
2011-11-09 11:09
tan2010
Rank: 2
等 级:论坛游民
帖 子:66
专家分:71
注 册:2011-10-13
收藏
得分:20 
#include<stdio.h>//创建链表后不能结束???求解
#include<stdlib.h>
#include<malloc.h>
struct student
{
    int num;
    char s;
    student *next;
};
student * creat()

{
    struct student *pnew,*pend;
    struct student *phead ;   
    phead=NULL;
    do
    {
        pnew=(struct student *)malloc(sizeof(struct student));

       printf("please input :\n");
        printf("num:");
        scanf("%d",&pnew->num);
        char ch=getchar();
        if(pnew->num==0) break;
        
        printf("s:");printf("\n");
        scanf("%c",&pnew->s);
        if(phead==NULL)
            phead=pnew;
        else
            pend->next=pnew;
        pend=pnew;
    }while(pnew->num!=0);
   pend->next=0;
    free(pnew);
    return phead;
}
student * save(student *phead)
{
    FILE *fp;
    struct student *pn;
    pn=phead;
    if((fp=fopen("stud","w"))==NULL)
    {
        printf("cannot open file!\n");
        exit(0);
    }
    do
    {
        fwrite(pn,sizeof(student),1,fp);
        pn=pn->next;
    }while(pn->next!=NULL);
    return phead;
}
void main()
{
    struct student *head;
   head =creat();
    save(head);
}
不过你这程序中之前的new用法我不懂,我改成了malloc函数!!
2011-11-09 16:47
tan2010
Rank: 2
等 级:论坛游民
帖 子:66
专家分:71
注 册:2011-10-13
收藏
得分:0 
这里的注意:写入s是必须是一个字符,否则 会紊乱的!!
2011-11-09 17:13
快速回复:创建链表后不能结束???求解
数据加载中...
 
   



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

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