Java數(shù)據(jù)結(jié)構(gòu)之鏈表的增刪查改詳解
一. 概念與結(jié)構(gòu)
鏈表是一種物理存儲結(jié)構(gòu)上非連續(xù)存儲結(jié)構(gòu),數(shù)據(jù)元素的邏輯順序是通過鏈表中的引用鏈接次序?qū)崿F(xiàn)的 。

雖然有這么多的鏈表的結(jié)構(gòu),但是我們重點掌握兩種:
1.無頭單向非循環(huán)鏈表:結(jié)構(gòu)簡單,一般不會單獨用來存數(shù)據(jù)。實際中更多是作為其他數(shù)據(jù)結(jié)構(gòu)的子結(jié)構(gòu),如哈希桶、圖的鄰接表等等。另外這種結(jié)構(gòu)在筆試面試中出現(xiàn)很多。
2.無頭雙向鏈表:在Java的集合框架庫中LinkedList底層實現(xiàn)就是無頭雙向循環(huán)鏈表。
二.單鏈表接口實現(xiàn)
接下來新一帶大家寫無頭單向非循環(huán)鏈表
import java.util.List;
/**
* Created with IntelliJ IDEA.
* Description: 鏈表
* User: mac
* Date: 2022-08-31
* Time: 10:09
*/
//ListNode代表一個節(jié)點 - 存放在一個節(jié)點類中
class ListNode {
public int val;
public ListNode next;
public ListNode(int val) {
this.val = val;
}
}
public class MyLinkedList {
public ListNode head;//鏈表的頭引用
//打印鏈表
public void display() {
//this.head.next != null 會丟失一個數(shù)據(jù)
ListNode cur = this.head;
while (cur != null) {
System.out.print(cur.val + " ");
cur = cur.next;
}
System.out.println();
}
//查找是否包含關(guān)鍵字k
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() {
int count = 0;
ListNode cur = this.head;
while (cur != null) {
count++;
cur = cur.next;
}
return count;
}
//頭插法
public void addFirst(int data){
//綁定位置的時候一定要先綁定后邊
ListNode node = new ListNode(data);
node.next = this.head;
this.head = node;
}
//尾插法
public void addLast(int data){
ListNode node = new ListNode(data);
if (this.head == null){//判空,否則就會造成引用異常this.head.next
this.head = node;
} else {
ListNode cur = this.head;
while (cur.next != null){
cur = cur.next;
}
//cur.next = null;
cur.next = node;
}
}
public ListNode findindex(int index){//通過下表來移動指針
ListNode cur = this.head;
while (index - 1 != 0){
cur = cur.next;
index--;
}
return cur;
}
//任意位置插入,第一個數(shù)據(jù)節(jié)點為0號下標(biāo)
public void addIndex(int index, int data){
if (index < 0 || index > size()){
System.out.println("index位置不合法!");
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;
}
//刪除第一次出現(xiàn)的關(guān)鍵字為key的節(jié)點
public void remove(int key){
if (this.head == null){
System.out.println("單鏈表為空,不能刪除!");
return;
}
if (this.head.val == key){//判斷頭部是否為目標(biāo)節(jié)點
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;
}
System.out.println("未找到該節(jié)點");
}
//刪除所有值為key的節(jié)點
public ListNode removeAllKey(int key){
if (this.head == null) return null;
ListNode prev = this.head;
ListNode cur = this.head.next;
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;
}
//清空鏈表
public void clear(){
//this.head = null;//暴力解決 - 不推薦但沒毛病
while (this.head != null){
ListNode curNext = this.head.next;
this.head.next = null;
this.head = curNext;
}
}
}到此這篇關(guān)于Java數(shù)據(jù)結(jié)構(gòu)之鏈表的增刪查改詳解的文章就介紹到這了,更多相關(guān)Java鏈表增刪查改內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot Actuator未授權(quán)訪問漏洞的排查和解決方法
Spring Boot Actuator 是開發(fā)和管理生產(chǎn)級 Spring Boot 應(yīng)用程序的重要工具,它可以幫助你確保應(yīng)用程序的穩(wěn)定性和性能,本文給大家介紹了SpringBoot Actuator未授權(quán)訪問漏洞的排查和解決方法,需要的朋友可以參考下2024-05-05
Spring Security如何在Servlet中執(zhí)行
這篇文章主要介紹了Spring Security如何在Servlet中執(zhí)行,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-04-04
IntelliJ IDEA報錯Error:java: Compilation failed: internal java
今天小編就為大家分享一篇關(guān)于IntelliJ IDEA報錯Error:java: Compilation failed: internal java compiler error的解決辦法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-10-10
基于SpringBoot后端導(dǎo)出Excel文件的操作方法
這篇文章給大家介紹了基于SpringBoot后端導(dǎo)出Excel文件的操作方法,文中通過代碼示例給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-02-02
java根據(jù)模板導(dǎo)出PDF的詳細(xì)實現(xiàn)過程
前段時間因為相關(guān)業(yè)務(wù)需求需要后臺生成pdf文件,所以下面這篇文章主要給大家介紹了關(guān)于java根據(jù)模板導(dǎo)出PDF的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-02-02

