#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct Student)
struct Student //创建结构体
{
int num; //存放数据
struct Student *next; //指针
};
struct Student *creat() //构造创建链表函数
{
struct Student *head,*p1,*p2;
head = (struct Student *)malloc(LEN); //建立头结点
p1 = p2 = (struct Student *)malloc(LEN);//首元结点
scanf("%d",&p1->num);
head->next = p1; //将头结点与首元结点链接
while(p1->num != 0) //当数据不为0是执行循环,否则停止创建链表
{
p1 = (struct Student*)malloc(LEN);
p2->next = p1;
p2 = p1;
scanf("%d",&p1->next);
}
p2->next = NULL;
return head;
}
struct Student *sort(void) //两个链表合并
{
struct Student *head1,*head2,*head,*p,*p1,*h1;
p = head;
head1 = creat(); //调用创建链表函数
head2 = creat();
p1 = head1->next; //p1指向创建链表的首元结点
h1 = head2->next;
head = head1; //用一个指针指向两个创建链表的中任意一个头结点
while(p1->next != NULL && h1->next != NULL)
{
if(p1->num > h1->num)
{
p->next = p1;
p = p1;
p1 = p1->next;
}
else
{
p->next = h1;
p = h1;
h1 = h1->next;
}
}
p->next = (p1 != NULL)?p1:h1;
return(head);
}
void print(struct Student *head) //构造输出函数
{
struct Student *h;
h = head->next; //h指向首元节点
if(h == NULL){
printf("null");
return;
}
while(h != NULL)
{
printf("%d ",h->num);
h = h->next;
}
}
int main()
{
struct Student *head;
head = sort();
print(head);
return 0;
}
scanf("%d",&p1->next);
改成
scanf("%d",&p1->num);