Java实现链表

/** * @author clydelou * */ class LinkTest { public static void main(String[] args) { Node n1 = new Node("a"); Node n2 = new Node("b"); Node n3 = new Node("c"); Node n4 = new Node("d"); Node n5 = new Node("e"); Link l = new Link(); System.out.print("1 "); l.display(); l.addNode(n2); System.out.print("2 "); l.display(); l.addNode(n3); System.out.print("3 "); l.display(); l.deleteNode(); System.out.print("4 "); l.display(); l.addNode(n2); System.out.print("5 "); l.display(); l.addNode(n5); System.out.print("6 "); l.display(); l.insertNode(0, n1); System.out.print("7 "); l.display(); l.insertNode(2, n4); System.out.print("8 "); l.display(); l.insertNode(l.getLength(), n5); System.out.print("9 "); l.display(); l.deleteNode(0); System.out.print("0 "); l.display(); l.deleteNode(l.getLength()); System.out.print("* "); l.display(); l.deleteNode(); System.out.print("$ "); l.display(); System.out.println("F " + l.getFirst().getName()); System.out.println("L " + l.getLast().getName()); } } class Node { private String name; private Node next; public Node(String name) { this.name = name; } public void setNext(Node next) { this.next = next; } public Node getNext() { return next; } public String getName() { return name; } } class Link { private Node head; private Node tail; public Link() { } public Link(Node head) { this.head = head; tail = head; } public void addNode(Node node) { node = new Node(node.getName()); if (getLength() == 0) { this.head = node; tail = head; tail.setNext(null); } else { tail.setNext(node); tail = tail.getNext(); tail.setNext(null); } } public void deleteNode() { if (getLength() == 0) { System.out.println("null"); } else { head = head.getNext(); } } public void display() { if (head == null && tail == null) { System.out.println("null"); } else { Node temp; temp = head; while (temp != null) { System.out.print(temp.getName() + " "); temp = temp.getNext(); } System.out.println(); } } public int getLength() { if (head == null && tail == null) { return 0; } else { Node temp; temp = head; int length = 0; while (temp != null) { length++; temp = temp.getNext(); } return length; } } public void insertNode(int position, Node node) { node = new Node(node.getName()); if (position < 0 || position > getLength()) { System.out.println("position error"); } else if (position == 0) { node.setNext(head); head = node; } else if (position == getLength()) { tail.setNext(node); tail = node; tail.setNext(null); } else { Node temp; Node pre; temp = head; pre = head; int index = 0; while (index < position) { index++; pre = temp; temp = temp.getNext(); } node.setNext(temp); pre.setNext(node); } } public void deleteNode(int position) { if (getLength() == 0) { System.out.println("null"); } else if (position < 0 || position > getLength()) { System.out.println("position error"); } else if (position == 0) { head = head.getNext(); } else if (position == getLength()) { Node temp; temp = head; while (temp.getNext() != tail) { temp = temp.getNext(); } tail = temp; tail.setNext(null); } else { Node temp; Node pre; temp = head; pre = head; int index = 0; while (index < position) { index++; pre = temp; temp = temp.getNext(); } pre.setNext(temp.getNext()); } } public Node getFirst() { return head; } public Node getLast() { return tail; } }

你可能感兴趣的:(Java实现链表)