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

本文介绍了双向链表的基本概念,详细阐述了节点结构,并设计了相应的API。同时,对操作时间复杂度进行了分析,提供了完整的Java源码实现及测试案例。

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

1.双向链表的概述与节点结构
在这里插入图片描述
2.双向链表的API设计
在这里插入图片描述
3.双向链表的时间复杂度分析:
在这里插入图片描述

4.源码实现:

package linkList;

import java.util.Iterator;

public class TwoWayLinkList<T> implements Iterable<T>{
   

	//首节点
	private Node head;
	//尾节点
	private Node last;
	//链表长度
	private int N;
	
	
	//节点类
	private class Node{
   
		//存储数据
		public T item;
		//指向上一个节点
		public Node pre;
		//指向下一个节点
		public Node next;
		//构造方法
		public Node(T item,Node pre, Node next){
   
			this.item=item;
			this.next=next;
			this.pre=pre;
		}
	}
	
	//构造方法:创建Twowaylinklist对象
	public TwoWayLinkList(){
   
		//初始化头节点,刚开始没有前驱也没有后继,只需要一个节点即可
		this.head=new Node(null,null,null);
		//初始化尾节点,刚开始是没有尾节点的
		this.last=null;
		//初始化元素个数
		this.N=0;
	}
	
	//清空链表
	public void clear(){
   
		this.head.next=null;
		this.last.pre=null;
		this.N=0;
	}
	
	//判断是否为空
	public boolean isEmpty(){
   
		return N==0;
	}
	
	//获取链表中元素的个数
	public int length(){
   
		return N;
	
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值