题目
C语言两种实现
仍旧跟上一篇文章说明的一样,单链表的头节点有两种实现方式,一种是头节点保存数据,另一种是头节点作为哨兵只占位,head->next才是第一个有数据的节点。
头节点保存数据
// polyA和polyB多项式相加,每个输入分别有系数和指数,以0,0结束
//相加规则,每个多项式按照指数从小到大展示,同指数的系数相加,系数为0则该项移除。
//head为第一个有数节点
#include<stdio.h>
#include<stdlib.h>
typedef struct Node {
int coef;
int exp;
struct Node *next;
} *node;
node createList() {
int coef, exp;
node head,a, b;
head = NULL;
while (1)
{
scanf("%d,%d", &coef, &exp);
if(coef==exp && exp==0) {
break;
}
b = (node)malloc(sizeof(node));
b->coef = coef;
b->exp = exp;
b->next = NULL;
if(head==NULL) {
a=head=b;
} else {
a->next = b;
a = b;
}
}
return head;
}
//冒泡排序,指数小在前
void sortExp(node head) {
node a,b;
int coef, exp;
for (a=head; a->next !=NULL; a = a->next)
{
for(b = a->next; b != NULL; b = b->next){
if(b->exp < a->exp) {
coef = a->coef;
exp = a->exp;
a->coef = b->coef;
a->exp = b->exp;
b->coef = coef;
b->exp = exp;
}
}
}
}
node polyAdd(node head1, node head2) {
node a1, b1, a2, b2, head, p;
a1 = head1;
b1 = a1->next;
a2 = head2;
b2 = a2->next;
head = (node)malloc(sizeof(node));
head->next = NULL;
p = head;
while ( a1 != NULL && a2 != NULL)
{
if (a1->exp < a2->exp) {
p->next = a1;
a1 = a1->next;
p = p->next;
} else if (a2->exp < a1->exp) {
p->next = a2;
a2 = a2->next;
p = p->next;
} else if (a1->coef+a2->coef==0) {
node tmp1 = a1->next;
node tmp2 = a2->next;
free(a1);
free(a2);
a1 = tmp1;
a2 = tmp2;
} else {
a1->coef += a2->coef;
p->next = a1;
a1 = a1->next;
node tmp = a2->next;
free(a2);
a2 = tmp;
p = p->next;
}
}
if (a1 != NULL) {
p->next = a1;
} else {
p->next = a2;
}
return head->next;
}
void print(node head) {
node p = head;
while(p) {
printf("%d,%d ", p->coef, p->exp);
p = p->next;
}
}
int main() {
node h1, h2,h;
h1 = createList();
sortExp(h1);
h2 = createList();
sortExp(h2);
h = polyAdd(h1, h2);
print(h);
return 0;
}
头节点哨兵占位
// polyA和polyB多项式相加,每个输入分别有系数和指数,以0,0结束
//相加规则,每个多项式按照指数从小到大展示,同指数的系数相加,系数为0则该项移除。
//head->next为第一个有数节点
#include<stdio.h>
#include<stdlib.h>
typedef struct Node {
int coef;
int exp;
struct Node *next;
} *node;
void createList(node head) {
int coef,exp;
node a,b;
a = head;
while (1)
{
scanf("%d,%d", &coef, &exp);
if(coef==exp&&exp==0) {
break;
}
b = (node)malloc(sizeof(node));
b->coef = coef;
b->exp = exp;
b->next = NULL;
a->next = b;
a = b;
}
}
//冒泡排序,指数小在前
void sortExp(node head) {
node a, b;
int coef, exp;
for(a = head->next; a->next; a = a->next) {
for(b=a->next; b; b = b->next) {
if(b->exp < a->exp) {
coef = b->coef;
exp = b->exp;
a->coef = b->coef;
a->exp = b->exp;
b->coef = coef;
b->exp = exp;
}
}
}
}
void print(node head) {
node p = head->next;
while (p->next)
{
printf("%d,%d ", p->coef, p->exp);
p = p->next;
}
printf("%d,%d", p->coef, p->exp);
}
node polyAdd(node h1, node h2) {
node a, b, head, p;
a = h1->next;
b = h2->next;
head = (node)malloc(sizeof(node));
head->next = NULL;
p = head;
while (a && b)
{
//a指数小
if (a->exp < b->exp) {
p->next = a;
a = a->next;
p = p->next;
}
//b指数小
else if (b->exp < a->exp) {
p->next = b;
b = b->next;
p = p->next;
}
//指数相等,系数和为0
else if (a->coef+b->coef==0) {
node t1 = a->next;
node t2 = b->next;
free(a);
free(b);
a = t1;
b = t2;
}
//指数相等,系数和不为0
else {
a->coef += b->coef;
p->next = a;
p = p->next;
a = a->next;
node t = b->next;
free(b);
b = t;
}
}
//剩余节点连接
if (a) {
p->next = a;
} else {
p->next = b;
}
return head;
}
int main() {
node h1, h2, h;
h1 = (node)malloc(sizeof(node));
h2 = (node)malloc(sizeof(node));
h1->next = NULL;
h2->next = NULL;
createList(h1);
sortExp(h1);
createList(h2);
sortExp(h2);
h = polyAdd(h1, h2);
print(h);
return 0;
}