4-3-Add Two Polynomials
来源:互联网 发布:大学java期末考试试题 编辑:程序博客网 时间:2024/06/06 12:44
4-3 Add Two Polynomials
Write a function to add two polynomials. Do not destroy the input. Use a linked list implementation with a dummy head node. Note: The zero polynomial is represented by an empty list with only the dummy head node.
Format of functions:
Polynomial Add( Polynomial a, Polynomial b );
where Polynomial
is defined as the following:
typedef struct Node *PtrToNode;struct Node { int Coefficient; int Exponent; PtrToNode Next;};typedef PtrToNode Polynomial;/* Nodes are sorted in decreasing order of exponents.*/
The function Add
is supposed to return a polynomial which is the sum of a
and b
.
Sample program of judge:
#include <stdio.h>#include <stdlib.h>typedef struct Node *PtrToNode;struct Node { int Coefficient; int Exponent; PtrToNode Next;};typedef PtrToNode Polynomial;Polynomial Read(); /* details omitted */void Print( Polynomial p ); /* details omitted */Polynomial Add( Polynomial a, Polynomial b );int main(){ Polynomial a, b, s; a = Read(); b = Read(); s = Add(a, b); Print(s); return 0;}/* Your function will be put here */
Sample Input:
43 4 -5 2 6 1 -2 035 20 -7 4 3 1
Sample Output:
5 20 -4 4 -5 2 9 1 -2 0
Polynomial Add( Polynomial a, Polynomial b ){Polynomial c, p;c = (Polynomial)malloc(sizeof(struct Node));c->Next = NULL;a = a->Next;b = b->Next;p = c;while(a!=NULL&&b!=NULL){if(a->Exponent < b->Exponent){p->Next = b;b = b->Next;p = p->Next;p->Next = NULL;}else if(a->Exponent > b->Exponent){p->Next = a;a = a->Next;p = p->Next;p->Next = NULL;}else{Polynomial t;t = (Polynomial)malloc(sizeof(struct Node));t->Coefficient = a->Coefficient + b->Coefficient;t->Exponent = a->Exponent;t->Next = NULL;if(t->Coefficient != 0){p->Next = t;p = p->Next;}a = a->Next;b = b->Next;}}while(a!=NULL){p->Next = a;p = p->Next;a = a->Next;}while(b!=NULL){p->Next = b;p = p->Next;b = b->Next;}p->Next = NULL;return c;}
1 0
- 4-3-Add Two Polynomials
- PTA 4-3 Add Two Polynomials
- Add Two Polynomials
- Add Two Polynomials
- c 编写Add Two Polynomials
- 6-14 Add Two Polynomials(20 point(s))
- leetcode 4ADD TWO NUMBERS
- Leetcode【4】:Add Two Numbers
- [leetcode 4] Add Two Numbers
- leetcode 第4题 Add Two Numbers
- [leetcode NO.3] Add Two Numbers (JAVA)
- Add two numbers & Add binary
- Two Sum & Add Two Numbers
- 题Two--Add Two Numbers
- LeetCode: Add Two Numbers
- Add Two Numbers
- LeetCode: Add Two Numbers
- [LeetCode]Add Two Numbers
- 简单的放大镜效果
- BZOJ2631: tree Link-cut-tree
- 【转】数组、链表、Hash
- 关于socket flush()目标端接收不到数据,close()可以接收到数据的问题
- char
- 4-3-Add Two Polynomials
- OLED显示-字库码表原理
- spark 介绍
- 用递归方法求数组的最大值
- android shell命令screenrecord和uptime
- 右移
- Red Hat 7.2重装并配置yum源
- rdheed
- 手把手教你玩转SOCKET模型之重叠I/O篇(上)