开始
在用单链表实现的过程中,遇到过许多问题,尤其是关于指针这方面,更是出了很多问题,希望大家引以为戒。
不多说了,直接上代码。
代码
节点类
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();
}
}