node结点
package link;
public class MyNode {
private Object ele;
private MyNode next;
public MyNode() {
this.ele = null;
this.next = null;
}
public MyNode(Object ele, MyNode next) {
this.ele = ele;
this.next = next;
}
public Object getEle() {
return ele;
}
public void setEle(Object ele) {
this.ele = ele;
}
public MyNode getNext() {
return next;
}
public void setNext(MyNode next) {
this.next = next;
}
}
Stack
package link;
public class LinkStack {
public MyNode head;
public int size;
public LinkStack() {
head = new MyNode();
size = 0;
}
public void push(Object obj) {
MyNode p = new MyNode(obj, head.getNext());
head.setNext(p);
size++;
}
public MyNode pop() {
MyNode o = new MyNode();
o = head.getNext();
if(o != null) {
head.setNext(o.getNext());
size--;
}
return o;
}
public void show() {
MyNode p = head;
for(int i = 0; i < size; i++) {
p = p.getNext();
System.out.println(p.getEle());
}
}
public static void main(String[] args) {
LinkStack list = new LinkStack();
list.push(1);
list.push(2);
list.push(3);
list.pop();
list.push(4);
list.show();
}
}
queue
package link;
public class LinkQueue {
public MyNode head;
public MyNode tail;
public int size;
public LinkQueue() {
size = 0;
tail = new MyNode();
head = tail;
}
public void push(Object obj) {
MyNode p = new MyNode(obj, null);
tail.setNext(p);
tail = p;
size++;
}
public MyNode pop() {
MyNode o = head.getNext();
if(o != null) {
head.setNext(o.getNext());
size--;
}
return o;
}
public void show() {
MyNode p = head;
for(int i = 0; i < size; i++) {
p = p.getNext();
System.out.println(p.getEle());
}
}
public static void main(String[] args) {
LinkQueue list = new LinkQueue();
list.push(1);
list.push(2);
list.push(3);
System.out.println(list.pop().getEle());
list.push(4);
list.show();
}
}
linklist
package link;
public class LinkList {
public MyNode head;
public int size;
public LinkList() {
head = new MyNode();
size = 0;
}
public void push(Object obj) {
MyNode p = new MyNode(obj, head.getNext());
head.setNext(p);
size++;
}
public void del(Object obj) {
MyNode cur = new MyNode();
MyNode pre = new MyNode();
pre = head;
cur = head.getNext();
while(cur != null) {
if(cur.getEle() == obj) {
pre.setNext(cur.getNext());
MyNode flg = cur;
cur = cur.getNext();
flg.setNext(null);
size--;
// break;
} else {
pre = cur;
cur = cur.getNext();
}
}
}
public void show() {
MyNode p = head;
for(int i = 0; i < size; i++) {
p = p.getNext();
System.out.println(p.getEle());
}
}
public static void main(String[] args) {
LinkList list = new LinkList();
list.push(1);
list.del(1);
list.push(1);
list.show();
}
}