java 实现双向链表(数据结构)
2013-12-11 17:32
886 查看
Node节点数据:
链表类 实现增删查基本操作:
测试结果:
total size 3
name L0 value: 1
name L1 value: 1
name L2 value: 4
name L0 value: 1
name L1 value: 1
total size 2
package com.model; public class Node { public String name ; public String value ; public Node(String name , String value){ this.name = name; this.value = value; } public Node(){ } }
链表类 实现增删查基本操作:
package com.model; public class ListNode { public Node node = null; private ListNode next = null; private ListNode pre = null; public ListNode(){ node = new Node(); } public ListNode(String name ,String value){ node = new Node(name, value); } public void setNext(ListNode next){ this.next = next; } public ListNode getNext(){ return this.next; } public void setPre(ListNode pre){ this.pre = pre; } public ListNode getPre(){ return this.pre; } public int getLength(){ int length = 0 ; ListNode index = new ListNode(); index = this; while(index.next!=null){ index = index.next; length++; } return length; } // public boolean insert(int index , ListNode insertNode){ if(index<0) { System.out.println("this position is quite small"); } else if(index==0) { this.next = insertNode ; insertNode.pre = this; return true; } else if(index>this.getLength()) { System.out.println("this position is out of boundary"); } else{ ListNode replace = new ListNode(); replace = this.select(index); insertNode.next = replace.next; replace.next = insertNode; insertNode.pre = replace; return true; } return false; } // public ListNode select(int index){ if(index<=0) { System.out.println("this position is quite small"); return null; } if(index>this.getLength()) { System.out.println("this position is out of boundary"); return null; } ListNode node_value = new ListNode(); node_value = this; int position = 0; do{ if(node_value.next!=null){ node_value = node_value.next; position ++; }else{ System.out.println("Nullsss"); } }while(position!=index); return node_value; } public boolean delete(int index){ if(index<=0) { System.out.println("this position is quite small,can't delete"); } else if(index>this.getLength()){ System.out.println("this position is out of boundary"); } else{ ListNode deleteNode = this.select(index); deleteNode.pre.next = deleteNode.next; return true; } return false; } }测试类:
package com.model; public class TestNode { public ListNode test,test1,test2; public static void main(String[] args) { new TestNode(); } public TestNode(){ test = new ListNode("L0","1"); test1 = new ListNode("L1","1"); test2 = new ListNode("L2","4"); ListNode head = new ListNode("head","head");//将其作为头节点,不计入链表中 head.insert(0, test); head.insert(1, test1); head.insert(2, test2); System.out.println("total size "+head.getLength()); System.out.println("name "+head.select(1).node.name+" value: "+head.select(1).node.value); System.out.println("name "+head.select(2).node.name+" value: "+head.select(2).node.value); System.out.println("name "+head.select(3).node.name+" value: "+head.select(3).node.value); head.delete(3); System.out.println("\n"+"name "+head.select(1).node.name+" value: "+head.select(1).node.value); System.out.println("name "+head.select(2).node.name+" value: "+head.select(2).node.value); System.out.println("total size "+head.getLength()); } }
测试结果:
total size 3
name L0 value: 1
name L1 value: 1
name L2 value: 4
name L0 value: 1
name L1 value: 1
total size 2
相关文章推荐
- 数据结构之循环双向链表java实现
- 使用java实现双向链表数据结构
- [java数据结构]--java双向链表LinkedList的简单实现
- 数据结构--java实现双向链表
- 双向链表-数据结构-java实现
- java与数据结构(4)---java实现双向循环链表
- java数据结构之实现双向链表的示例
- java数据结构:双向链表结构与实现
- Java数据结构-线性表之双向链表
- 数据结构:线性表的链式存储(单向链表)--Java实现
- Java 单链表的反转 以及 双向链表的实现
- java实现二叉树的三种遍历方式,并把二叉树转为双向链表,输出二叉树
- C实现通用数据结构--双向链表
- JAVA实现单双向链表的增、删、改、查
- Java自己实现双向链表LinkList
- 数据结构-Java实现链表
- 数据结构Java实现【3】----单向链表的插入和删除
- 链表的介绍及双向链表的实现c/c++/java
- Java模拟单向链表和双向链表的实现
- JAVA实现最简单的双向链表、队列与栈(内部使用链表形式存储)