主题:帮忙看看!
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define NULL 0
typedef struct student
{
long num;
struct student *next;
}Link;
struct student *creat();
void print(struct student *head);
struct student *creat()
{
struct student *head,*p1,*p2;
p1=(struct student*)malloc(sizeof(struct student));
p2=(struct student*)malloc(sizeof(struct student));
head=NULL;
int n=0;
scanf("%ld ",&p1->num);
while(p1->num)
{
n=n+1;
//p1 =*Link;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student*)malloc(sizeof(struct student));
scanf("%ld ",&p1->num);
}
p2->next=NULL;
return(head);
}
void print(struct student *head)
{
struct student *p;
p=head;
if(head)
do{
printf("this is %ld ",p->num);
p=p->next;
}while(p);
}
main()
{
struct student *head;
head=creat();//创建链表
print(head);//输出链表
return 0;
没错误,但不是我要的结果.为什么?
#include<stdlib.h>
#include<malloc.h>
#define NULL 0
typedef struct student
{
long num;
struct student *next;
}Link;
struct student *creat();
void print(struct student *head);
struct student *creat()
{
struct student *head,*p1,*p2;
p1=(struct student*)malloc(sizeof(struct student));
p2=(struct student*)malloc(sizeof(struct student));
head=NULL;
int n=0;
scanf("%ld ",&p1->num);
while(p1->num)
{
n=n+1;
//p1 =*Link;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student*)malloc(sizeof(struct student));
scanf("%ld ",&p1->num);
}
p2->next=NULL;
return(head);
}
void print(struct student *head)
{
struct student *p;
p=head;
if(head)
do{
printf("this is %ld ",p->num);
p=p->next;
}while(p);
}
main()
{
struct student *head;
head=creat();//创建链表
print(head);//输出链表
return 0;
没错误,但不是我要的结果.为什么?