- 双链表的代码
public class DoubleLinkedList {
DoubleLinkedList pre;
DoubleLinkedList next;
int n;
public static void print(DoubleLinkedList node){
while (node!=null){
System.out.print(node.n+" ");
node = node.next;
}
System.out.println();
}
public static void reversePrint(DoubleLinkedList node){
DoubleLinkedList end = null;
while (node.next!=null){
node = node.next;
}
while (node!=null){
System.out.print(node.n+" ");
node = node.pre;
}
System.out.println();
}
public static DoubleLinkedList init(int[] ints){
if(ints==null||ints.length==0)
return null;
DoubleLinkedList head = new DoubleLinkedList();
DoubleLinkedList p = head;
head.n = ints[0];
for (int i = 1 ;i<ints.length;i++) {
DoubleLinkedList temp = new DoubleLinkedList();
temp.n = ints[i];
temp.pre = p;
p.next = temp;
p = temp;
}
return head;
}
public void add(int t){
DoubleLinkedList temp = new DoubleLinkedList();
DoubleLinkedList p = this;
while (p.next!=null){
p = p.next;
}
temp.n = t;
temp.pre = p;
p.next = temp;
}
public static void main(String[] args) {
DoubleLinkedList init = init(new int[]{1, 2, 3, 4,5});
print(init);
init.add(6);
reversePrint(init);
}
}