java 实现双向链表(数据结构)

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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值