熟悉一下Java。。。
package ChianTable; import java.util.Scanner; /** * Created by Administrator on 2018/3/26. */ public class LinkedList { private LinkedList prev; private LinkedList next; private int val; private LinkedList head; private LinkedList rear; public LinkedList() { super(); } public LinkedList(LinkedList p, LinkedList n, int v) { super(); this.prev = p; this.next = n; this.val = v; } public LinkedList Create() { Scanner type = new Scanner(System.in); int val = type.nextInt(); LinkedList nextNode = new LinkedList(null, null, val); this.head = nextNode; while ((val = type.nextInt()) != 0) { LinkedList curr = new LinkedList(nextNode, null, val); nextNode.next = curr; nextNode = curr; } this.rear = nextNode; nextNode.next = null; return this.head; } public void nextprint() { if (this.head != null) { System.out.println(this.head.val); this.head = this.head.next; nextprint(); } } public void prevprint() { if (this.rear != null) { System.out.println(this.rear.val); this.rear = this.rear.prev; prevprint(); } } }
测试:
package main; import ChianTable.LinkedList; public class Main { public static void main(String [] args) { LinkedList linkedlist = new LinkedList(); linkedlist.Create(); linkedlist.nextprint(); linkedlist.prevprint(); } }