1.双向链表的概述与节点结构
2.双向链表的API设计
3.双向链表的时间复杂度分析:
4.源码实现:
package linkList;
import java.util.Iterator;
public class TwoWayLinkList<T> implements Iterable<T>{
//首节点
private Node head;
//尾节点
private Node last;
//链表长度
private int N;
//节点类
private class Node{
//存储数据
public T item;
//指向上一个节点
public Node pre;
//指向下一个节点
public Node next;
//构造方法
public Node(T item,Node pre, Node next){
this.item=item;
this.next=next;
this.pre=pre;
}
}
//构造方法:创建Twowaylinklist对象
public TwoWayLinkList(){
//初始化头节点,刚开始没有前驱也没有后继,只需要一个节点即可
this.head=new Node(null,null,null);
//初始化尾节点,刚开始是没有尾节点的
this.last=null;
//初始化元素个数
this.N=0;
}
//清空链表
public void clear(){
this.head.next=null;
this.last.pre=null;
this.N=0;
}
//判断是否为空
public boolean isEmpty(){
return N==0;
}
//获取链表中元素的个数
public int length(){
return N;