有头单向链表实现多项式合并
来源:互联网 发布:js事件追踪代码布局 编辑:程序博客网 时间:2024/06/07 19:00
#include <stdio.h>
#include <stdlib.h>
struct node_st
{
int coef;int exp;structnode_st *next;
};
struct node_st *poly_create(int a[][2], int n)
{
int i;struct node_st *node;struct node_st *newnode, *cur;node = malloc(sizeof(*node));if(node == NULL)return NULL;node->next = NULL;cur = node;
for(i = 0; i < n; i++){
newnode = malloc(sizeof(*newnode));if(newnode == NULL)return NULL;newnode->coef = a[i][0];newnode->exp = a[i][1];newnode->next = NULL;
cur->next = newnode;cur = newnode;
}return node;
}
void poly_show(struct node_st *node)
{
struct node_st *cur;for(cur = node->next; cur != NULL; cur = cur->next){printf("(%d %d) ", cur->coef, cur->exp);}printf("\n");
}
void poly_union(struct node_st *p1, struct node_st *p2)
{
struct node_st *p, *q, *r;p = p1->next;q = p2->next;r = p1;
while(p && q){if(p->exp < q->exp){r->next = p;r = p;p = p->next;}else if(p->exp > q->exp){r->next = q;r = q;q = q->next;}else{p->coef += q->coef;if(p->coef){r->next = p;r = p;}p = p->next;q = q->next;}}if(p == NULL)r->next = q;elser->next = p;
}
int main(int argc, char *argv[]) {
int a[][2]={{5,0},{2,1},{8,8},{3,16}};
int b[][2] ={{6,1},{16,6},{-8,8}};
struct node_st *p1, *p2;
p1 = poly_create(a,4);
if(p1 == NULL)
return -1;
p2 = poly_create(b,3);
if(p2 == NULL)
return -2;
poly_show(p1);
poly_show(p2);
poly_union(p1, p2);
poly_show(p1);
return 0;
}
阅读全文
0 0
- 有头单向链表实现多项式合并
- 单向有头节点链表
- 有头节点单向循环链表
- 有头结点的单向链表
- 单向链表 练习3.6 多项式相加
- 单向链表 练习3.7 多项式相乘
- 《单向链表》----------------------合并,反转
- 合并两个单向链表
- 单向链表实现多项式加和乘--自己写数据结构
- 双链表&链表合并&多项式相加算法
- 有环单向链表
- 用单向链表计算多项式的加法
- 利用单向链表计算多项式的乘法
- 单向无头节点链表操作
- 合并两个有序单向链表
- 合并两个排好序的单向链表
- 单向循环链表的合并图解
- 两个有序单向链表的合并
- 使用Springcloud及Docker实现微服务架构
- iPhone/iPad/Android UI尺寸规范
- 重启服务器后,rabbit用户丢失问题
- 清除浮动的几种方式?
- App安装失败的几种情况
- 有头单向链表实现多项式合并
- IOS开发-网络监听以及网络状态更改
- 复杂链表的复制
- feature scaling 特征缩放
- 制作UEFI启动模式的U盘
- Quartz教程详情
- 用 C 语言实现神经网络需要几步?
- HDU4183最大流
- 虚拟现实:打造视觉革命 VR营造立体式交互空间