链表问题

top啦它 2020-03-12 11:42:48
这道题意思是可以输入多实例的,我怕出错,这个代码只能输入一组数据
Input
输入数据包含多组测试数据,每组数据包含两行一元多项式。
每行多项式首先输入一个n,代表多项式的项数,接着是n对整数,每对整数的第一个是系数,第二个是指数。每个多项式不超过100项,整数间用空格隔开,并且指数是递减的。
Output
每组测试数据输出一行结果,每个整数后面空一格。(包括行尾)
Sample Input
3 8 2 4 1 7 0
2 4 4 1 1
1 2 3
1 1 4
3 5 2 4 1 7 0
2 1 4 -4 1
Sample Output/////这个输出是,就比如input中的第一行第一组 8 2,第二行中的4 4,因为4 > 2,所以head2就将4 4这组数弄了进去
4 4 8 2 5 1 7 0
1 4 2 3
1 4 5 2 7 0
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct list{
int xishu,zhishu,dat;//定义系数、指数、dat(这个后面要用到,随便起了个名字)
struct list *next;
}data;
void Greatlist(data *head);//尾插发创造链表
void sor_t(data *head0,data *head1,data *head2);
void print(data *head2);//输出head2
int main(void)
{
data *head0,*head1,*head2;
head0 = (data *)malloc(sizeof(data));
head1 = (data *)malloc(sizeof(data));
head2 = (data *)malloc(sizeof(data));
head0->next = NULL,head1->next = NULL,head2->next = NULL;
Greatlist(head0);
Greatlist(head1);
sor_t(head0,head1,head2);
print(head2);
return 0;
}
void Greatlist(data *head)
{
int n;
scanf("%d",&n);
data *p,*pre = head;
while(n--)
{
p = (data *)malloc(sizeof(data));
scanf("%d%d",&p->xishu,&p->zhishu);
pre->next = p;
pre = p;
}
pre->next = NULL;
}
void sor_t(data *head0,data *head1,data *head2)
{
data *p2,*pre0 = head0,*pre1 = head1,*pre2 = head2;
pre0 = head0->next,pre1 = head1->next;
if(pre0->zhishu > pre1->zhishu)
{
p2 = (data *)malloc(sizeof(data));
p2->dat = pre0->xishu;
pre2->next = p2;
pre2 = p2;
pre2->next = NULL;
p2 = (data *)malloc(sizeof(data));
p2->dat = pre0->zhishu;
pre2->next = p2;
pre2 = p2;
pre2->next = NULL;
pre0 = pre0->next;
}
else if(pre0->zhishu < pre1->zhishu)//比如input中的第一行第一组 8 2,第二行中的4 4,因为4 > 2,所以head2就将4 4这组数弄了进去
{
p2 = (data *)malloc(sizeof(data));
p2->dat = pre1->xishu;
pre2->next = p2;
pre2 = p2;
pre2->next = NULL;
p2 = (data *)malloc(sizeof(data));
p2->dat = pre1->zhishu;
pre2->next = p2;
pre2 = p2;
pre2->next = NULL;
pre1 = pre1->next;
}
else if(pre0->zhishu == pre1->zhishu)//如果第一行数和第二行数的对应组的指数相等,就比如一组数为3 2,另一组为4 2,那么就相加,即 输出7 2这一组数,应该是这个意思吧
{
p2 = (data *)malloc(sizeof(data));
p2->dat = pre0->xishu + pre1->xishu;
pre2->next = p2;
pre2 = p2;
pre2->next = NULL;
p2 = (data *)malloc(sizeof(data));
p2->dat = pre0->zhishu;//或者 p2->dat = pre1->zhishu;因为此时pre0->zhishu等于pre1->zhishu
pre2->next = p2;
pre2 = p2;
pre2->next = NULL;
pre0 = pre0->next,pre1 = pre1->next;
}
}
void print(data *head3)
{
data *p = head3->next;
while(p)
{
printf("%d %d ",p->xishu,p->zhishu);
p = p->next;
}
printf("\n");
}
...全文
98 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
top啦它 2020-03-12
  • 打赏
  • 举报
回复
求解,多谢了
寻开心 2020-03-12
  • 打赏
  • 举报
回复
之前做了一个类似的
就三个函数
init()构造空的头链表
insert 插入一个项, 自动合并
printList() 输出这个多项式

输入输出和你的要求不同,觉得有用就自己改吧

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define EPSLON 1e-6
typedef struct Polynomial {
float coef; // 系数
int power; // 幂
struct Polynomial * next;
} Poly;


Poly * init() {
// 构造多项式,保留空头
Poly * head = (Poly*) malloc ( sizeof(Poly));
head->next = NULL;
return head;
}

//
// 向链表当中插入一项
// 插入算法保证 power小的在前面,大的在后面, 相同的就合并(合并后系数是0就摘除)
void insert(Poly* head, float coef, int power ) {
if (NULL==head) return;
Poly * p = head;
while ( p->next ) {
if ( power < p->next->power ) { // 插入
Poly * pT = (Poly*) malloc( sizeof(Poly));
pT->power = power;
pT->coef=coef;
pT->next = p->next;
p->next = pT;
return;
} else if ( power == p->next->power ) {
// 合并
if ( fabs( coef + p->next->coef ) < EPSLON ) {
// 合并后系数是0. float类型不能直接 == 来判断
Poly* pT = p->next;
p->next = p->next->next;
free(pT);
return;
} else {
p->next->coef += coef;
return;
}
} else
p = p->next;
}
if ( p->next == NULL ) { // 到尾巴了,直接插入
p->next = (Poly*) malloc(sizeof(Poly));
p->next->power = power;
p->next->coef = coef;
p->next->next = NULL;
}
}

// 输出多项式
void printPoly(Poly * head) {
if (NULL == head ) return;
Poly * p = head;
while (p->next) {
if ( head->next == p->next)
printf("%g*x^%d", p->next->coef, p->next->power);
else
printf("+%g*x^%d", p->next->coef, p->next->power);
p = p->next;
}
}


int main() {
Poly * head;
head = init();

// 直接输入第一个多项式 遇到coef和power都是0就结束
while (1) {
float coef; int power;
scanf("%f %d", &coef, &power);
if ( power !=0 && fabs(coef)>EPSLON)
insert(head, coef, power);
else
break;
}

// 输入第二个多项式
while (1) {
float coef; int power;
scanf("%f %d", &coef, &power);
if ( power !=0 && fabs(coef)>EPSLON)
insert(head, coef, power);
else
break;
}
// 其实还可以输入任意多个多项式的项, 是否有重复的指数部分都无所谓, 反正都是自动合并

printPoly(head);
return 0;
}
自信男孩 2020-03-12
  • 打赏
  • 举报
回复
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct list{
//int coe,exp,dat;//定义系数、指数、dat(这个后面要用到,随便起了个名字)
int coe;//, exp,dat;//定义系数、指数、dat(这个后面要用到,随便起了个名字)
int exp;
struct list *next;
}data;

void Greatlist(data *head);//尾插发创造链表
void sor_t(data *head0,data *head1,data *head2);
void destroy_link(data *phead);
void print(data *head2);//输出head2

int main(void)
{
data *head0,*head1,*head2;

head0 = (data *)malloc(sizeof(data));
head1 = (data *)malloc(sizeof(data));
head2 = (data *)malloc(sizeof(data));
head0->next = NULL,head1->next = NULL,head2->next = NULL;

while (1) {
Greatlist(head0);
Greatlist(head1);
sor_t(head0,head1,head2);
print(head2);
destroy_link(head2);
}
return 0;
}

void destroy_link(data *phead)
{
data *p = phead->next, *q;

while (p) {
q = p->next;
free(p);
p = q;
}
}
void Greatlist(data *head)
{
int n;
scanf("%d",&n);
data *p,*pre = head;

while(n--)
{
p = (data *)malloc(sizeof(data));
scanf("%d%d", &p->coe,&p->exp);
pre->next = p;
pre = p;
}
pre->next = NULL;
}

void sor_t(data *head0,data *head1,data *head2)
{

#if 1
data *p0, *p1, *p2, *ptmp;
p0 = head0->next;
p1 = head1->next;
p2 = head2;
while (p0 && p1) {
if (p0->exp > p1->exp) {
ptmp = p0;
p0 = p0->next;
p2->next = ptmp;
p2 = ptmp;
} else if (p0->exp < p1->exp) {
ptmp = p1;
p1 = p1->next;
p2->next = ptmp;
p2 = ptmp;
} else {
p0->coe = p0->coe + p1->coe;
if (p0->coe == 0) {
ptmp = p0;
p0 = p0->next;
free(ptmp);
ptmp = p1;
p1 = p1->next;
free(ptmp);
continue;
}
ptmp = p0;
p0 = p0->next;
p2->next = ptmp;
p2 = ptmp;
//free current p1
ptmp = p1;
p1 = p1->next;
free(ptmp);
}
}

while (p0) {
p2->next = p0;
p2 = p0;
p0 = p0->next;
}
while (p1) {
p2->next = p1;
p2 = p1;
p1 = p1->next;
}
p2->next = NULL;
#else
data *p2,*pre0 = head0,*pre1 = head1,*pre2 = head2;
pre0 = head0->next,pre1 = head1->next;
if(pre0->exp > pre1->exp)
{
p2 = (data *)malloc(sizeof(data));
p2->dat = pre0->coe;
pre2->next = p2;
pre2 = p2;
pre2->next = NULL;
p2 = (data *)malloc(sizeof(data));
p2->dat = pre0->exp;
pre2->next = p2;
pre2 = p2;
pre2->next = NULL;
pre0 = pre0->next;
}
else if(pre0->exp < pre1->exp)//比如input中的第一行第一组 8 2,第二行中的4 4,因为4 > 2,所以head2就将4 4这组数弄了进去
{
p2 = (data *)malloc(sizeof(data));
p2->dat = pre1->coe;
pre2->next = p2;
pre2 = p2;
pre2->next = NULL;
p2 = (data *)malloc(sizeof(data));
p2->dat = pre1->exp;
pre2->next = p2;
pre2 = p2;
pre2->next = NULL;
pre1 = pre1->next;
}
else if(pre0->exp == pre1->exp)//如果第一行数和第二行数的对应组的指数相等,就比如一组数为3 2,另一组为4 2,那么就相加,即 输出7 2这一组数,应该是这个意思吧
{
p2 = (data *)malloc(sizeof(data));
p2->dat = pre0->coe + pre1->coe;
pre2->next = p2;
pre2 = p2;
pre2->next = NULL;
p2 = (data *)malloc(sizeof(data));
p2->dat = pre0->exp;//或者 p2->dat = pre1->exp;因为此时pre0->exp等于pre1->exp
pre2->next = p2;
pre2 = p2;
pre2->next = NULL;
pre0 = pre0->next,pre1 = pre1->next;
}
#endif
}
void print(data *head3)
{
data *p = head3->next;
while(p)
{
printf("%d %d ",p->coe,p->exp);
p = p->next;
}
printf("\n");
}

供参考~

建议对比代码找出自己的问题和熟悉一下逻辑~

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧