1. 定义节点node
public class Node<T> { private Node<T> pre; private Node<T> next; private T data; public void setPre(Node<T> node){ this.pre = node; } public Node<T> getPre(){ return pre; } public void setNext(Node<T> node){ this.next = node; } public Node<T> getNext(){ return next; } public void setData(T data){ this.data = data; } public T getData(){ return this.data; } public Node(Node<T> pre, Node<T> next, T data){ this.pre = pre; this.next = next; this.data = data; } public Node(){ super(); } }
2. 创建链表方式1,与c++类似
public class ListDirectNode<T extends Comparable<T>> { private byte[] lock = new byte[1]; /** * 链表大小 */ private int size; /** * 链表头节点 */ private Node<T> header; /** * 链表尾部节点 */ private Node<T> tail; public void setHeader(Node<T> node){ this.header = node; } public Node<T> getHeader(){ return this.header; } public void setTail(Node<T> node){ this.tail = node; } public Node<T> getTail(){ return this.tail; } public int size(){ return this.size; } public ListDirectNode(){ size = 0; header = new Node<T>(null, null, null); tail = new Node<T>(header, null, null); header.setNext(tail); } /** * 往链表尾部追加数据 * @param data */ public void add(T data){ synchronized (lock) { Node<T> node = new Node<T>(null, null, data); tail.getPre().setNext(node); node.setPre(tail); node.setNext(tail); tail.setPre(node); size++; } } /** * @param sortType 排序方式 * true:升序 * false:降序 */ public void sort(boolean sortType){ for(Node<T> node1 = header.getNext(); node1.getNext() != tail; node1 = node1.getNext()){ for(Node<T> node2 = node1.getNext(); node2 != tail; node2 = node2.getNext()){ T d1 = node1.getData(); T d2 = node2.getData(); if(sortType){ if(compare(d1, d2) > 0){ node1.setData(d2); node2.setData(d1); } }else{ if(compare(d1, d2) < 0){ node1.setData(d2); node2.setData(d1); } } } } } private int compare(T d1, T d2){ return d1.compareTo(d2); } public boolean isEmpty(){ return size==0; } public T get(int index){ if(index > size || index < 0){ throw new IndexOutOfBoundsException(); } Node<T> currentNode = new Node<T>(header, null, null); for(int i=1; i<=index; i++){ currentNode = currentNode.getNext(); } return currentNode.getData(); } public void print(){ int index = 0; Node<T> currentNode = header.getNext(); while(currentNode.getNext() != null){ index++; System.out.println("当前位置:"+index+",当前值"+currentNode.getData().toString()); currentNode = currentNode.getNext(); } } }
3. 创建链表方式2,迭代器
public class MyList<T extends Comparable<T>> implements Iterable<T>{ /** * 链表大小 */ private int size; /** * 链表头节点 */ private Node<T> header; /** * 链表下一节点 */ private Node<T> tail; public void setHeader(Node<T> node){ this.header = node; } public Node<T> getHeader(){ return this.header; } public void setTail(Node<T> node){ this.tail = node; } public Node<T> getTail(){ return this.tail; } public int size(){ return this.size; } public MyList(){ size = 0; header = new Node<T>(null, null, null); tail = new Node<T>(header, null, null); header.setNext(tail); } public void add(T data){ Node<T> node = new Node<T>(null, null, data); tail.getPre().setNext(node); node.setPre(tail); node.setNext(tail); tail.setPre(node); size++; } /** * @param sortType 排序方式 * true:升序 * false:降序 */ public void sort(boolean sortType){ for(Node<T> node1 = header.getNext(); node1.getNext() != tail; node1 = node1.getNext()){ for(Node<T> node2 = node1.getNext(); node2 != tail; node2 = node2.getNext()){ T d1 = node1.getData(); T d2 = node2.getData(); if(sortType){ if(compare(d1, d2) > 0){ node1.setData(d2); node2.setData(d1); } }else{ if(compare(d1, d2) < 0){ node1.setData(d2); node2.setData(d1); } } } } } private int compare(T d1, T d2){ return d1.compareTo(d2); } public void print(){ int index = 0; Iterator<T> iterator = this.iterator(); while(iterator.hasNext()){ index++; System.out.println("当前位置:"+index+",当前值"+iterator.next().toString()); } } @Override public Iterator<T> iterator() { return new MyListIterator<T>(); } private class MyListIterator<T> implements Iterator<T>{ @SuppressWarnings("unchecked") Node<T> current = (Node<T>) header.getNext(); @Override public boolean hasNext() { return current != tail; } @Override public T next() { if(!hasNext()){ throw new IndexOutOfBoundsException(); } T data = current.getData(); current = current.getNext(); return data; } @Override public void remove() { if(!hasNext()){ throw new NoSuchElementException(); } current.getPre().setNext(current.getNext()); current.getNext().setPre(current.getPre()); current = current.getNext(); size--; } } }
4. 测试代码
public class TestMain { public static void main(String[] args){ ListDirectNode<String> list = new ListDirectNode<String>(); list.add("a"); list.add("d"); list.add("b"); list.add("e"); list.sort(true); list.print(); MyList<String> list2 = new MyList<String>(); list2.add("a1"); list2.add("d1"); list2.add("b1"); list2.add("e1"); list2.sort(true); list2.print(); ListDirectNode<Integer> list3 = new ListDirectNode<Integer>(); list3.add(1); list3.add(3); list3.add(2); list3.add(10); list3.sort(false); list3.print(); MyList<Integer> list4 = new MyList<Integer>(); list4.add(10); list4.add(3); list4.add(6); list4.add(0); list4.sort(false); list4.print(); } }