Java实现双向链表

来源:互联网 发布:淘宝首页导航怎么去掉 编辑:程序博客网 时间:2024/06/05 03:51

双向链表和单向链表的不同之处在于,双向链表的指针域除了指向后一个节点以外,还有个指针指向前一个节点;查找的时候可以从前往后或从后往前找;第一个节点的前向指针域和最后一个节点的后向指针域为null。

下面是具体实现:


首先,先定义一个双向链表对象:

package com.zxd.link;/** * DoubleLinkedNode:双向链表节点类:包含一个数据域,两个指针域(一个指向前一个节点,一个指向后一个节点) *  * @author zeng.xiangdong 1770534116@qq.com * @version V1.0 2014-7-2 下午10:49:23 */public class DoubleLinkedNode {private int data;private DoubleLinkedNode preNode;private DoubleLinkedNode nextNode;public DoubleLinkedNode(int data) {this.data = data;}public int getData() {return data;}public void setData(int data) {this.data = data;}public DoubleLinkedNode getPreNode() {return preNode;}public void setPreNode(DoubleLinkedNode preNode) {this.preNode = preNode;}public DoubleLinkedNode getNextNode() {return nextNode;}public void setNextNode(DoubleLinkedNode nextNode) {this.nextNode = nextNode;}}

下面是双向链表的具体实现:

package com.zxd.link;/** * DoubleLinkedList:Java实现非循环的双向链表 双向链表:有数据域和指针域组成,指针域中包含了前一个数据和后一个数据的指针,删除和插入操作时需考虑前后方向的操作; 第一个节点的前向指针和最后一个节点的后向指针均指向NULL *  * @author zeng.xiangdong 1770534116@qq.com * @version V1.0 2014-6-30 下午11:03:41 */public class DoubleLinkedList {// 头节点private DoubleLinkedNode headNode;// 链表长度private int size;/** * 构造方法 *  * @param data 头结点的值 */public DoubleLinkedList(int data) {headNode = new DoubleLinkedNode(data);headNode.setPreNode(null);headNode.setNextNode(null);size++;}/** * addFirst:从链表表头插入 *  * @param data 要插入的值 */private void addFirst(int data) {DoubleLinkedNode node = new DoubleLinkedNode(data);node.setPreNode(null);node.setNextNode(headNode);headNode.setPreNode(node);headNode = node;size++;}/** * addLast:从链表表尾插入 *  * @param data 要插入的数据 */private void addLast(int data) throws Exception {if(headNode == null) {throw new Exception("null link list");} else {DoubleLinkedNode node = headNode;// 找出最后一个节点while(hasNext(node)) {node = node.getNextNode();}DoubleLinkedNode currentNode = new DoubleLinkedNode(data);currentNode.setNextNode(null);currentNode.setPreNode(node);node.setNextNode(currentNode);}// end of listsize++;}/** * removeFirst:删除头结点 */private void removeFirst() throws Exception {if(headNode == null) {throw new Exception("null link list");}if(size == 1) {headNode = null;size = 0;} else {DoubleLinkedNode node = headNode.getNextNode();node.setPreNode(null);headNode = node;size--;} // end of else}/** * removeLast:删除尾结点 */private void removeLast() throws Exception {if(headNode == null) {throw new Exception("null link list");}if(size == 1) {headNode = null;size = 0;} else {DoubleLinkedNode node = headNode;while(hasNext(node)) {node = node.getNextNode();}node.getPreNode().setNextNode(null);size--;} // end of else}/** * insert:在指定位置插入数据 *  * @param index 要插入的下标,从0开始 * @param data 要插入的数据 */private void insert(int index, int data) throws Exception {if(headNode == null) {throw new Exception("null link list");} else {// 如果要插入的下标为0,if(index == 0) {addFirst(data);return;}if(index == size) {addLast(data);return;}if(index > size) {throw new Exception("index out of range");}DoubleLinkedNode node = headNode;int currentIndex = 1;while(hasNext(node)) {if(currentIndex == index) {DoubleLinkedNode currentNode = new DoubleLinkedNode(data);currentNode.setPreNode(node);currentNode.setNextNode(node.getNextNode());node.setNextNode(currentNode);size++;break;} // end of ifnode = node.getNextNode();currentIndex++;} // end of while} // end of else}// remove(index), removeFirst(), removeLast(), get(index)/** * remove:删除指定节点 *  * @param index 下标 */private void remove(int index) throws Exception {if(headNode == null) {throw new Exception("null link list");}if(size == 1) {headNode = null;return;}if(size < index + 1) {throw new Exception("index out of range");}// 如果是要删除头结点,或删除最后的节点if(index == 0) {removeFirst();size--;return;}if(index == (size - 1)) {removeLast();size--;return;}DoubleLinkedNode node = headNode;int flag = 0;while(hasNext(node)) {if(flag == index) {DoubleLinkedNode preNode = node.getPreNode();DoubleLinkedNode nextNode = node.getNextNode();preNode.setNextNode(nextNode);nextNode.setPreNode(preNode);return;}node = node.getNextNode();flag++;} // end of while}/** * hasNext:该节点是否是最后的节点 *  * @param node */private boolean hasNext(DoubleLinkedNode node) {return (node.getNextNode() == null) ? false : true;}/** * toString:(这里用一句话描述这个方法的作用) *  * @return */public String toString() {StringBuffer sBuffer = new StringBuffer();if(headNode == null) {try {throw new Exception("null link list");} catch(Exception e) {e.printStackTrace();}} else {DoubleLinkedNode node = headNode;sBuffer.append(node.getData()).append(" ");while(hasNext(node)) {node = node.getNextNode();sBuffer.append(node.getData()).append(" ");}return sBuffer.toString().trim();} // end of elsereturn null;}/** * main:测试main方法 *  * @param args */public static void main(String[] args) throws Exception {DoubleLinkedList list = new DoubleLinkedList(1);// 从表头插入list.addFirst(0);System.out.println(list.toString());// 从表尾插入list.addLast(2);list.addLast(3);System.out.println(list.toString());// 从指定位置插入list.insert(4, 4);System.out.println(list.toString());// // 删除表头list.removeFirst();System.out.println(list.toString());// 删除表尾list.removeLast();System.out.println(list.toString());// 从指定位置删除list.remove(1);System.out.println(list.toString());}}

输出结果:


0 0