自定义双向链表

本文介绍了双向链表的基本概念,详细阐述了节点类的设计,包括数据域和指针域。接着,讨论了如何创建双向链表,包括从头部和尾部插入节点的逻辑,并讲述了删除节点的操作,分别是从头部和尾部删除。最后,提供了两个测试用例来验证双向链表的功能,测试结果显示双向链表操作正确。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

双向链表的含义:

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

 

节点类的定义:

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

/** 
 * 链节点
 */
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 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值