Java數(shù)據(jù)結(jié)構(gòu)之簡(jiǎn)單鏈表的定義與實(shí)現(xiàn)方法示例
本文實(shí)例講述了Java數(shù)據(jù)結(jié)構(gòu)之簡(jiǎn)單鏈表的定義與實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:
一、概述:
1、原理:
只有一個(gè)數(shù)據(jù)項(xiàng)(鏈接點(diǎn)Link),每個(gè)數(shù)據(jù)插入時(shí)都是對(duì)第一個(gè)數(shù)據(jù)的引用。
2、插入數(shù)據(jù)說(shuō)明:
當(dāng)鏈表沒(méi)有數(shù)據(jù)時(shí),插入的值就是第一個(gè)數(shù)據(jù),如果鏈表里有數(shù)據(jù),就把當(dāng)前的數(shù)據(jù)的next指針指向第一個(gè)數(shù)據(jù)。
3、插入數(shù)據(jù)圖:

4、特點(diǎn):先進(jìn)后出
5、實(shí)現(xiàn)功能:
數(shù)據(jù)插入,指定位置插入,顯示,查詢,刪除等
6、刪除原理

7、插入頭節(jié)點(diǎn)原理

二、實(shí)現(xiàn):
1、創(chuàng)建節(jié)點(diǎn)
/**
* @描述 節(jié)點(diǎn)
* @項(xiàng)目名稱 Java_DataStruct
* @包名 com.struct.linklist
* @類名 Node
* @author chenlin
* @date 2010年6月26日 上午7:58:59
* @version 1.0
*/
public class Node {
public long data;
public Node next;
public long getData() {
return data;
}
public void display(){
System.out.print(data + " ");
}
public Node(long data) {
this.data = data;
}
public void setData(long data) {
this.data = data;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
2、鏈表實(shí)現(xiàn)
/**
* @描述 鏈表
* @項(xiàng)目名稱 Java_DataStruct
* @包名 com.struct.linklist
* @類名 LinkList
* @author chenlin
* @date 2010年6月26日 上午8:00:28
* @version 1.0
*/
public class LinkList {
private Node first;
public LinkList(){
first = null;
}
/**
* 插入數(shù)據(jù)
* @param value
*/
public void insertFirst(long value){
Node newNode = new Node(value);
if (first == null) {
first = newNode;
}else {
//把first節(jié)點(diǎn)往下移動(dòng)
newNode.next = first;
//把插入的節(jié)點(diǎn)作為新的節(jié)點(diǎn)
first = newNode;
}
}
/**
* 刪除頭節(jié)點(diǎn)
* @param value
* @return
*/
public Node deleteFirst(){
if (first == null) {
throw new RuntimeException("鏈表數(shù)據(jù)不存在");
}
Node temp = first;
first = temp.next;
return temp;
}
public Node deleteByKey(long key){
Node current = first;
Node last = first;
while(current.data != key){
if (current.next == null) {
System.out.println("沒(méi)找到節(jié)點(diǎn)");
return null;
}
last = current;
current = current.next;
}
if (current == first) {
//return deleteFirst();
//指向下個(gè)就表示刪除第一個(gè)
first = first.next;
}else {
last.next = current.next;
}
return current;
}
/**
* 顯示所有的數(shù)據(jù)
*/
public void display(){
if (first == null) {
//throw new RuntimeException("鏈表數(shù)據(jù)不存在");
return;
}
Node current = first;
while(current != null){
current.display();
current = current.next;
}
System.out.println("---------------");
}
/**
* 查找節(jié)點(diǎn)1
* @param value
* @return
*/
public Node findByValue(long value){
Node current = first;
while(current != null){
if (current.data != value) {
current = current.next;
}else {
break;
}
}
if (current == null) {
System.out.println("沒(méi)找到");
return null;
}
return current;
}
/**
* 查找節(jié)點(diǎn)2
*
* @param key
* @return
*/
public Node findByKey(long key) {
Node current = first;
while (current.data != key) {
if (current.next == null) {
System.out.println("沒(méi)找到");
return null;
}
current = current.next;
}
return current;
}
/**
* 根據(jù)索引查找對(duì)應(yīng)的值
* @param position
* @return
*/
public Node findByPosition(int position){
Node current = first;
//為什么是position - 1,因?yàn)橐褂帽闅v,讓current指向下一個(gè), 所以position - 1的下個(gè)node就是要找的值
for (int i = 0; i < position - 1 ; i++) {
current = current.next;
}
return current;
}
public static void main(String[] args) {
LinkList linkList = new LinkList();
linkList.insertFirst(21);
linkList.insertFirst(22);
linkList.insertFirst(23);
linkList.insertFirst(24);
linkList.insertFirst(25);
linkList.insertFirst(26);
linkList.insertFirst(27);
System.out.println("腳本之家測(cè)試結(jié)果:");
linkList.display();
System.out.println("---查找-------------------------------------");
linkList.findByKey(25).display();
System.out.println("--刪除first-------------------------------------");
//linkList.deleteFirst().display();
///linkList.deleteFirst().display();
//linkList.deleteFirst().display();
//linkList.deleteFirst().display();
System.out.println("-刪除指定值---------------------------------------");
linkList.deleteByKey(27).display();
linkList.deleteByKey(21).display();
System.out.println("----------------------------------------");
linkList.display();
}
}
顯示結(jié)果:

更多關(guān)于java算法相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
- java實(shí)現(xiàn)單鏈表中是否有環(huán)的方法詳解
- java實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)單鏈表示例(java單鏈表)
- Java單鏈表基本操作的實(shí)現(xiàn)
- Java實(shí)現(xiàn)單鏈表翻轉(zhuǎn)實(shí)例代碼
- java實(shí)現(xiàn)單鏈表之逆序
- Java單鏈表的實(shí)現(xiàn)代碼
- Java模擬單鏈表和雙端鏈表數(shù)據(jù)結(jié)構(gòu)的實(shí)例講解
- java 實(shí)現(xiàn)單鏈表逆轉(zhuǎn)詳解及實(shí)例代碼
- java實(shí)現(xiàn)單鏈表增刪改查的實(shí)例代碼詳解
- Java實(shí)現(xiàn)單鏈表的操作
相關(guān)文章
java項(xiàng)目中讀取jdbc.properties文件操作
這篇文章主要介紹了java項(xiàng)目中讀取jdbc.properties文件操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08
Java二分查找算法實(shí)現(xiàn)代碼實(shí)例
這篇文章主要介紹了Java二分查找算法實(shí)現(xiàn)代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
探討Java中最常見(jiàn)的十道面試題(超經(jīng)典)
本篇文章是對(duì)Java中最常見(jiàn)的十道面試題進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-07-07
Java中關(guān)于http請(qǐng)求獲取FlexManager某設(shè)備分組監(jiān)控點(diǎn)
這篇文章主要介紹了Java中關(guān)于http請(qǐng)求獲取FlexManager某設(shè)備分組監(jiān)控點(diǎn),本文僅僅介紹了使用http請(qǐng)求獲取FlexManager平臺(tái)某個(gè)FBox盒子即某設(shè)備的監(jiān)控點(diǎn)分組的分組下的所有監(jiān)控點(diǎn)信息,需要的朋友可以參考下2022-10-10
JAVA 數(shù)據(jù)結(jié)構(gòu)之Queue處理實(shí)例代碼
這篇文章主要介紹了JAVA 數(shù)據(jù)結(jié)構(gòu)之Queue處理實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-02-02

