链表的输入与输出
#include<stdio.h>#include<stdlib.h>
#define LEN sizeof(struct number)
struct number
{
int num;
struct number *next;
};
int n;
struct number *creat(int k)
{
struct number *head,*p1,*p2;
n=0;
p1=p2=(struct number *)malloc(LEN);
scanf("%d ",&p1->num);
head=NULL;
while(n<k-1)
{
n++;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct number *)malloc(LEN);
scanf(" %d",&p1->num);
}
p2->next=NULL;
return(head);
}
void print(struct number *head,int k)
{
struct number *p;
n=0;
p=head;
if(head!=NULL)
do
{
printf("%d ",p->num);
p=p->next;
}while(p!=NULL);
}
int main()
{
int N,M;
struct number *pt1,*pt2;
scanf("%d",&N);
pt1=creat(N);
scanf("%d",&M);
pt2=creat(M);
print(pt1,N);
print(pt2,M);
printf("\n");
return 0;
}
咋结果为:
输入:
5
1 2 3 4 5
6
11 22 33 44 55 66
输出:
1 2 3 4 11 22 33 44 55
咋是5和66没输出啊!!