原文链接:数据结构之单链表实现多项式加和减法_sunny_daily的博客-CSDN博客_数据结构集合减法
package dataStructure;
public class Node {
int data;//数据本身
int exp;//指数
Node next;//下一个节点
public Node(int data) {
this.data = data;
}
public Node(int data,Node next) {
this.data = data;
this.next = next;
}
public Node(int data,int exp) {
this.data = data;
this.exp = exp;;
}
public Node() {
// TODO Auto-generated constructor stub
}
}
package dataStructure;
/*
* 单链表的元素节点包括两个部分,一个是元素对象本身,一个是指向下一个节点的元素
*/
public class SingleList {
int size = 0;
Node first = new Node();// 第一个节点,是虚拟出来的头节点
Node last;// 最后一个节点
// 在第i个元素位置添加一个新的节点
public void insertNode(Node node, int i) {
Node preNode = first;
Node tempNode = preNode.next;
for (int k = 0; k < i+1; k++) {
if (k == i) {
node.next = null;
preNode.next = node;
if(tempNode !=null) {
node.next = tempNode;
}else {
last = node;
}
size++;
} else {
preNode = preNode.next;
tempNode = preNode.next;
}
}
}
// 删除第i个元素节点
public void deleteNode(int i) {
Node preNode = first;
Node tempNode = preNode.next;
for (int k = 0; k < i+1; k++) {
if (k == i) {
if(tempNode == last) { //如果是删除最后一个节点
preNode.next = null;
size--;
last = preNode;
}else {
Node nextNode = tempNode.next;
preNode.next = nextNode;
size--;
}
} else {
preNode = tempNode;
}
}
}
// 删除node节点
public void deleteNode(Node node) {
Node preNode = first;
int data = node.data;
int exp = node.exp;
while(preNode!=null && preNode.next!=null) {
Node tempNode = preNode.next;
int tempData = tempNode.data;
int tempExp = tempNode.exp;
if(tempData == data && tempExp == exp) {
if(tempNode == last) {
last = preNode;
preNode.next = null;
}else {
Node nextNode = tempNode.next;
preNode.next = nextNode;
}
size--;
}else {
preNode = preNode.next;
}
}
}
// 构造单链表,尾插法
public void addNode(Node node) {
if (size == 0) {
first.next = node;
last = node;
} else {
Node temp = last;
last = node;
temp.next = node;
}
// 添加元素成功,则size加1
size++;
}
// 构造单链表,采用头插法
public void addHead(Node node) {
if (size == 0) {
first.next = node;
last = node;
} else {
Node temp = first.next;
node.next = temp;
first.next = node;
}
// 添加元素成功,则size加1
size++;
}
}
package dataStructure;
/*
* 单链表实现多项式加法
*/
public class Demo3 {
public static void main(String args[]) {
SingleList list1 = new SingleList();
Node node1 = new Node(7,0);
Node node2 = new Node(3,1);
Node node3 = new Node(9,8);
Node node4 = new Node(5,17);
list1.addNode(node1);
list1.addNode(node2);
list1.addNode(node3);
list1.addNode(node4);
SingleList list2 = new SingleList();
Node node21 = new Node(8,1);
Node node22 = new Node(22,7);
Node node23 = new Node(-9,8);
Node node24 = new Node(20,80);
Node node25 = new Node(21,90);
list2.addNode(node21);
list2.addNode(node22);
list2.addNode(node23);
list2.addNode(node24);
list2.addNode(node25);
SingleList returnList = doPlu(list1,list2);
System.out.println("返回的单链表元素有:"+returnList.size+"个元素");
Node first = returnList.first;//第一个节点是初始化的节点
int i=1;
while(first!=null && first.next!=null) {
Node node = first.next;
System.out.println("第"+i+"个元素的系数是:"+node.data+";指数是:"+node.exp);
i++;
first = node;
}
System.out.println("success!");
}
public static SingleList doPlu(SingleList list1,SingleList list2) {
int list1_size = list1.size;
int list2_size = list2.size;
int i=0;int j=0;
Node list1_preNode = list1.first;
Node list2_preNode = list2.first;
Node list1_currNode = list1_preNode.next;
Node list2_currNode = list2_preNode.next;
while(i<list1_size && j<list2_size) {
int list1_data = list1_currNode.exp;
int list2_data = list2_currNode.exp;
if(list1_data < list2_data) {
i++;
list1_currNode = list1_currNode.next;
}else if(list1_data == list2_data){
list1_currNode.data = list1_currNode.data + list2_currNode.data;
if(list1_currNode.data == 0) {
list1.deleteNode(list1_currNode);//如果两个元素相加值为0,则删除该元素
}
list2_currNode = list2_currNode.next;
j++;
}else{
Node tempNode = list2_currNode.next;
list1.insertNode(list2_currNode, i);
list2_currNode = tempNode;
j++;
}
}
/*
* 遍历结束后list2存在两种可能,一种是遍历完,一种是没遍历完
* 如果没遍历完,需要做特殊处理
*/
if(j<list2_size) {
list1.addNode(list2_currNode);
list1.size = list1.size+list2_size-j-1;
list1.last = list2.last;
}
return list1;
}
}