本文小编为大家详细介绍“Java数据结构链表的概念是什么与怎么实现”,内容详细,步骤清晰,细节处理妥当,希望这篇“Java数据结构链表的概念是什么与怎么实现”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。
一、什么是链表
链表的概念
链表是一种物理存储结构上非连续存储结构,数据元素的逻辑顺序是通过链表中的引用链接次序实现的 。
链表的结构
链表结构分为8种:

这里我们只讲最下面两种,因为在工作中、业务里、考试题、刷到的链表题、面试题里面都是用到这两种链表。
链表如何存储数据
链表是由一个一个节点组成的。(这里我们以单链表为例)
什么叫做节点?
节点分为两个域 ,假设一个叫做val域,一个叫做next域。
val:数据域
next:下一个节点的地址

链表的实现
//ListNode代表一个节点
class ListNode{
public int val;
public ListNode next;
//构造方法
public ListNode(int val){
this.val = val;
}
}
//MyLinkedList 代表这是一个链表
public class MyLinkedList {
public ListNode head;//链表的头引用,所以定义在链表里,head是链表的头,不是节点的头,节点只有两个属性,一个属性是val值,一个属性是next值,所以不能定义在ListNode类里面
ListNode listNode = new ListNode(2);//节点实例化,val域赋值为2
}
穷举法创建链表
/MyLinkedList 代表这是一个链表
public class MyLinkedList {
public ListNode head;//链表的头引用,所以定义在链表里
public void createList(){
ListNode listNode0 = new ListNode(11);
ListNode listNode1 = new ListNode(26);
ListNode listNode2 = new ListNode(23);
ListNode listNode3 = new ListNode(45);
ListNode listNode4 = new ListNode(56);
listNode0.next = listNode1;
listNode1.next = listNode2;
listNode2.next = listNode3;
listNode3.next = listNode4;
this.head = listNode0;
}
打印链表
//打印链表
public void display(){
ListNode cur = this.head;
while (cur != null){
System.out.print(cur.val+" ");
cur = cur.next;
}
System.out.println();
}
打印结果:

查找是否包含关键字key是否在单链表当中
//查找是否包含关键字key是否在单链表当中
public boolean contains(int key) {
ListNode cur = this.head;
while (cur != null) {
if (cur.val == key) {
return true;
}
cur = cur.next;
}
return false;
}
打印结果:

得到单链表的长度:
//得到单链表的长度
public int size(){
ListNode cur = this.head;
int count = 0;
while(cur != null){
count++;
cur = cur.next;
}
return count;
}
打印结果:

头插法
//头插法
public void addFirst(int data) {
ListNode node = new ListNode(data);
if (this.head == null) {
this.head = node;
} else {
node.next = this.head;
head = node;
}
}
打印结果:

尾插法
//尾插法
public void addLast(int data){
ListNode node = new ListNode(data);
ListNode cur = this.head;
if(this.head == null){
this.head = node;
}else {
while(cur.next != null){
cur = cur.next;
}
cur.next = node;
}
}
打印结果:

任意位置插入,第一个数据节点为0号下标
public ListNode findIndex(int index){
ListNode cur = this.head;
while(index -1 != 0){
cur = cur.next;
index--;
}
return cur;
}
//任意位置插入,第一个数据节点为0号下标
public void addIndex(int index,int data){
if(index < 0 || index > size()){
System.out.println("位置不合法");
return;
}
if(index == 0){
addFirst(data);
return;
}
if(index == size()){
addLast(data);
return;
}
ListNode cur = findIndex(index);
ListNode node = new ListNode(data);
node.next = cur.next;
cur.next = node;
}
打印结果:

删除第一次出现关键字为key的节点
//删除第一次出现关键字为key的节点
public void remove(int key){
if(this.head == null){
System.out.println("没有你要删除的节");
return;
}
if (this.head.val == key){
this.head = this.head.next;
return;
}
ListNode cur = this.head;
while (cur.next != null){
if(cur.next.val == key){
cur.next = cur.next.next;
return;
}
cur = cur.next;
}
if(cur.next == null){
System.out.println("没有该节点");
return;
}
}
打印结果:

删除所有值为key的节点
//删除所有值为key的节点
public ListNode removeAllKey(int key){
if(this.head == null) return null;
ListNode prev = this.head;
ListNode cur = this.head;
while (cur != null){
if(cur.val == key){
prev.next = cur.next;
cur = cur.next;
}else{
prev = cur;
cur = cur.next;
}
}
if(this.head.val == key){
this.head = this.head.next;
}
return this.head;
}
打印结果:

读到这里,这篇“Java数据结构链表的概念是什么与怎么实现”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注天达云行业资讯频道。