数据结构-双端链表和双向链表

本文详细介绍了双端链表和双向链表的概念及实现,包括节点的插入、删除、查找等操作,并通过示例代码展示了如何在Java中创建和使用这两种链表。

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

1、双端链表
链表中保存着对最后一个节点的引用,这就是双端链表

package chap05;

import chap04.Node;

//双端链表
public class FirstLastLinkList {
	private Node first;//头结点
	private Node last;//尾结点
	
	  FirstLastLinkList() {
		first=null;
		last=null;
	}
	//插入一个节点,在头结点后进行插入
	public void insertFirst(long value) {
		Node node=new Node(value);
		if(isEmpty()) {
			last=node;
		}
		node.next=first;
		first=node;
	}
	//插入一个节点,在尾结点后进行插入
	public void insertLast(long value) {
		Node node=new Node(value);
		if(isEmpty()) {
			first=node;
		}else {
			last.next=node;
		}
		last=node;
	}
	
	//删除一个节点,在头结点后进行删除
	public Node deleteFirst() {
		Node temp=first;
		if(first.next==null) {
			last=null;
		}
		first=temp.next;
		return temp;
	}
	//显示方法
	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;
		Node previous=first;
		while(current.data!=value) {
			if(current.next==null) {
				return null;
			}
			previous=current;
			current=current.next;
		}
		if(current==first) {
			first=first.next;
		}else {
			previous.next=current.next;
		}
		return current;
	}
	//判断是否为空
	public boolean isEmpty() {
		return first==null;
	}
}

测试类

package chap05;

public class TestFirstLastLinkList {
	public static void main(String[] args) {
		FirstLastLinkList f=new FirstLastLinkList();
		f.insertFirst(34);
		f.insertFirst(56);
		f.insertFirst(67);
		f.display();
		
		f.deleteFirst();
		f.display();
		
		f.insertLast(20);
		f.display();
	}
}

运行结果
在这里插入图片描述
2、双向链表
每个节点除了保存下一个节点的引用之外,同时还保存着对前一个节点的引用

package chap05;
//链节点,相当于车厢
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+" ");
	}
	
}
package chap05;

//双向链表
public class DoubleLinkList {
	private Node first;//头结点
	private Node last;//尾结点
	
	  DoubleLinkList() {
		first=null;
		last=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(long value) {
		Node node=new Node(value);
		if(isEmpty()) {
			first=node;
		}else {
			last.next=node;
			node.previous=last;
		}
		last=node;
	}
	
	//删除一个节点,在头结点后进行删除
	public Node deleteFirst() {
		Node temp=first;
		if(first.next==null) {
			last=null;
		}else {
			first.next.previous=null;
		}
		first=temp.next;
		return temp;
	}
	//删除一个节点,在尾2结点后进行删除
		public Node deleteLast() {
			Node temp=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;
		Node previous=first;
		while(current.data!=value) {
			if(current.next==null) {
				return null;
			}
			previous=current;
			current=current.next;
		}
		if(current==first) {
			first=first.next;
		}else {
			current.previous.next=current.next;
		}
		return current;
	}
	//判断是否为空
	public boolean isEmpty() {
		return first==null;
	}
}
package chap05;

public class TestDoubleLinkList {
	public static void main(String[] args) {
		DoubleLinkList d=new DoubleLinkList();
		d.insertLast(10);
		d.insertLast(20);
		d.insertLast(30);
		d.display();
		
		d.deleteLast();
		d.display();
		
		d.deleteFirst();
		d.display();
	}
}

运行结果
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值