简单双向链表队列的创建
/** * 定义一个双向链表的节点类 * @author YangKang * */public class DoublyNode {private Object data;//节点内的数据对象private DoublyNode child;//对下一个节点的引用private DoublyNode parent;//对上一节点的引用//在创建节点对象的时候就传入节点中的数据对象public DoublyNode (Object data) {this.data = data;}public Object getData() {return data;}public void setData(Object data) {this.data = data;}public DoublyNode getChild() {return child;}public void setChild(DoublyNode child) {this.child = child;}public DoublyNode getParent() {return parent;}public void setParent(DoublyNode parent) {this.parent = parent;}}
?
?
然后定义双向链表队列(主函数用来测试其功能)
?
/** * 定义一个简单的双向链表 * @author YangKang 2013.07.16 * */public class DoublyNodeList {public static DoublyNode root = null;//根节点public static DoublyNode last = null;//最后一个节点public static void main(String[] args) {//加入节点DoublyNodeList list = new DoublyNodeList();//从链表最后添加节点,先进先出list.add("aa");list.add("bb");list.add("cc"); //插入节点list.insertDoublyNode(1, "000");//链表队列置空list.setNull();list.add("aa");list.add("bb");list.add("cc"); //插入节点list.insertDoublyNode(2, "000");//删除节点list.deleteDoublyNote(3);//遍历节点list.printDoublyNodeList(root);}??
?
在尾部增加节点
?
/** * 在尾部增加节点 * @param obj :插入的节点对象 */public void add(Object obj){DoublyNode node = new DoublyNode(obj);if(null == root){root = node;last = root;}else{last.setChild(node);node.setParent(last);last = node;}}??
?
在指定索引下插入节点
?
/** * 在指定索引下插入节点 * @param index :(索引值)第几个节点(从零开始) * @param obj :需要插入的节点对象 */public void insertDoublyNode(int index,Object obj){if((this.getLength() < index)||(index < 0)){throw new java.lang.RuntimeException("下标越界:" + index + ",最大长度:" +this.getLength());}else{//创建一个新节点DoublyNode newNode = new DoublyNode(obj);//得到当前的索引位置的节点DoublyNode node = this.getDoublyNode(index);if(index == 0){//若链表中没有节点,则插入的节点作为根节点root = newNode;}else{//得到父节点DoublyNode fNode = node.getParent();//加入待插入的节点,设置新的引用关系fNode.setChild(newNode);newNode.setParent(fNode);}//设置新的引用关系newNode.setChild(node);node.setParent(newNode);}}?
根据索引删除节点
?
/** * 根据索引删除节点 * @param index : (索引)第几个节点(从零开始) */public void deleteDoublyNote(int index){if((this.getLength() < index)||(index < 0)){throw new java.lang.RuntimeException("下标越界:" + index + ",最大长度:" +this.getLength());}else{//得到当前索引位置的节点DoublyNode node =this.getDoublyNode(index);//得到父节点DoublyNode fNode = node.getParent();//得到父节点DoublyNode cNode = node.getChild();//设置新的索引关系if (fNode == null){root = cNode;}else if(cNode == null){fNode.setChild(null);}else {fNode.setChild(cNode);cNode.setParent(fNode);}}}??
?
根据索引取出节点
?
/** * 根据索引取出节点 * @param index :第几个节点(从零开始索引) * @return :根据索引返回的节点 */public DoublyNode getDoublyNode(int index){if((this.getLength() < index)||(index < 0)){throw new java.lang.RuntimeException("下标越界:" + index + ",最大长度:" +this.getLength());}else{int num = 0;DoublyNode node = root;while (num != index){node = node.getChild();num++;}return node;}}??
?
?得到链表的长度
/** * 得到链表的长度 * @return 链表的长度 */public int getLength(){int count = 0;//内部计数器,可以不受多线程的影响if(root == null){return count;}DoublyNode node = root.getChild();while (null != node){count++;node = node.getChild();}return count + 1;}?
?
修改对象节点
/** * 修改对象节点 * @param index 对象节点的索引 * @param obj修改对象内容 */public void ReviseDoublyNode(int index,Object obj){if(this.getLength() < index || index < 0){throw new java.lang.RuntimeException("下标越界:" + index + ",最大长度:" +this.getLength());}else{//得到当前索引的位置DoublyNode node = this.getDoublyNode(index);node.setData(obj);}}?
?
遍历链表的方法
?
/** * 遍历链表的方法 * @param root :链表的根节点 */public void printDoublyNodeList(DoublyNode root){if (null != root ){Object data = root.getData();System.out.println(data);DoublyNode temp = root.getChild();printDoublyNodeList(temp);}}?
链表置空操作:
/** * 链表置空操作 */public void setNull(){root = null;last = null;}?