Node节点数据:
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