🟡前言
21天挑战赛的第二周,从本文开始将会介绍链表相关知识,本文主要是介绍有关链表的基本概念
活动地址:CSDN21天学习挑战赛
🟡概述
1️⃣定义
链表是一种物理存储单元上非连续、非顺序的存储结构,其物理结构不能只管的表示数据元素的逻辑顺序,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。
链表由一系列的结点(链表中的每一个元素称为结点)组成,结点可以在运行时动态生成
2️⃣示意图
- 在链表内插入数据只需要移动和添加指针即可
- 在链表内删除元素只需要将前一个节点的指针指向后一个节点即可
🟡节点
1️⃣API设计
-
构造方法
Node(T t, Node next)
-
成员变量
T item
:存储数据
Node next
:指向下一个节点
2️⃣节点代码实现
public class Node<T>{
//存储元素
public T item;
//指向下一个节点
public Node next;
public Node(T item, Node next){
this.item = item;
this.next = next;
}
}
3️⃣生成链表
public static void main(String[] args) throws Exception {
//构建结点
Node<Integer> first = new Node<Integer>(11, null);
Node<Integer> second = new Node<Integer>(13, null);
Node<Integer> third = new Node<Integer>(12, null);
Node<Integer> fourth = new Node<Integer>(8, null);
Node<Integer> fifth = new Node<Integer>(9, null);
//生成链表
first.next = second;
second.next = third;
third.next = fourth;
fourth.next = fifth;
}
🟡结语
本文主要是针对链表有了个初步认识,接下来会介绍单链表、双链表、循环链表及其相关问题