#include"stdio.h"
#include "stdlib.h"
#include"math.h"
struct list
{
//double coef;
int data;
struct list *next;
};
typedef struct list List;
#define End -327
List *Create(void)
{
int size=sizeof(List);
List *head,*p;
//double coef;
int data;
head=p=(List*)malloc(size);
//p->next=p;
scanf("%d",&data);
while(data!=-327)
{
p->next=(List*)malloc(size);
p=p->next;
p->data=data;
//p->exp=exp;
scanf("%d",&data);
}
//p->next=head;
p->next=NULL;
return head;
}
List *combine(List *a,List *b)
{
int size=sizeof(List);
//double x;
List *head,*p,*p1=a->next,*p2=b->next;
head=p=(List*)malloc(size);
p->next=p;
while(p1!=NULL&&p2!=NULL)
{
if(p1->data<p2->data)
{
p->next=(List*)malloc(size);
p=p->next;
//p->coef=p1->coef;
p->data=p1->data;
p1=p1->next;
}
else
{
p->next=(List*)malloc(size);
p=p->next;
//p->coef=p2->coef;
p->data=p2->data;
p2=p2->next;
}
}
while(p1!=NULL)
{
p->next=(List*)malloc(size);
p=p->next;
//p->coef=p1->coef;
p->data=p1->data;
p1=p1->next;
}
while(p2!=NULL)
{
p->next=(List*)malloc(size);
p=p->next;
//p->coef=p2->coef;
p->data=p2->data;
p2=p2->next;
}
p->next=NULL;
return head;
}
void Display(List *head)
{
List *p=head->next;
if(p==NULL)
printf("\nlist null!\n");
else
{
while(p!=NULL)
{
printf("%3d\n",p->data);
p=p->next;
}
//printf("\b \n");
}
}
int main(void )
{
List *a,*b,*c;
printf("input the a list data:\n");
a=Create();
printf("input the b list data:\n");
b=Create();
c=combine(a,b);
printf("the a list is:\n");
Display(a);
printf("the b list is:\n");
Display(b);
printf("the combined list is:\n");
Display(c);
return 0;
}
评论0