Java实现双向链表的基本操作
2016-07-26 10:49
633 查看
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">双向链表即表中任一结点都是由data数据域,上一个结点引用域lastNode,下一个结点引用域nextNode组成。</span>
/** *双向链表结点类 * @author liangxiamoyi * @param <T> */ public class DLNode<T> { /** * 数据域 */ protected T data; /** * 下一个结点 */ protected DLNode<T> nextNode; /** * 上一个结点 */ protected DLNode<T> lastNode; /** * 构造方法 * @param last 上一个结点 * @param next 下一个结点 */ public DLNode(DLNode<T> last,DLNode<T> next ){ this.lastNode=last; this.nextNode=next; } /** * 构造方法 * @param item 数据域 * @param last 上一个结点 * @param next 下一个结点 */ public DLNode(T item,DLNode<T> last,DLNode<T> next){ this.data=item; this.nextNode=next; this.lastNode=last; } }
/** * 双向链表类 * @author liangxiamoyi * * @param <T> */ public class DLList<T> { /** * 表头 */ private DLNode<T> head; /** * 表尾 */ private DLNode<T> tail; /** * 当前结点 */ private DLNode<T> currNode; /** * 链表大小 */ private int size; /** * 构造只有一个哨位结点的空表 */ public DLList(){ this.head=new DLNode<T>(null,null); this.size=0; } /** * 构建有一个结点的单链表 * @param item 数据域 */ public DLList(T item){ this.head=new DLNode<T>(null, null); tail=currNode=new DLNode<T>(item,head,null); head.nextNode=tail; size=1; } /** * 判断是否为空 * @return */ public boolean isEmpty(){ return head.nextNode==null; } /** * 得到单链表的长度 * @return */ public int getSize(){ return this.size; } /** * 存取,得到第k个结点的数据域 * @param k 第k个结点 * @return 返回第k个结点的数据 */ public T find(int k){ if(k<0||k>size||size==0){ throw new RuntimeException("empty list or unreasonable position!"); } currNode=head.nextNode; for(int i=2;i<=k;i++){ currNode=currNode.nextNode; } return currNode.data; } /** * 查找,得到第一个数据为item的结点的位置 * @param item 数据 * @return 位置 */ public int search(T item){ currNode=head.nextNode; for(int i=1;i<=size;i++){ if(currNode.data==item)return i; else{ currNode=currNode.nextNode; } } return -1; } /** * 删除当前结点,并将其数据返回 * @return */ public T delete(){ if(currNode==head||isEmpty()){ throw new RuntimeException("head node or empty list"); } DLNode<T> temp=currNode; currNode.nextNode.lastNode=currNode.lastNode; currNode.lastNode.nextNode=currNode.nextNode; size--; if(currNode==tail){ tail=currNode.lastNode; } currNode=currNode.lastNode; return temp.data; } /** * 删除哨位结点后的第一个真正表结点,并将数据返回 * @return */ public T deleteFromHead(){ if(isEmpty()){ throw new RuntimeException("empty list"); } DLNode<T> temp=head.nextNode; head.nextNode=head.nextNode.nextNode; size--; if(temp==tail){ tail=head; } else{ head.nextNode.lastNode=head; } return temp.data; } /** * 删除表尾结点,并将数据返回 * @return */ public T deleteFromTail(){ if(isEmpty()){ throw new RuntimeException("empty list"); } DLNode<T> p=tail; tail=tail.lastNode; tail.nextNode=null; size--; return p.data; } /** * 删除当前结点的下一个结点 * @return */ public T deleteNext(){ if(isEmpty()||currNode.nextNode==null){ throw new RuntimeException("empty list or the next node is null"); } DLNode<T> p=currNode.nextNode; if(p==tail){ tail=currNode; currNode.nextNode=null; } else{ currNode.nextNode=p.nextNode; p.nextNode.lastNode=currNode; } size--; return p.data; } /** * 在当前结点后插入一个数据为item的结点 * @param item 数据 */ public void insert(T item){ if(isEmpty()){ tail=head.nextNode=new DLNode<T>(item,head,null); size++; return; } DLNode<T> p=new DLNode<T>(item,currNode,currNode.nextNode); currNode.nextNode.lastNode=p; currNode.nextNode=p; size++; if(tail==currNode){ tail=p; } } /** * 从表尾插入一个数据为item的结点 * @param item */ public void insertFromTail(T item){ tail.nextNode=new DLNode<T>(item,tail,null); tail=tail.nextNode; size++; } /** * 在哨位结点后插入一个数据为item的结点 * @param item */ public void insertFromHead(T item){ if(isEmpty()){ head.nextNode=new DLNode<T>(item,head,null); tail=head.nextNode; size++; return; } else{ DLNode<T> p=new DLNode<T>(item, head,head.nextNode); head.nextNode.lastNode=p; head.nextNode=p; size++; } } /** * 打印所有结点的数据 */ public void showNodes(){ currNode=head.nextNode; for(int i=1;i<=size;i++){ System.out.println(currNode.data); currNode=currNode.nextNode; } } //测试 public static void main(String[] args){ DLList<Integer> list=new DLList<Integer>(5); list.insertFromHead(4); list.insertFromTail(3); list.insertFromHead(2); list.showNodes(); System.out.println(list.search(4)); System.out.println(list.find(2)); list.deleteFromHead(); list.deleteFromTail(); list.showNodes(); } }
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序