| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1311 人关注过本帖, 1 人收藏
标题:关于列表问题!谢谢
只看楼主 加入收藏
独孤小梦
Rank: 1
等 级:新手上路
威 望:1
帖 子:338
专家分:0
注 册:2008-6-5
结帖率:80%
收藏(1)
 问题点数:0 回复次数:8 
关于列表问题!谢谢
谢谢大家能看我的帖:问题如下:
能编译出来,但是输入不了!不知道哪里出问题了,大家给看看!
#include"stdio.h"
#include"malloc.h"
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
    int xuehao;
    char xingming[10];
    int yuwen;
    int shuxue;
    int yingyu;
    int wuli;
    int huaxue;
    int zhengzhi;
    int dili;
    int lishi;
    int shengwu;
    struct student *next;
};
int n;
struct student *creat(void)
{
    struct student *head;
    struct student *p1,*p2;
    n=0;
    p1=p2=(struct student *)malloc(LEN);  //*开辟一个新单元*//
    scanf("%ld,%s,%d,%d,%d,%d,%d,%d,%d,%d,%d",&p1->xuehao,&p1->xingming,&p1->yuwen,&p1->shuxue,&p1->yingyu,&p1->wuli,&p1->huaxue,&p1->zhengzhi,&p1->dili,&p1->lishi,&p1->shengwu);
    head=NULL;
    while(&p1->xuehao!=0)
    {
        n=n+1;
        if(n==1)
            head=p1;
        else
            p2->next=p1;
        p2=p1;
        p1=(struct student *)malloc(LEN);
        scanf("%ld,%s,%d,%d,%d,%d,%d,%d,%d,%d,%d",&p1->xuehao,&p1->xingming,&p1->yuwen,&p1->shuxue,&p1->yingyu,&p1->wuli,&p1->huaxue,&p1->zhengzhi,&p1->dili,&p1->lishi,&p1->shengwu);
    }
    p2->next=NULL;
    return(head);
}

void print(struct student *head)
{
    struct student *p;
    printf("adfadf%d\n",n);
    p=head;
    if(head!=NULL)
        do
        {
            printf("%ld,%s,%d,%d,%d,%d,%d,%d,%d,%d,%d",p->xuehao,p->xingming,p->yuwen,p->shuxue,p->yingyu,p->wuli,p->huaxue,p->zhengzhi,p->dili,p->lishi,p->shengwu);
            p=p->next;
        }while(p!=NULL);
}

void main()
{
    struct student *head,stu;
    long del_num;
    printf("建立\n");
    head=creat();
    print(head);
}
搜索更多相关主题的帖子: int student struct 列表 define 
2008-07-28 01:19
StarWing83
Rank: 8Rank: 8
来 自:仙女座大星云
等 级:贵宾
威 望:19
帖 子:3951
专家分:748
注 册:2007-11-16
收藏
得分:0 
http://

给一篇文章你看看。

专心编程………
飞燕算法初级群:3996098
我的Blog
2008-07-28 09:29
woshiyun
Rank: 1
等 级:新手上路
威 望:2
帖 子:348
专家分:0
注 册:2008-6-16
收藏
得分:0 
把while(&p1->xuehao!=0)的&去掉,
把scanf("%ld,%s,%d,%d,%d,%d,%d,%d,%d,%d,%d"的逗号都去掉,有两个scanf啊
你再试试看吧
2008-07-28 09:40
独孤小梦
Rank: 1
等 级:新手上路
威 望:1
帖 子:338
专家分:0
注 册:2008-6-5
收藏
得分:0 
恩,我先试下!谢谢了!

编程小菜
2008-07-28 11:35
独孤小梦
Rank: 1
等 级:新手上路
威 望:1
帖 子:338
专家分:0
注 册:2008-6-5
收藏
得分:0 
把SCANF里的逗号去了以后都输入了30个数了,它还在提示输入……为什么会出现这个情况呢?

编程小菜
2008-07-28 12:02
StarWing83
Rank: 8Rank: 8
来 自:仙女座大星云
等 级:贵宾
威 望:19
帖 子:3951
专家分:748
注 册:2007-11-16
收藏
得分:0 
//原作者:独孤小梦,由StarWing修改于2008-07-28
#define _STARWING_ //去掉本行,则是独孤小梦的代码

#include"stdio.h"

#include <stdlib.h>
#include <string.h> //for memset

#ifdef _STARWING_
//一个宏,用于标明你认为一定成立的事实,我们叫它“断言”
//#include <assert.h>
#define Assert //assert
#else
//#include"malloc.h"
//#define NULL 0  // warning: "NULL" redefined
#endif

#ifndef _STARWING_
#define LEN sizeof(struct student)
struct student
{
    int xuehao;
    char xingming[10];
    int yuwen;
    int shuxue;
    int yingyu;
    int wuli;
    int huaxue;
    int zhengzhi;
    int dili;
    int lishi;
    int shengwu;
    struct student *next;
};
#else
//这不是一个好办法,如果你要增加一门课,修改输入输出会很麻烦,而且这么多名字,并不好管理。
//你可以为这个结构体专门写一个填充函数,把链表和数据分开管理
//这里给出一个稍微好一点的解决方法,注意使用英文的名字,比较好辨认

#define NAME_LEN 10
#define SCORE_COUNT 9

struct Student
{
    int id,score[SCORE_COUNT];
    char name[NAME_LEN];
};
struct StuList
{
    struct Student stu;
    struct StuList *next;
};
#endif
#ifndef _STARWING_
int n;
struct student *creat(void)
{
    struct student *head;
    struct student *p1,*p2;
    n=0;
    p1=p2=(struct student *)malloc(LEN);  //*开辟一个新单元*//
    scanf("%ld,%s,%d,%d,%d,%d,%d,%d,%d,%d,%d",&p1->xuehao,&p1->xingming,&p1->yuwen,&p1->shuxue,&p1->yingyu,&p1->wuli,&p1->huaxue,&p1->zhengzhi,&p1->dili,&p1->lishi,&p1->shengwu);
    head=NULL;
    while (&p1->xuehao!=0)
    {
        n=n+1;
        if (n==1)
            head=p1;
        else
            p2->next=p1;
        p2=p1;
        p1=(struct student *)malloc(LEN);
        scanf("%ld,%s,%d,%d,%d,%d,%d,%d,%d,%d,%d",&p1->xuehao,&p1->xingming,&p1->yuwen,&p1->shuxue,&p1->yingyu,&p1->wuli,&p1->huaxue,&p1->zhengzhi,&p1->dili,&p1->lishi,&p1->shengwu);
    }
    p2->next=NULL;
    return(head);
}

void print(struct student *head)
{
    struct student *p;
    printf("adfadf%d\n",n);
    p=head;
    if (head!=NULL)
        do
        {
            printf("%ld,%s,%d,%d,%d,%d,%d,%d,%d,%d,%d",p->xuehao,p->xingming,p->yuwen,p->shuxue,p->yingyu,p->wuli,p->huaxue,p->zhengzhi,p->dili,p->lishi,p->shengwu);
            p=p->next;
        }
        while (p!=NULL);
}

int main() //int main,而不是void main
{
    struct student *head,stu;
    long del_num;
    printf("建立\n");
    head=creat();
    print(head);
    return 0; // 注意是int main
}
#else
//你上面有两个函数,完成了“创建”和“打印”,这样不安全,至少需要有两方面的函数,创建和销毁。
//要注意这种思想。
//注意下面每个函数,都对参数进行了检查

//这个函数用于填充Student,(有一个小小的习惯,就是自己写的函数首字母大写,而库函数全小写,这样容易分辨)
void StuFill(struct Student* pstu)
{
    int i;
    Assert(pstu != NULL);
    memset(pstu,0,sizeof(struct Student));
    printf("Input Student's ID:");
    scanf("%d",&pstu->id);
    printf("Input Student's name:");
    scanf("%10s",pstu->name);
    printf("Input %d scores of %s:\n",SCORE_COUNT,pstu->name);
    for (i=0;i<SCORE_COUNT;++i)
        scanf("%d",&pstu->score[i]);
}

//打印一个学生结构的内容
void StuPrint(struct Student* pstu)
{
    int i;
    Assert(pstu != NULL);
    printf("ID:%d Name:%s\n",pstu->id,pstu->name);
    for (i=0;i<SCORE_COUNT;++i)
        printf("%3d%c",pstu->score[i],(i+1)%5?' ':'\n');
    printf("\n");
}

//在适当的位置插入一个新的元素
struct StuList* LinkInsert(struct StuList** ppnode)
{
    struct StuList *next= *ppnode;
    Assert(ppnode != NULL);
    *ppnode=(struct StuList*)malloc(sizeof(struct StuList));
    (*ppnode)->next=next;
    return *ppnode;
}

//释放掉所有的节点
void LinkFree(struct StuList* pnode)
{
    Assert(pnode != NULL);

    while (pnode)
    {
        struct StuList* next=pnode->next;
        free(pnode);
        pnode=next;
    }
}

//使用上面四个函数
int main()
{
    int i,n;
    struct StuList* link=NULL, *pt;
    printf("请输入学生数目:");
    scanf("%d",&n);
    for (i=0 ; i<n ; i++)
        StuFill(&LinkInsert(&link)->stu);
    for (pt=link ; pt!=NULL ; pt=pt->next)
        StuPrint(&pt->stu);
    LinkFree(link);
    return 0;
}

#endif


稍微修改了一下下…………

专心编程………
飞燕算法初级群:3996098
我的Blog
2008-07-28 15:13
StarWing83
Rank: 8Rank: 8
来 自:仙女座大星云
等 级:贵宾
威 望:19
帖 子:3951
专家分:748
注 册:2007-11-16
收藏
得分:0 
上面的main函数,保存学生的数据是反过来的,如果期望正向保存数据,使用下面的main函数:

//使用上面四个函数
int main()
{
    int i,n;
    struct StuList* link=NULL, *pt;
    printf("请输入学生数目:");
    scanf("%d",&n);
    StuFill(&LinkInsert(&link)->stu);
    for (pt=link,i=1 ; i<n ; pt=pt->next,i++)
        StuFill(&LinkInsert(&pt->next)->stu);
    for (pt=link ; pt!=NULL ; pt=pt->next)
        StuPrint(&pt->stu);
    LinkFree(link);
    return 0;
}

专心编程………
飞燕算法初级群:3996098
我的Blog
2008-07-28 15:22
StarWing83
Rank: 8Rank: 8
来 自:仙女座大星云
等 级:贵宾
威 望:19
帖 子:3951
专家分:748
注 册:2007-11-16
收藏
得分:0 
StuFill(&LinkInsert(&link)->stu);
这一句要注意一下,这里还可以这么写:
StuFill((struct Student*)LinkInsert(&link));
思考一下为什么这里可以强制转换。

专心编程………
飞燕算法初级群:3996098
我的Blog
2008-07-28 15:24
独孤小梦
Rank: 1
等 级:新手上路
威 望:1
帖 子:338
专家分:0
注 册:2008-6-5
收藏
得分:0 
以前收藏了这个贴,现在发现我居然没有给SW回复
谢谢SW的长久帮助!和技术支持!

编程小菜
2008-09-08 10:07
快速回复:关于列表问题!谢谢
数据加载中...
 
   



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

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