#include <stdio.h>
#include <stdlib.h>
typedef struct ListNode
{
int ratio;
int expton;
struct ListNode *next;
}List;
List *CreatList ()
{
List *e1, *e2, *L;
L = NULL;
int expton;
printf("Enter expton:");
scanf("%d", &expton);
while(expton != -1) //-1表示输入的结束
{
e1 = (List *)malloc(sizeof(struct ListNode));
e1->expton = expton;
e1->next = NULL;
printf("Enter ratio:");
scanf("%d", &(e1->ratio));
if(!L)
{
L = e1;
}
else
{
e2->next = e1;
}
e2 = e1;
printf("Enter expton:");
scanf("%d", &expton);
}
return L;
}
void PrintList (List *L)
{
List *e;
e = L;
for(e; e != NULL; e = e->next)
{
printf("%d %d\n", e->ratio, e->expton);
}
}
List *AddList (List *L1, List *L2)
{
List *L3, *e1, *e2;
L3 = NULL;
while(L1 && L2)
{
e1 = (List *)malloc(sizeof(struct ListNode));
e1->next = NULL;
if(L1->expton == L2->expton)
{
e1->expton = L1->expton;
e1->ratio = L1->ratio + L2->ratio;
L1 = L1->next;
L2 = L2->next;
}
else if(L1->expton > L2->expton)
{
e1->expton = L1->expton;
e1->ratio = L1->ratio;
L1 = L1->next;
}
else
{
e1->expton = L2->expton;
e1->ratio = L2->ratio;
L2 = L2->next;
}
if(!L3)
{
L3 = e1;
}
else
{
e2->next = e1;
}
e2 = e1;
}
while(L1)
{
e1 = (List *)malloc(sizeof(struct ListNode));
e1->next = NULL;
e1->expton = L1->expton;
e1->ratio = L1->ratio;
L1 = L1->next;
e2->next = e1;
e2 = e1;
}
while(L2)
{
e1 = (List *)malloc(sizeof(struct ListNode));
e1->next = NULL;
e1->expton = L2->expton;
e1->ratio = L2->ratio;
L2 = L2->next;
e2->next = e1;
e2 = e1;
}
return L3;
}
int main ()
{
List *L1, *L2, *L3;
L1 = CreatList();
L2 = CreatList();
L3 = AddList(L1, L2);
PrintList(L3);
return 0;
}
版权声明:本文为sinat_28826891原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。