Java的单向链表和双向链表的写法
- 单向链表:
- 简单单向链表:
- 代码添加节点方法和遍历链表方法的代码:
- 使用:
- 双向链表:
- 简单的双向链表:
- 代码添加节点方法和遍历链表方法的代码:
- 使用:
单向链表:
拥有后指针next
简单单向链表:
import java.util.Iterator;public class UnidirectionalLinkedList<E> {//首节点private Node<E> first;//尾节点private Node<E> last;//链表长度private int size;//静态内部类Node,节点类public static class Node<E>{E item;Node<E> next;public Node(E item, Node<E> next) {this.item = item;this.next = next;}}}
代码添加节点方法和遍历链表方法的代码:
import java.util.Iterator;public class UnidirectionalLinkedList<E> {//首节点private Node<E> first;//尾节点private Node<E> last;//链表长度private int size;//添加节点方法public void add(E e){Node<E> node = new Node<>(e, null);if(first == null){first = node;}else{last.next = node;}last = node;size++;}//迭代器public Iterator<E> iterator(){return new Itr();}//实现链表的遍历public class Itr implements Iterator<E>{private int cursor;private Node<E> node = first;@Overridepublic boolean hasNext() {return cursor != size;}@Overridepublic E next() {//node节点的内容E item = node.item;//node的下一个节点node = node.next;cursor++;return item;}}//静态内部类Node,节点类public static class Node<E>{E item;Node<E> next;public Node(E item, Node<E> next) {this.item = item;this.next = next;}}}
使用:
public static void main(String[] args) {UnidirectionalLinkedList<String> list = new UnidirectionalLinkedList<>();list.add("aaa");list.add("bbb");list.add("ccc");list.add("ddd");list.add("eee");Iterator<String> it = list.iterator();while(it.hasNext()){String element = it.next();System.out.println(element);}}
双向链表:
拥有前指针 prev 和后指针next
简单的双向链表:
import java.util.Iterator;public class BidirectionalLinkedList<E> {//首节点private Node<E> first;//尾节点private Node<E> last;//链表长度private int size;//静态内部类Node,节点类public static class Node<E>{//前指针Node<E> prev;//节点内容E item;//后指针Node<E> next;//有参构造public Node(Node<E> prev,E item, Node<E> next) {this.prev = prev;this.item = item;this.next = next;}}}
代码添加节点方法和遍历链表方法的代码:
import java.util.Iterator;public class BidirectionalLinkedList<E> {//首节点private Node<E> first;//尾节点private Node<E> last;//链表长度private int size;public void add(E e){Node<E> l = last;Node<E> node = new Node<>(l,e, null);if(first == null){first = node;}else{last.next = node;}last = node;size++;}public Node<E> getLast() {return last;}public Iterator<E> iterator(){return new Itr();}public class Itr implements Iterator<E>{private int cursor;private Node<E> node = first;@Overridepublic boolean hasNext() {return cursor != size;}@Overridepublic E next() {E item = node.item;node = node.next;cursor++;return item;}}//静态内部类Node,节点类public static class Node<E>{//前指针Node<E> prev;//节点内容E item;//后指针Node<E> next;//有参构造public Node(Node<E> prev,E item, Node<E> next) {this.prev = prev;this.item = item;this.next = next;}}}
使用:
public static void main(String[] args) {BidirectionalLinkedList<String> list = new BidirectionalLinkedList<>();list.add("aaa");list.add("bbb");list.add("ccc");list.add("ddd");list.add("eee");//正序遍历Iterator<String> it = list.iterator();while(it.hasNext()){String element = it.next();System.out.println(element);}System.out.println("-----------------------");//倒序遍历Node<String> node = list.getLast();while(node != null){System.out.println(node.item);node = node.prev;}}