题目:
思路:
在做这个题之前呢,首先得看懂理解整个题的题意:
或者说最后一步打印之前再加一个删除底数为0的节点操作再来打印也是可以的:
代码一:
#include<stdio.h>
#include<stdlib.h>
typedef struct SList
{
int data;
int index;
struct SList* next;
}SL;
void SLInit(SL** ps)
{
(*ps) = (SL*)malloc(sizeof(SL));
(*ps)->next = NULL;
}
void SLCreate(SL** ps)
{
SL* cur = *ps;
int x1 = 0;
int x2 = 0;
while (1)
{
scanf("%d,%d", &x1, &x2);
getchar();
if (x1 == 0 && x2 == 0)
{
cur->next = NULL;
break;
}
SL* newnode = (SL*)malloc(sizeof(SL));
newnode->data = x1;
newnode->index = x2;
cur->next = newnode;
cur = newnode;
}
}
void SLLink(SL** psA, SL** psB)
{
SL* tail = (*psA)->next;
while (tail->next)
{
tail = tail->next;
}
SL* curB = (*psB)->next;
while (curB)
{
SL* curA = (*psA)->next;
int flag = 0;
while (curA)
{
if (curB->index == curA->index)
{
flag = 1;
curA->data = (curA->data) + (curB->data);
}
curA = curA->next;
}
if (flag == 0)
{
SL* newnode = (SL*)malloc(sizeof(SL));
newnode->data = curB->data;
newnode->index = curB->index;
tail->next = newnode;
tail = newnode;
tail->next = NULL;
}
curB = curB->next;
}
}
void SLSort(SL** ps)
{
SL* cur = (*ps)->next;
SL* prev = (*ps)->next;
while (prev)
{
cur = prev;
while (cur)
{
if (cur && cur->index < prev->index)
{
//交换一下
int tmp1 = prev->data;
prev->data = cur->data;
cur->data = tmp1;
int tmp2 = prev->index;
prev->index = cur->index;
cur->index = tmp2;
}
cur = cur->next;
}
prev = prev->next;
}
}
void SLPrint(SL** ps)
{
SL* cur = (*ps)->next;
while (cur)
{
if (cur->data == 0)
{
cur = cur->next;
}
else
{
printf("%dx^%d", cur->data, cur->index);
if (cur->next != NULL)
{
printf("+");
}
cur = cur->next;
}
}
}
int main()
{
SL* LA = NULL;
SLInit(&LA);
SL* LB = NULL;
SLInit(&LB);
SLCreate(&LA);
SLCreate(&LB);
SLLink(&LA, &LB);
SLSort(&LA);
SLPrint(&LA);
return 0;
}
代码二:
#include<stdio.h>
#include<stdlib.h>
typedef struct SList
{
int data;
int index;
struct SList* next;
}SL;
void SLInit(SL** ps)
{
(*ps) = (SL*)malloc(sizeof(SL));
(*ps)->next = NULL;
}
void SLCreate(SL** ps)
{
SL* cur = *ps;
int x1 = 0;
int x2 = 0;
while (1)
{
scanf("%d,%d", &x1, &x2);
getchar();
if (x1 == 0 && x2 == 0)
{
cur->next = NULL;
break;
}
SL* newnode = (SL*)malloc(sizeof(SL));
newnode->data = x1;
newnode->index = x2;
cur->next = newnode;
cur = newnode;
}
}
void SLLink(SL** psA, SL** psB)
{
SL* tail = (*psA)->next;
while (tail->next)
{
tail = tail->next;
}
SL* curB = (*psB)->next;
while (curB)
{
SL* curA = (*psA)->next;
int flag = 0;
while (curA)
{
if (curB->index == curA->index)
{
flag = 1;
curA->data = (curA->data) + (curB->data);
}
curA = curA->next;
}
if (flag == 0)
{
SL* newnode = (SL*)malloc(sizeof(SL));
newnode->data = curB->data;
newnode->index = curB->index;
tail->next = newnode;
tail = newnode;
tail->next = NULL;
}
curB = curB->next;
}
}
void SLSort(SL** ps)
{
SL* cur = (*ps)->next;
SL* prev = (*ps)->next;
while (prev)
{
cur = prev;
while (cur)
{
if (cur && cur->index < prev->index)
{
//交换一下
int tmp1 = prev->data;
prev->data = cur->data;
cur->data = tmp1;
int tmp2 = prev->index;
prev->index = cur->index;
cur->index = tmp2;
}
cur = cur->next;
}
prev = prev->next;
}
}
void SLPop(SL** ps)
{
SL* cur = (*ps)->next;
SL* prev = NULL;
while (cur)
{
if (cur->data == 0)
{
prev->next = cur->next;
free(cur);
cur = prev->next;
}
else
{
prev = cur;
cur = cur->next;
}
}
}//运用pop试一下
void SLPrint(SL** ps)
{
SL* cur = (*ps)->next;
while (cur)
{
printf("%dx^%d", cur->data, cur->index);
if (cur->next != NULL)
{
printf("+");
}
cur = cur->next;
}
}
int main()
{
SL* LA = NULL;
SLInit(&LA);
SL* LB = NULL;
SLInit(&LB);
SLCreate(&LA);
SLCreate(&LB);
SLLink(&LA, &LB);
SLSort(&LA);
SLPop(&LA);
SLPrint(&LA);
return 0;
}