关于老谭链表的问题
运行结果不大对,不知道怎么搞的,那位高人帮忙看看吧先谢谢啦
#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);
}
#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;
----------------解决方案--------------------------------------------------------
再次感谢啦
----------------解决方案--------------------------------------------------------