当前位置: 代码迷 >> C语言 >> 关于老谭链表的问题
  详细解决方案

关于老谭链表的问题

热度:158   发布时间:2008-05-06 17:46:25.0
关于老谭链表的问题
运行结果不大对,不知道怎么搞的,那位高人帮忙看看吧
先谢谢啦
#include    <stdio.h>
#include    <malloc.h>
#define        NULL    0
#define        LEN        sizeof(struct student)
struct student
{
    long num;
    double score;
    struct student *next;};

int n;

struct student *creat(void)//制作链表函数
{
    n = 0;
    struct student * head;
    struct student * p1, * p2;
    p2 = p1 = (struct student *) malloc(LEN);
    scanf("%ld,%f", &p1 -> num, &p1 -> score);
    head = NULL;
    while(p1 -> num != NULL)
    {
        n = n + 1;
        if(n == 1)    head = p1;
        else    p2 -> next = p1;
        p2 = p1;
        p1 = (struct student *) malloc(LEN);
        scanf("%ld,%f", &p1 -> num, &p1 -> score);
    }
    p2 -> next = NULL;
    return (head);
}

void print(struct student * head)//输出链表函数
{
    struct student * p;
    printf("%d学生记录如下:\n", n);
    p = head;
    if(head != NULL)
        do
        {
            printf("%ld %5.1f\n", p -> num, p -> score);
            p = p -> next;
        }while(p != NULL);
        
}

void main()
{
    struct student *head;
    printf("输入学生学号和成绩:\n");
    head = creat();
    print(head);
}
搜索更多相关的解决方案: 链表  

----------------解决方案--------------------------------------------------------
scanf("%ld,%lf", &p1 -> num, &p1 -> score);
----------------解决方案--------------------------------------------------------
为什么呢?
老谭的书上没有l呢
不过问题的确解决了
十分感谢
----------------解决方案--------------------------------------------------------
VC中输入double型,必须为%lf;
----------------解决方案--------------------------------------------------------
再次感谢啦

----------------解决方案--------------------------------------------------------
  相关解决方案