#include
#include
struct student {
char name;
int age;
int no;
struct student *next;
};
main() {
struct student create(void) {
struct student *p1,*p2,*head;
head = NULL;
p1 = p2 = (struct student)malloc(sizeof(struct student));
scanf("%c,%d,%d",p1->name,p1->age,p1->no);
while (p1->no != 0) {
if (p1->no == 1)
head = p1;
else
p2->next = p1;
p2 = p1;
p1 = (struct student*)malloc(sizeof(struct student));
scanf("%c,%d,%d",p1->name,p1->age,p1->no);
}
p2->next = NULL;
return head;
}
}
要把debug那些提示也应该传上来
#include "stdafx.h"
#include <stdio.h>
#include <malloc.h>
struct student
{
char name;
int age;
int no;
student *next;
};
student * create(void)
{
student *p1,*p2,*head;
head = NULL;
p1 = p2 = (student*)malloc(sizeof(student));
scanf("%c,%d,%d",&p1->name,&p1->age,&p1->no);
while (p1->no != 0)
{
if (p1->no == 1)
head = p1;
else
p2->next = p1;
p2 = p1;
p1 = (student*)malloc(sizeof(student));
scanf("%c,%d,%d",&p1->name,&p1->age,&p1->no);
}
p2->next = NULL;
return head;
}
int main()
{
create();
}
首先你根本程序不能编译的,错了很多,我帮你修改了下。然后你scanf("%c,%d,%d",&p1->name,&p1->age,&p1->no);忘记取地址了。
你有一点点问题
1. 建议成后看一看代码风格,比如3楼这样, 看起来就很清爽,是不是啊 :D
2. create() 函数要在 main函数之前声明;实现可以在前面也可以在后面。不能嵌套声明。
3. create()函数的返回值, 你是要将head 返回,而head是一个指针类型,所以你函数的返回类型应该是指针类型struct student * 。
4. malloc()函数返回的是 void * 类型,因此你做强制转换的时候应该转换成指针类型struct student * 。
5. scanf()后面的参数列表,要加上取地址符。
就这些了。 我改了改你的代码,作个参考 ~~ 。
#include
#include
struct student {
char name;
int age;
int no;
struct student *next;
};
struct student *create(void)
{
struct student *p1, *p2, *head = NULL;
p1 = p2 = (struct student *)malloc(sizeof(struct student));
scanf("%c,%d,%d", &p1->name, &p1->age, &p1->no);
while (p1->no != 0)
{
if (p1->no == 1)
head = p1;
else
p2->next = p1;
p2 = p1;
p1 = (struct student*)malloc(sizeof(struct student));
scanf("%c,%d,%d", &p1->name, &p1->age, &p1->no);
}
p2->next = NULL;
return head;
}
main()
{
create();
}
不好意思,刚回复的时候少打了几个字,是你的程序有一点点问题,别介意哈 ~~ 。
看着楼上的大神给出的答案,不错不错
你说的debug的提示是什么?可以发上来吗?