java實(shí)現(xiàn)簡(jiǎn)單單鏈表
本文實(shí)例為大家分享了java實(shí)現(xiàn)簡(jiǎn)單單鏈表的具體代碼,供大家參考,具體內(nèi)容如下
一、定義:
單鏈表是一種鏈?zhǔn)酱嫒〉臄?shù)據(jù)結(jié)構(gòu),用一組地址任意的存儲(chǔ)單元存放線性表中的數(shù)據(jù)元素。鏈表中的數(shù)據(jù)是以結(jié)點(diǎn)來(lái)表示的,每個(gè)結(jié)點(diǎn)的構(gòu)成:元素(數(shù)據(jù)元素的映象) + 指針(相當(dāng)于JAVA中的引用,指示后繼元素存儲(chǔ)位置,),元素就是存儲(chǔ)數(shù)據(jù)的存儲(chǔ)單元,指針就是連接每個(gè)結(jié)點(diǎn)的地址數(shù)據(jù)。
二、結(jié)構(gòu):

如圖所示,data就是當(dāng)前節(jié)點(diǎn)的數(shù)據(jù),next是指針,指針存放的是內(nèi)存地址,是當(dāng)前結(jié)點(diǎn)的下一結(jié)點(diǎn)內(nèi)存地址,順著這個(gè)地址就能找到下一個(gè)結(jié)點(diǎn)。
三、代碼實(shí)現(xiàn):
package com.example.demo.linkedlist;
/**
* 結(jié)點(diǎn)
* Created by xinan on 2021/02/23
*/
public class Node {
public Integer value;
public Node next;
public Node(Integer value) {
this.value = value;
}
public Node(Integer value, Node next) {
this.value = value;
this.next = next;
}
public Integer getValue() {
return value;
}
public void setValue(Integer value) {
this.value = value;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
package com.example.demo.linkedlist;
/**
* 單鏈表
* Created by xinan on 2021/2/23
*/
public class SingleLinkedList {
public Node head;
/**
* 從頭部添加
* @param data 待添加數(shù)據(jù)
*/
public void addHead(Integer data) {
Node node = new Node(data);
node.next = head;
head = node;
}
/**
* 從尾部添加
* @param data 待添加數(shù)據(jù)
*/
public void addLast(Integer data) {
Node node = new Node(data);
if (head == null) {
head = node;
return;
}
Node temp = head;
while (temp.next != null) {
temp = temp.next;
}
temp.next = node;
}
/**
* 獲取鏈表的長(zhǎng)度
* @return 鏈表長(zhǎng)度
*/
public Integer length() {
int length = 0;
Node temp = head;
while (temp != null) {
temp = temp.next;
length ++;
}
return length;
}
/**
* 從指定下標(biāo)處添加
* @param index 指定下標(biāo)
* @param data 待添加的數(shù)據(jù)
*/
public void addByIndex(int index, Integer data) {
if (index < 0 || index > length()) {
System.out.println("插入下標(biāo)不合規(guī),請(qǐng)檢查!");
return;
}
if (index == 0) {
addHead(data);
return;
}
Node node = new Node(data);
Node temp = head;
for (int i = 1; i < index; i++) {
temp = temp.next;
}
node.next = temp.next;
temp.next = node;
}
/**
* 指定下標(biāo)刪除
* @param index 指定下標(biāo)
*/
public void deleteByIndex(int index) {
if (index < 0 || index > length()) {
System.out.println("刪除下標(biāo)不合規(guī),請(qǐng)檢查!");
return;
}
if (index == 0) {
head = head.next;
return;
}
Node temp = head;
for (int i = 1; i < index; i++) {
temp = temp.next;
}
temp.next = temp.next.next;
}
/**
* 通過(guò)下標(biāo)獲取結(jié)點(diǎn)
* @param index 下標(biāo)
* @return 結(jié)點(diǎn)
*/
public Node getByIndex(Integer index) {
if (index < 0 || index > length() - 1) {
System.out.println("不存在此下標(biāo)結(jié)點(diǎn)");
}
Node temp = head;
int i = 0;
while (temp != null) {
if (i == index) {
return temp;
}
i ++;
temp = temp.next;
}
return null;
}
/**
* 打印鏈表值
*/
public void printLink() {
Node temp = head;
while (temp != null) {
System.out.println(temp.value);
temp = temp.next;
}
}
/**
* 打印某個(gè)節(jié)點(diǎn)之后的所有值
* @param node
*/
public static void printAfterNode(Node node) {
while (node != null) {
System.out.println(node.value);
node = node.next;
}
}
/**
* 清除單鏈表
*/
public void clearLink() {
head = null;
}
/**
* 單鏈表反轉(zhuǎn)
* @param head 頭節(jié)點(diǎn)
*/
public Node reverseLink(Node head) {
Node prev = null;
Node curr = head;
while (curr != null) {
Node nextTemp = curr.next;
curr.next = prev;
prev = curr;
curr = nextTemp;
}
return prev;
}
/**
* 測(cè)試
* @param args
*/
public static void main(String[] args) {
SingleLinkedList linkNode = new SingleLinkedList();
linkNode.addHead(2);
linkNode.addHead(3);
linkNode.addHead(5);
linkNode.addLast(9);
linkNode.addLast(7);
System.out.println("打印單鏈表: ");
linkNode.printLink();
Node byIndex1 = linkNode.getByIndex(0);
System.out.println("獲取下標(biāo)為1的結(jié)點(diǎn)值: " + byIndex1.value);
linkNode.addByIndex(2, 8);
System.out.println("下標(biāo)2添加后打印單鏈表: ");
linkNode.printLink();
linkNode.addByIndex(0, 11);
System.out.println("下標(biāo)0添加后打印單鏈表: ");
linkNode.printLink();
linkNode.deleteByIndex(0);
System.out.println("下標(biāo)0刪除后打印單鏈表: ");
linkNode.printLink();
Node node = linkNode.reverseLink(linkNode.head);
System.out.println("反轉(zhuǎn)后打印單鏈表: ");
printAfterNode(node);
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用Java實(shí)現(xiàn)DNS域名解析的簡(jiǎn)單示例
這篇文章主要介紹了使用Java實(shí)現(xiàn)DNS域名解析的簡(jiǎn)單示例,包括對(duì)一個(gè)動(dòng)態(tài)IP主機(jī)的域名解析例子,需要的朋友可以參考下2015-10-10
SpringCloud Alibaba 基本開(kāi)發(fā)框架搭建過(guò)程
這篇文章主要介紹了SpringCloud Alibaba 基本開(kāi)發(fā)框架搭建過(guò)程,開(kāi)發(fā)工具選用的idea,本文通過(guò)圖文實(shí)例相結(jié)合給大家分享搭建全過(guò)程,需要的朋友可以參考下2021-06-06
springboot使用CommandLineRunner解決項(xiàng)目啟動(dòng)時(shí)初始化資源的操作
這篇文章主要介紹了springboot使用CommandLineRunner解決項(xiàng)目啟動(dòng)時(shí)初始化資源的操作,幫助大家更好的理解和學(xué)習(xí)使用springboot框架,感興趣的朋友可以了解下2021-02-02
Spring Cloud GateWay 路由轉(zhuǎn)發(fā)規(guī)則介紹詳解
這篇文章主要介紹了Spring Cloud GateWay 路由轉(zhuǎn)發(fā)規(guī)則介紹詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-05-05
SpringBoot+Mybatis使用Mapper接口注冊(cè)的幾種方式
本篇博文中主要介紹是Mapper接口與對(duì)應(yīng)的xml文件如何關(guān)聯(lián)的幾種姿勢(shì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-07-07
SpringBoot中關(guān)于static和templates的注意事項(xiàng)以及webjars的配置
今天小編就為大家分享一篇關(guān)于SpringBoot中關(guān)于static和templates的注意事項(xiàng)以及webjars的配置,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-01-01
Java Poi 在Excel中輸出特殊符號(hào)的實(shí)現(xiàn)方法
這篇文章主要介紹了Java Poi 在Excel中輸出特殊符號(hào)的實(shí)現(xiàn)方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07
Java不用算數(shù)運(yùn)算符來(lái)實(shí)現(xiàn)求和方法
我們都知道,Java的運(yùn)算符除了具有優(yōu)先級(jí)之外,還有一個(gè)結(jié)合性的特點(diǎn)。當(dāng)一個(gè)表達(dá)式中出現(xiàn)多種運(yùn)算符時(shí),執(zhí)行的先后順序不僅要遵守運(yùn)算符優(yōu)先級(jí)別的規(guī)定,還要受運(yùn)算符結(jié)合性的約束,以便確定是自左向右進(jìn)行運(yùn)算還是自右向左進(jìn)行運(yùn)算,但是如果不用運(yùn)算符怎么求和呢2022-04-04

