java实现单链表加法,JAVA 用链表实现多项式加法解决方案

Java codeimport java.io.*; //To implement I/O operations

class link {

int coef, exp; // data part for the link list

link next; // next link in list

link(int a, int b) // constructor

{

coef = a; // initialize data

exp = b; // initialize data

}

void display() // To display the list

{

System.out.print(" " + coef + "xe" + exp);

}

} // end of class link

class linklist {

link p, q, d;

link first; // ref to first link on list

linklist() // constructor

{

first = null; // no links on list yet

}

void create(int a, int b) // To insert into the list

{

link node = new link(a, b); // make new link

node.next = first;

first = node;

} // End of function create

void padd(linklist A, linklist B) // To add the polynomials

{

int x; // Temporary variable for storing coef

p = A.first;

q = B.first;

d = first;

while ((p != null) && (q != null)) {

if (p.exp == q.exp) {

x = p.coef + q.coef;

if (x != 0) {

link node = new link(x, p.exp);

// make new link

node.next = d;

d = node;

}

p = p.next; // move to next node of 'A'

q = q.next; // move to next node of 'B'

} else if (p.exp > q.exp) {

link node = new link(p.coef, p.exp);

node.next = d;

d = node;

p = p.next;

} else {

link node = new link(q.coef, q.exp);

node.next = d;

d = node;

q = q.next;

}

}

while (p != null) {

link node = new link(p.coef, p.exp);

node.next = d;

d = node;

p = p.next;

}

while (q != null) {

link node = new link(q.coef, q.exp);

node.next = d;

d = node;

q = q.next;

}

first = d;

} // end of function padd

void disp() // To display the resultant polynomial

{

link current = first; // start at the beginning of the list

while (current != null) // until end of the list

{

current.display();

if (current.next != null) {

System.out.print("+");

} else {

System.out.print(" ");

// print data

}

current = current.next;

// move to next link

}

System.out.println(" ");

} // end of function disp

} // end of class linklist

public class Polyadd // The main class add

{

public static void main(String args[])

// main function

{

try // to catch any exceptions

{

int r = 0, n, x, y;

System.out.println("/* POLYNOMIAL ADDITION */");

linklist A = new linklist(); // make new linklist 'A'

linklist B = new linklist(); // make new linklist 'B'

linklist C = new linklist(); // make new linklist 'C'

BufferedReader f = new BufferedReader(new InputStreamReader(

System.in));

for (int j = 1; j <= 2; j++) {

// To insert the polynomials

System.out.println("Enter the " + j + " polynomial:");

System.out.println("Enter the no. of terms:");

n = Integer.parseInt(f.readLine());

for (int i = n; i > 0; i--) {

System.out.println("Enter the coeff & exponent of " + i

+ " term");

x = Integer.parseInt(f.readLine());

y = Integer.parseInt(f.readLine());

if (j == 1)

A.create(x, y);

// Assign values to links

else

B.create(x, y);

// Assign values to links

}

}

System.out.println("FIRST POLYNOMIAL IS:");

A.disp(); // Display the first plynomial

System.out.println("SECOND POLYNOMIAL IS:");

B.disp(); // Display the second plynomial

C.padd(A, B); // Calling the function padd System.out.println

// ("THE SUM OF POLYNOMIALS IS:");

C.disp(); // Display the resultant plynomial

} catch (IOException e) // To catch I/O Exception

{

}

} // End of main function

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值