题目: 用Java实现一个完整的双向链表类,并向外提供头、尾节点的增删操作,任意节点的增删操作,获取任意节点,并且要求链表能以字符形式进行输出! 答: /** * */ package util. /** * 这个一个完整的双向链表,通过private私有化基础的链表操作,通过public包装向外暴露操作柄。 * */ public class DoubleLinkedListTest { /** * The Test in main should not test in a time! * @param args */ public static void main(String[] args) { DoubleLinkedList dll = new DoubleLinkedList(). //Initialize dll.add("HH"). dll.add("II"). dll.add("JJ"). System.out.println(dll). //add first dll.addFirst("AA"). System.out.println(dll). //add last dll.addLast("ZZ"). System.out.println(dll). //add in some location dll.add(4, "Daniel"). System.out.println(dll). //remove first node dll.removeFirst(). System.out.println(dll). //remove last node dll.removeLast(). System.out.println(dll). //remove node in some index dll.remove(2). System.out.println(dll). //get the node of some index System.out.println(dll.get(1)). } } class DoubleLinkedList{ //Node class public class Node{ //Attribute of the node Object value. Node prev=this. Node next=this. //construct public Node(Object value){ this.value=value. } //to String public String toString(){ return value.toString(). } } //Attributes of the DoubleLinkedList private Node head=new Node(null). private int size. //base operate of the doubleLinkedList private void addBefore(Node newNode, Node node){ newNode.prev=node.prev. newNode.next=node. newNode.prev.next=newNode. newNode.next.prev=newNode. size . }