一、利用链表实现两个一元多项式的加法,分为三步:
1、构建每个节点NodeOne,java里面没有指针,所以节点里面一般都会再包含一个节点NodeOne,其值会等于下一个节点,所以代码如下:
package com.heshufan.list_mul_acount;
public class Nodeone{
private int a;
private int i;
public Nodeone next;
public Nodeone(){
}
public Nodeone(int a,int i) {
this.a=a;
this.i=i;
this.next=null;
}
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
public int getI() {
return i;
}
public void setI(int i) {
this.i = i;
}
}
2、构造一个链表Polylist,里面包含两个NodeOne节点head与last,head指向链表的第一个元素,注意它本身并不是链表的第一个节点,last则指向的是链表的最后一个元素,里面自己可以定义许多方法,我这里就只写了构造方法、判断链表是否为空的方法,以及两个链表相加的方法
package com.heshufan.list_mul_acount;
class PolyList {
Nodeone head,last;
public PolyList() {
head=new Nodeone();
last=head;
head.next=null;
}
public void insert(Nodeone node) {
last.next=node;
last=node;
}
public boolean isEmpty() {
return head.next==null;
}
public static PolyList add(PolyList list1, PolyList list2) {
PolyList list = new PolyList();
list1.last=list1.head.next;
list2.last=list2.head.next;
while (list1.last!=null&&list2.last!=null) {
if (list1.last.getI() > list2.last.getI()) {
list.insert(list1.last);
list1.last=list1.last.next;
} else if (list1.last.getI() < list2.last.getI()) {
list.insert(list2.last);
list2.last=list2.last.next;
}else {
Nodeone nodeone=new Nodeone(list1.last.getA()+list2.last.getA(), list1.last.getI());
list.insert(nodeone);
list2.last=list2.last.next;
list1.last=list1.last.next;
}
}
while (list1.last!=null) {
list.insert(list1.last);//直接赋值则循环不会结束
list1.last=list1.last.next;
}
while (list2.last!=null) {
list.insert(list2.last);
list2.last=list2.last.next;
}
return list;
}
}
3、最后是验证,自己没有将其打印出来,而是debug验证了代码的正确性
package com.heshufan.list_mul_acount;
//多项式和的链表实现
public class mul_count {
public static void main(String[] args) {
PolyList list1 = new PolyList();
list1.insert(new Nodeone(5, 11));
list1.insert(new Nodeone(4, 3));
list1.insert(new Nodeone(2, 1));
list1.insert(new Nodeone(1, 0));
PolyList list2 = new PolyList();
list2.insert(new Nodeone(5, 11));
list2.insert(new Nodeone(4, 10));
list2.insert(new Nodeone(3, 2));
list2.insert(new Nodeone(2, 1));
PolyList list=new PolyList();
list=PolyList.add(list1, list2);
}
}
二、利用链表实现后进先出的栈,分为三步
1、同样是设置一个节点,和前面的一样
package com.heshfuan.stack;
public class Node {
private Object element;
Node next;
public Node() {
}
public Node(Object element){
this.element=element;
next=null;
}
public Object getElement() {
return element;
}
public void setElement(Object element) {
this.element = element;
}
}
2、实现压栈push与出栈pop
package com.heshfuan.stack;
public class List_Stack {
Node header;//并不是第一个元素,是指向第一个元素的节点
int element_size;
public List_Stack() {
}
public List_Stack(Node header) {
this.header=new Node();
this.header.next=header;
element_size+=1;
}
public void push(Node node){
node.next=header.next;
header.next=node;
element_size++;
}
public Node pop(){
Node node=new Node();
if (header.next!=null) {
node=header.next;
header.next=header.next.next;
element_size--;
}
return node;
}
}
3、代码的检验,同样是debug观察是否正确,没有直接输出
package com.heshfuan.stack;
public class test {
public static void main(String[] args) {
Node node=new Node("he");
List_Stack list_Stack=new List_Stack(node);
List_Stack list_Stack2=new List_Stack(node);
Node node2=list_Stack.pop();
list_Stack.push(node2);
}
}