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;
@Override
public boolean hasNext() {
return cursor != size;
}
@Override
public 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;
@Override
public boolean hasNext() {
return cursor != size;
}
@Override
public 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;
}
}