创建链表问题:提交与自己学号相邻的两位同学的学号与一门考试成绩,编程建立由这三组数据结点组成的简单链表。
我在CSDN上允许各种案例,输出都是正确的,但是到了希冀平台上就是输出有问题
#include
#include
typedef struct Lnode* node;
struct Lnode
{
int num, score;
struct Lnode next;
};
node createlist()
{
struct Lnode lis=(struct Lnode)malloc(sizeof(struct Lnode));
lis->next = NULL;
lis->num = 0;
return lis;
}
void addelement(struct Lnode p)
{
struct Lnode newone= (struct Lnode)malloc(sizeof(struct Lnode));
newone->next = NULL;
struct Lnode* nextp=p;
for (int i = 0; i < p->num; i++)
nextp = nextp->next;
nextp->next = newone;
p->num++;
scanf("%d,%d", &newone->num, &newone->score);
}
void outelement(struct Lnode* p)
{
struct Lnode* nextp = p;
for (int t = 0; t < p->num; t++)
{
printf("[num=%d,score=%d]", nextp->next->num, nextp->next->score);
if (i != p->num - 1)
printf("\n");
nextp = nextp->next;
}
}
int main()
{
struct Lnode *p;
p = createlist();
for (int t= 0; t < 3; i++)
{
addelement(p);
}
outelement(p);
return 0;
}
有思考过空格问题,但是改了几次还是不行
重新修改,输入时注意学号和分数间的逗号 ‘,' 是英文半角的,供参考:
#include <stdio.h>
#include <stdlib.h>
typedef struct Lnode* node;
struct Lnode
{
int num, score;
struct Lnode* next;
};
node createlist()
{
struct Lnode* lis = (struct Lnode*)malloc(sizeof(struct Lnode));
lis->next = NULL;
lis->num = 0;
return lis;
}
void addelement(struct Lnode* p)
{
struct Lnode* newone = (struct Lnode*)malloc(sizeof(struct Lnode));
newone->next = NULL;
scanf("%d,%d", &newone->num, &newone->score);
struct Lnode* nextp = p;
for (nextp = p;nextp->next != NULL; nextp = nextp->next);
nextp->next = newone;
p->num++;
}
void outelement(struct Lnode* p)
{
struct Lnode* nextp = p;
for (nextp = p;nextp->next != NULL;nextp = nextp->next)
{
printf("[num=%d,score=%d]", nextp->next->num, nextp->next->score);
if (nextp->next->next != NULL)
printf("\n");
}
}
int main()
{
struct Lnode *p;
p = createlist();
for (int t = 0; t < 3; t++)
{
addelement(p);
}
outelement(p);
return 0;
}
这里有问题就是 struct Lnode 不是指针类型,但是很多地方把它当指针了。
比如
struct Lnode lis=(struct Lnode)malloc(sizeof(struct Lnode));
应该是
struct Lnode* lis=(struct Lnode*)malloc(sizeof(struct Lnode))
原来代码结构那里有星号,复制不上去
不知道你这个问题是否已经解决, 如果还没有解决的话: