读书人

链表链接有关问题

发布时间: 2012-03-21 13:33:15 作者: rapoo

链表链接问题
#include <stdio.h>
#include <malloc.h>
#define LON sizeof(struct student)
#define NULL 0
struct student
{
int num;
int score;
struct student *next;
};

int n;

void main()
{
struct student *head1,*head2,*head;
struct student *creat(void);
struct student *join(struct student *head1,struct student *head2);
void print(struct student *head);
printf( "input 1:\n ");
head1=creat();
printf( "input 2:\n ");
head2=creat();
head=join(head1,head2);
print(head);
getch();
}


struct student *creat(void)
{
struct student *head,*p1,*p2;
n=0;
p1=p2=(struct student *)malloc(LON);
scanf( "%d%d ",&p1-> num,&p2-> score);
head=NULL;
while(p1-> num!=0)
{
n=n+1;
if(n==1) head=p1;
else p2-> next=p1;
p2=p1;
p1=(struct student *)malloc(LON);
scanf( "%d%d ",&p1-> num,&p2-> score);
}
p2-> next=NULL;
return(head);
}

struct student *join(struct student *head1,struct student *head2)
{
struct student *p1;
p1=head1;

while(p1-> next!=NULL)

p1=p1-> next;
if(p1-> next==NULL)
p1-> next=head2;

return(head1);
}


void print(struct student *head)
{
struct student *p;
p=head;

while(p!=NULL)
{
printf( "%d %d\n ",p-> num,p-> score);


p=p-> next;
}
}
把2个链表连接起来了,但是输出的数据和输入的不一样

[解决办法]
scanf( "%d%d ",&p1-> num,&p2-> score);
————————————————————
scanf( "%d%d ",&p1-> num,&p1-> score);

读书人网 >C语言

热点推荐