LinkedList底层结构
LinkedList的全面说明
- LinkedList底层实现了双向链表和双端队列特点
- 可以添加任意元素(元素可以重复),包括null
- 线程不安全,没有实现同步
LinkedList的底层操作机制
- LinkedList底层维护了一个双向链表.
- LinkedList中维护了两个属性first和last分别指向首节点和尾节点
- 每个节点(Node对象),里面又维护了prev、next、item三个属性,其中通过prev指向前一个,通过next指向后一个节点。最终实现双向链表.
- 所以LinkedList的元素的添加和删除,不是通过数组完成的,相对来说效率较高。
- 模拟一个简单的双向链表
public class LinkedList01 {
public static void main(String[] args) {
Node jack = new Node("jack");
Node tom = new Node("tom");
Node wsh = new Node("wsh");
jack.next = tom;
tom.next = wsh;
wsh.pre = tom;
tom.pre = jack;
Node first = jack;
Node last = wsh;
System.out.println("===从头到尾进行遍历===");
while (true){
if(first == null){
break;
}
System.out.println(first);
first = first.next;
}
System.out.println("====从尾到头的遍历====");
while (true){
if(last == null){
break;
}
System.out.println(last);
last = last.pre;
}
Node smith = new Node("smith");
tom.next = smith;
smith.next = wsh;
wsh.pre = smith;
smith.pre = tom;
first = jack;
System.out.println("===从头到尾进行遍历===");
while (true){
if(first == null){
break;
}
System.out.println(first);
first = first.next;
}
last = wsh;
System.out.println("====从尾到头的遍历====");
while (true){
if(last == null){
break;
}
System.out.println(last);
last = last.pre;
}
}
}
class Node{
public Object item;
public Node next;
public Node pre;
public Node(Object name) {
this.item = name;
}
@Override
public String toString() {
return "Node name = " + item;
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?