class DoubleLNode {
private int data;
private DoubleLNode preNode;
private DoubleLNode nextNode;
public int getData() {
return data;
}
public DoubleLNode getPreNode() {
return preNode;
}
public DoubleLNode getNextNode() {
return nextNode;
}
public void setData(int data) {
this.data = data;
}
public void setPreNode(DoubleLNode preNode) {
this.preNode = preNode;
}
public void setNextNode(DoubleLNode nextNode) {
this.nextNode = nextNode;
}
}
public class DoubleLinkList {
public DoubleLNode head;
public int size;
public void initList(int[] datas) {
head = new DoubleLNode();
head.setNextNode(head);
head.setPreNode(head);
DoubleLNode tail;
tail = head;
for(int i = 0;i<datas.length;i++){
DoubleLNode newDNode = new DoubleLNode();
newDNode.setData(datas[i]);
newDNode.setPreNode(tail);
tail.setNextNode(newDNode);
newDNode.setNextNode(head);
head.setPreNode(newDNode);
tail=newDNode;
}
}
public void printListNormally() {
DoubleLNode cursor ;
cursor = head;
while(cursor.getNextNode()!=head){
cursor = cursor.getNextNode();
System.out.println(cursor.getData());
}
}
public void printNotNormal() {
DoubleLNode cursor ;
cursor = head;
while(cursor.getPreNode()!=head){
cursor = cursor.getPreNode();
System.out.println(cursor.getData());
}
}
public void insertIntoList(int value) {
DoubleLNode newLNode = new DoubleLNode();
newLNode.setData(value);
DoubleLNode lastLNode;
lastLNode = head.getPreNode();
lastLNode.setNextNode(newLNode);
newLNode.setPreNode(lastLNode);
newLNode.setNextNode(head);
head.setPreNode(newLNode);
}
public static void main(String[] args) {
int [] a = {1,2,3,4,5,6};
DoubleLinkList doubleLinkList = new DoubleLinkList();
doubleLinkList.initList(a);
System.out.println("顺序打印");
doubleLinkList.printListNormally();
System.out.println("倒序打印");
doubleLinkList.printNotNormal();
doubleLinkList.insertIntoList(10);
System.out.println("插入后顺序打印");
doubleLinkList.printListNormally();
System.out.println("插入后倒序打印");
doubleLinkList.printNotNormal();
}
}