自定义双向链表

 

双向链表的含义:

每个节点保存着下一个节点的引用,同时还保存着对前一个节点的引用

 

节点类的定义:

节点类中需要定义数据域、和指针域,其中指针域包括下一个节点和前一个节点。

/** 
 * 链节点
 */
public class Node {
	//数据域
	public long data;
	//节点域(指针域)
	public Node next;
	public Node previous;
	
	public Node(long value) {
		this.data=value;
	}
	/**
	 * 显示方法
	 */
	public void display(){
		System.out.print(data+" ");
	}
}

双向链表创建:

1.双向链表是在双端链表基础上得来的,所以双向链表中需要定义头结点和尾节点。

public class DoubleLinkedList {
	//头结点
	private Node first;
	//尾节点
	private Node last;
	
	public DoubleLinkedList() {
		first = null;
		last = null;

    
    //待补充...

	}
}

2.从头部进行插入:

要对链表进行判断,如果空则设置尾节点为新添加的节点。如果不为空,还需要设置头结点的前一个节点为新添加的节   点。

public class DoubleLinkedList {
	//头结点
	private Node first;
	//尾节点
	private Node last;
	
	public DoubleLinkedList() {
		first = null;
		last = null;
	}
        /*
	 * 判断是否为空
	 */
	
	public boolean isEmpty() {
		return (first ==null);
	}
	
	/**
	 * 插入节点,在头结点后插入
	 */
	public void insertFirst(long value) {
		Node node = new Node(value);
		if (isEmpty()) {
			last = node;
		}else {
			first.previous = node;
		}
		
			node.next = first;
			first = node;			
	}
}

3.从尾部进行插入:

如果链表为空,则直接设置头节点为新添加的节点,否则设置尾节点的后一个节点为新添加节点。同时设置新添加的节点的前一个节点尾节点。

	//接上

        /**
	 * 插入一个节点,从尾节点进行插入
	 */
	public void insertLast(int value) {
		Node node = new Node(value);
		if (isEmpty()) {
			first = node;
		}
		else {
			last.next = node;
			node.previous = last;
		}
		last = node;
	}

4.从头部进行删除:

判断头结点是否有下一个节点,如果没有则设置尾节点为null。否则设置尾节点的下一个节点的previous为null

        //接上
        /**
	 * 删除节点,删除第一个节点
	 */
	public Node deleteFirst(){
		Node tmp = first;
		if (first.next ==null) {
			last = null;
		}else {
			first.next.previous =null;
		}
		first = tmp.next;
		return tmp;
	}

5.从尾部进行删除

如果头结点后没有其他节点,则设置节点为null 。否则设置尾节点前一个节点的next位null。 设置尾节点为其前一个节点。

        //接上
        /*
	 * 删除节点,从尾部删除
	 */
	public Node deleteLast() {
		Node tmp = last;
		if (first.next ==null) {
			first = null;
		}
		else {
				last.previous.next=null;				
			}
		last = last.previous;
		return last;
	}

最后附上完整双向链表的代码:


/*
 * 双向链表
 */

public class DoubleLinkedList {
	//头结点
	private Node first;
	//尾节点
	private Node last;
	
	public DoubleLinkedList() {
		first = null;
		last = null;
	}
	/*
	 * 判断是否为空
	 */
	
	public boolean isEmpty() {
		return (first ==null);
	}
	
	/**
	 * 插入节点,在头结点后插入
	 */
	public void insertFirst(long value) {
		Node node = new Node(value);
		if (isEmpty()) {
			last = node;
		}else {
			first.previous = node;
		}
		
			node.next = first;
			first = node;			
	}
	/**
	 * 插入一个节点,从尾节点进行插入
	 */
	public void insertLast(int value) {
		Node node = new Node(value);
		if (isEmpty()) {
			first = node;
		}
		else {
			last.next = node;
			node.previous = last;
		}
		last = node;
	}
	
	
	
	/**
	 * 删除节点,删除第一个节点
	 */
	public Node deleteFirst(){
		Node tmp = first;
		if (first.next ==null) {
			last = null;
		}else {
			first.next.previous =null;
		}
		first = tmp.next;
		return tmp;
	}
	/*
	 * 删除节点,从尾部删除
	 */
	public Node deleteLast() {
		Node tmp = last;
		if (first.next ==null) {
			first = null;
		}
		else {
				last.previous.next=null;				
			}
		last = last.previous;
		return last;
	}
	
	/**
	 * 显示方法
	 */
	public void display() {
		Node current = first;
		while (current!=null) {
			current.display();
			current = current.next;
		}
		System.out.println();
	}
	/**
	 * 查找方法
	 */
	public Node find(long value) {
		Node current = first;
		while (current.data != value) {
			if (current.next==null) {
				return null;
			}
			current = current.next;
		}
		return current;
	}
	
	/**
	 * 删除方法,根据数据域来进行删除
	 */
	public Node delete(long value) {
		Node current = first;

		while(current.data != value) {
			if (current.next == null) {
				return null;
			}
			current = current.next;
		}
		if (current == first) {
			first = first.next;		
		}else {
			current.previous.next = current.next; 
		}
		return current;
	}
}

双向链表测试用例1:

public class TestDoubleLinkedList {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		DoubleLinkedList d1 = new DoubleLinkedList();
		d1.insertLast(2);
		d1.insertLast(4);
		d1.insertLast(6);
		d1.insertLast(8);
		
		d1.display();
	
		while (!d1.isEmpty()) {
			d1.deleteLast();
			d1.display();
		}		
	}

}

测试结果:

2 4 6 8 
2 4 6 
2 4 
2 

​​​​​​​双向链表测试用例2:

public class TestDoubleLinkedList {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		DoubleLinkedList d1 = new DoubleLinkedList();
		d1.insertLast(2);
		d1.insertLast(4);
		d1.insertLast(6);
		d1.insertLast(8);
		
		d1.display();
		
		while (!d1.isEmpty()) {
			d1.deleteFirst();
			d1.display();
		}
	}
}

测试结果:

2 4 6 8 
2 4 6 
2 4 
2 

 

 

 

 

 

猜你喜欢

转载自blog.csdn.net/zjt980452483/article/details/81145525
今日推荐