程序代码:
#include<stdio.h>
#include<stdlib.h>
struct ListNode
{
int data;
struct ListNode* next;
};
typedef struct ListNode node;
int main()
{
int i;
node l1,l2,l3;
l1.next=NULL;
l2.next = NULL;
l3.next = NULL;
node* p = &l1;
node* tmp;
char c;
while((scanf("%d",&i))&&i!=-1)
{
node* s = (node*)malloc(sizeof(node));
s->data = i;
s->next = NULL;
p->next=s;
p=p->next;
}
p=&l2;
while((scanf("%d",&i))&&i!=-1)
{
node* s = (node*)malloc(sizeof(node));
s->data = i;
s->next = NULL;
p->next=s;
p=p->next;
}
p=l1.next;
node *q=l2.next;
node *m = &l3;
while((p!=NULL)&&(q!=NULL))
{
if((p->data)<(q->data))
{
node* s = (node*)malloc(sizeof(node));
s->data = p->data;
s->next = NULL;
m->next = s;
m= m->next;
p = p->next;
tmp = s;
}
else if((p->data)>(q->data))
{
node* s = (node*)malloc(sizeof(node));
s->data = q->data;
s->next = NULL;
m->next = s;
m= m->next;
//p = p->next;
q = q->next;
tmp =s;
}
else if((p->data)==(q->data))
{
node* s = (node*)malloc(sizeof(node));
s->data = p->data;
s->next = NULL;
m->next = s;
m = m->next;
p = p->next;
node* s1 = (node*)malloc(sizeof(node));
s1->data = q->data;
s1->next = NULL;
m->next = s1;
m=m->next;
q=q->next;
tmp =s1;
}
}
if(p!=NULL)
{
tmp->next = p;
}
else if(q!=NULL)
{
tmp->next = q;
}
m=l3.next;
if(l3.next==NULL)
{ printf("NULL");return 0;}
while(m!=NULL)
{
if(m->next==NULL)
printf("%d",m->data);
else
printf("%d ",m->data);
m=m->next;
}
return 0;
}
运行结果: