java实现一元多项式加法乘法

本文详细介绍了使用单链表进行多项式运算的过程,包括添加、移除节点,链表相加、相乘,以及合并同类项等功能的实现。通过具体代码示例,展示了如何在单链表中进行这些操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

开始

在用单链表实现的过程中,遇到过许多问题,尤其是关于指针这方面,更是出了很多问题,希望大家引以为戒。
不多说了,直接上代码。

代码

节点类

public class Node1 {
	private int coefficient;	//系数
	private int power;			//指数
	Node1 next;
	
	
	public Node1(){
		this(0,0);
	}
	public Node1(int coefficient, int power) {
		this.coefficient = coefficient;
		this.power = power;
		this.next = null;
	}
	public int getCoefficient() {
		return coefficient;
	}
	public void setCoefficient(int coefficient) {
		this.coefficient = coefficient;
	}
	public int getPower() {
		return power;
	}
	public void setPower(int power) {
		this.power = power;
	}
}

链表类

public class LinkedList {
	Node1 head; //头节点
	Node1 current;	//指针
	//构造方法
	public LinkedList(){
		head = new Node1();
		current = head;
		head.next = null;
	}
	//是否为空
	public boolean isEmpty(){
		return head.next == null;
	}
	//添加节点
	public void addNode(int a, int b) {
		Node1 node = new Node1(a, b);
		Node1 temp = head;
		while(temp.next != null) {
			temp = temp.next;
		}
		temp.next = node;
	}
	//移除节点
	public void remove(Node1 node) {
		if (node == null && node.next == null) {
			throw new RuntimeException("节点有误");
		}
		if (node == null) {	//如果移除的是头节点
			head = head.next;
		} 
		//移除中间节点
		Node1 preNode = head;
		Node1 curNode = preNode.next;
		while(curNode != null) {
			boolean a =	(curNode.getCoefficient() == node.getCoefficient());
			boolean b = (curNode.getPower() == node.getPower());
			if (a && b) {
				preNode.next = curNode.next;
			}
			preNode = preNode.next;	//节点向后移
			curNode = curNode.next;
		}
	}
	//链表长度
	public int length() {
		int i = 0;
		Node1 temp = head.next;
		while(temp != null) {
			i++;
			temp = temp.next;
		}
		return i;
	}
	//链表相加
	public LinkedList sum(LinkedList l1, LinkedList l2) {
		LinkedList l = new LinkedList();
		l1.current = l1.head.next;
		Node1 temp1 = l1.current;//获取第一个元素

		l2.current = l2.head.next;
		Node1 temp2 = l2.current;//获取第一个元素
		if (l1.isEmpty()) {			//如果l1为空
			return l2;
		}
		if (l2.isEmpty()) {			//如果l2为空
			return l1;
		}
		while (temp1 != null || temp2 != null) {
			if (temp1 == null) {	//l1已经遍历结束,只剩l2
				l.addNode(temp2.getCoefficient(), temp2.getPower());
				temp2 = temp2.next;
				continue;
			}
			if (temp2 == null) {	//l2已经遍历结束,只剩l1
				l.addNode(temp1.getCoefficient(), temp1.getPower());
				temp1 = temp1.next;
				continue;
			}
			if (temp1.getPower() > temp2.getPower()) {	//l1指数大于l2
				l.addNode(temp1.getCoefficient(), temp1.getPower());
				temp1 = temp1.next;
			}else if (temp1.getPower() == temp2.getPower()) {	//l1指数等于l2
				l.addNode(temp1.getCoefficient() + temp2.getCoefficient(), temp1.getPower());
				temp1 = temp1.next;
				temp2 = temp2.next;
			}else {			//l1指数小于l2
				l.addNode(temp2.getCoefficient(), temp2.getPower());
				temp2 = temp2.next;
			}	
		}
		return l;
	}
	//链表相乘
	public LinkedList multiply(LinkedList l1, LinkedList l2) {
		LinkedList l = new LinkedList();
		l1.current = l1.head.next;	//获取第一个元素
		l2.current = l2.head.next;	//获取第一个元素
		if (l1.isEmpty()) {			//如果l1为空
			return l2;
		}
		if (l2.isEmpty()) {			//如果l2为空
			return l1;
		}
		while (l1.current != null) {	//如果l1不为空
			while (l2.current != null) {	//如果l2不为空
				int a=l1.current.getCoefficient() * l2.current.getCoefficient();
				int i=l1.current.getPower()+l2.current.getPower();
				l.addNode(a, i);
				l2.current = l2.current.next;
			}
			l1.current = l1.current.next;
			l2.current = l2.head.next;
		}
		//合并同类项
		l.current = l.head;
		Node1 cur = l.current.next ;
		while (cur != null) { //下一个不为空
			Node1 n = cur.next;
			while (n != null) {	//下一个不为空
				if (n.getPower() != cur.getPower()) {	//指数不相等
					n = n.next;
				}else {									//指数相等
					cur.setCoefficient(n.getCoefficient() + cur.getCoefficient());	//系数相加
					l.remove(n);	//移除后面那一项
					n = n.next;
				}
			}
			l.current = l.current.next;	//节点后移
			cur = l.current.next;	//节点后移
		}
		return l;
	}
	//打印输出
	public void print() {
		Node1 temp = head.next;
        while(temp !=null){
            System.out.print(temp.getCoefficient() + "x^" + temp.getPower() + " ");
            temp = temp.next;
        }
        System.out.println();
		
	}
}

测试类

public class LinkedListTest1 {

	public static void main(String[] args) {
		LinkedList l1 = new LinkedList();
		l1.addNode(3, 4);
		l1.addNode(-5, 2);
		l1.addNode(6, 1);
		l1.addNode(-2, 0);
		l1.print();
		System.out.println("xxxxxxx");
		LinkedList l2 = new LinkedList();
		l2.addNode(3, 20);
		l2.addNode(-7, 4);
		l2.addNode(3, 1);
		l2.print();
		System.out.println("****");
		LinkedList sum1 = new LinkedList();
		sum1 = sum1.sum(l1, l2);
		sum1.print();
		l1.print();
		l2.print();
		System.out.println("---------");
		LinkedList sum2 = new LinkedList();
		sum2 = sum2.multiply(l1, l2);
		sum2.print();
		l1.print();
		l2.print();
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值