student.h 头文件 没找到,检查一下在不在项目里
不知道你这个问题是否已经解决, 如果还没有解决的话:示例
输入:1->2->4, 1->3->4
输出:1->1->2->3->4->4
上面的格式写不出…
我的格式
输入:
1 2 4
1 3 4
输出:
1->1->2->3->4->4
#include<stdio.h>
#include <stdlib.h>
struct ListNode {
int number;
struct ListNode *next;
};
struct ListNode *printlist( struct ListNode *head )
{
struct ListNode *p = head;
while(p)
{
printf("%d", p->number);
if(p->next != NULL)
{
printf(" -> ");
}
p = p->next;
}
}
struct ListNode *readlist();
struct ListNode *creatlist(struct ListNode *head_1, struct ListNode *head_2);
int main(void)
{
struct ListNode *head_1, *head_2, *head;
head_1 = readlist();
head_2 = readlist();
head = creatlist(head_1, head_2);
printlist(head);
return 0;
}
struct ListNode *readlist()
{
struct ListNode *p, *head, *tail;
head = tail = NULL;
p = ( struct ListNode *) malloc (sizeof(struct ListNode));
scanf("%d", &p->number);
while( p->number!=-1)
{
if(head == NULL)
{
head = p;
head->next = NULL;
}
if(tail != NULL)
{
tail->next = p;
}
tail = p;
tail->next = NULL;
p = ( struct ListNode *) malloc (sizeof(struct ListNode));
scanf("%d", &p->number);
}
return head;
}
struct ListNode *creatlist(struct ListNode *head_1, struct ListNode *head_2)
{
struct ListNode *head, *q, *tail, *h1, *h2;
q = (struct ListNode*) malloc(sizeof(struct ListNode));
h1 = head_1, h2 = head_2;
head = tail =NULL;
while(h1 && h2)
{
if(h1->number < h2->number)
{
if(head == NULL)
{
head = q;
head->next = NULL;
}
if(tail != NULL)
{
tail->next = q;
}
q->number = h1->number;
tail = q;
tail->next = NULL;
q = (struct ListNode*) malloc(sizeof(struct ListNode));
h1 = h1->next;
}
else
{
if(head==NULL)
{
head=q;
head->next=NULL;
}
if(tail!=NULL)
{
tail->next = q;
}
q->number = h2->number;
tail = q;
tail->next = NULL;
q = (struct ListNode*) malloc(sizeof(struct ListNode));
h2 = h2->next;
}
}
if(h1==NULL)
{
while(h2)
{
if(tail!=NULL)
{
tail->next = q;
}
q->number = h2->number;
tail = q;
tail->next = NULL;
q = (struct ListNode*) malloc(sizeof(struct ListNode));
h2 = h2->next;
}
}
else
{
while(h1)
{
if(tail != NULL)
{
tail->next = q;
}
q->number = h1->number;
tail = q;
tail->next = NULL;
q = (struct ListNode*) malloc(sizeof(struct ListNode));
h1 = h1->next;
}
}
return head;
}