Java單鏈表基本操作的實(shí)現(xiàn)
最近被問(wèn)到鏈表,是一個(gè)朋友和我討論Java的時(shí)候說(shuō)的。說(shuō)實(shí)話,我學(xué)習(xí)編程的近一年時(shí)間里,學(xué)到的東西還是挺少的。語(yǔ)言是學(xué)了Java和C#,關(guān)于Web的學(xué)了一點(diǎn)Html+css+javascript。因?yàn)楸容^偏好,學(xué)習(xí)WinForm時(shí)比較認(rèn)真,數(shù)據(jù)庫(kù)操作也自己有所研究。但鏈表這個(gè)東西我還真沒(méi)有學(xué)習(xí)和研究過(guò),加上最近自己在看WPF,而課程也到了JSP了,比較緊。
但是我還是抽了一個(gè)晚上加半天的時(shí)間看了一下單向鏈表。并且使用Java試著寫(xiě)了一個(gè)實(shí)例出來(lái)。沒(méi)有接觸過(guò)鏈表的朋友可以作為參考,希望大家多提寶貴意見(jiàn)。
我們首先解釋一下什么是鏈表。就我所知,鏈表是一種數(shù)據(jù)結(jié)構(gòu),和數(shù)組同級(jí)。比如,Java中我們使用的ArrayList,其實(shí)現(xiàn)原理是數(shù)組。而LinkedList的實(shí)現(xiàn)原理就是鏈表了。我的老師說(shuō),鏈表在進(jìn)行循環(huán)遍歷時(shí)效率不高,但是插入和刪除時(shí)優(yōu)勢(shì)明顯。那么他有著愈十年的編程經(jīng)驗(yàn),我是相信的。不過(guò)不知道他是否是說(shuō)雙向鏈表,我們?cè)诖四刂粚?duì)單向鏈表做一個(gè)了解。
鏈表(Chain本文所說(shuō)鏈表均為單向鏈表,以下均簡(jiǎn)稱(chēng)單向鏈表)實(shí)際上是由節(jié)點(diǎn)(Node)組成的,一個(gè)鏈表?yè)碛胁欢〝?shù)量的節(jié)點(diǎn)。而向外暴露的只有一個(gè)頭節(jié)點(diǎn)(Head),我們對(duì)鏈表的所有操作,都是直接或者間接地通過(guò)其頭節(jié)點(diǎn)來(lái)進(jìn)行的。
節(jié)點(diǎn)(Node)是由一個(gè)需要儲(chǔ)存的對(duì)象及對(duì)下一個(gè)節(jié)點(diǎn)的引用組成的。也就是說(shuō),節(jié)點(diǎn)擁有兩個(gè)成員:儲(chǔ)存的對(duì)象、對(duì)下一個(gè)節(jié)點(diǎn)的引用。
這樣說(shuō)可能大家不是很明白,我貼一張圖大家可能更容易理解。
Java單鏈表基本操作的實(shí)現(xiàn)關(guān)鍵代碼如下所示:
package com.tyxh.link; //節(jié)點(diǎn)類(lèi) public class Node { protected Node next; //指針域 protected int data;//數(shù)據(jù)域 public Node( int data) { this. data = data; } //顯示此節(jié)點(diǎn) public void display() { System. out.print( data + " "); } } package com.tyxh.link; //單鏈表 public class LinkList { public Node first; // 定義一個(gè)頭結(jié)點(diǎn) private int pos = 0;// 節(jié)點(diǎn)的位置 public LinkList() { this. first = null; } // 插入一個(gè)頭節(jié)點(diǎn) public void addFirstNode( int data) { Node node = new Node(data); node. next = first; first = node; } // 刪除一個(gè)頭結(jié)點(diǎn),并返回頭結(jié)點(diǎn) public Node deleteFirstNode() { Node tempNode = first; first = tempNode. next; return tempNode; } // 在任意位置插入節(jié)點(diǎn) 在index的后面插入 public void add(int index, int data) { Node node = new Node(data); Node current = first; Node previous = first; while ( pos != index) { previous = current; current = current. next; pos++; } node. next = current; previous. next = node; pos = 0; } // 刪除任意位置的節(jié)點(diǎn) public Node deleteByPos( int index) { Node current = first; Node previous = first; while ( pos != index) { pos++; previous = current; current = current. next; } if(current == first) { first = first. next; } else { pos = 0; previous. next = current. next; } return current; } // 根據(jù)節(jié)點(diǎn)的data刪除節(jié)點(diǎn)(僅僅刪除第一個(gè)) public Node deleteByData( int data) { Node current = first; Node previous = first; //記住上一個(gè)節(jié)點(diǎn) while (current. data != data) { if (current. next == null) { return null; } previous = current; current = current. next; } if(current == first) { first = first. next; } else { previous. next = current. next; } return current; } // 顯示出所有的節(jié)點(diǎn)信息 public void displayAllNodes() { Node current = first; while (current != null) { current.display(); current = current. next; } System. out.println(); } // 根據(jù)位置查找節(jié)點(diǎn)信息 public Node findByPos( int index) { Node current = first; if ( pos != index) { current = current. next; pos++; } return current; } // 根據(jù)數(shù)據(jù)查找節(jié)點(diǎn)信息 public Node findByData( int data) { Node current = first; while (current. data != data) { if (current. next == null) return null; current = current. next; } return current; } } package com.tyxh.link; //測(cè)試類(lèi) public class TestLinkList { public static void main(String[] args) { LinkList linkList = new LinkList(); linkList.addFirstNode(20); linkList.addFirstNode(21); linkList.addFirstNode(19); //19,21,20 linkList.add(1, 22); //19,22,21,20 linkList.add(2, 23); //19,22,23,21,20 linkList.add(3, 99); //19,22,23,99,21,20 linkList.displayAllNodes(); // Node node = linkList.deleteFirstNode(); // System.out.println("node : " + node.data); // linkList.displayAllNodes(); // node = linkList.deleteByPos(2); // System.out.println("node : " + node.data); // linkList.displayAllNodes(); // linkList.deleteFirstNode(); Node node = linkList.deleteByData(19); // Node node = linkList.deleteByPos(0); System. out.println( "node : " + node. data); linkList.displayAllNodes(); Node node1 = linkList.findByPos(0); System. out.println( "node1: " + node1. data); Node node2 = linkList.findByData(22); System. out.println( "node2: " + node2. data); } }
以上所述是小編給大家介紹的Java單鏈表基本操作的實(shí)現(xiàn),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Java中常見(jiàn)的編碼集問(wèn)題總結(jié)
這篇文章主要為大家整理了一些Java中常見(jiàn)的編碼集問(wèn)題,文中的示例代碼講解詳細(xì),對(duì)我們深入理解Java有一定的幫助,感興趣的小伙伴可以了解一下2023-02-02IDEA的常見(jiàn)的設(shè)置和優(yōu)化功能圖文詳解
這篇文章主要介紹了IDEA的常見(jiàn)的設(shè)置和優(yōu)化功能,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07springboot配置多數(shù)據(jù)源后mybatis攔截器失效的解決
這篇文章主要介紹了springboot配置多數(shù)據(jù)源后mybatis攔截器失效的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09Spring實(shí)戰(zhàn)之SpEl語(yǔ)法實(shí)例詳解
這篇文章主要介紹了Spring實(shí)戰(zhàn)之SpEl語(yǔ)法,結(jié)合實(shí)例形式分析了SpEl創(chuàng)建數(shù)組、集合及解析變量等相關(guān)操作原理與實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-12-12mybatis判斷l(xiāng)ist不為空/大小的問(wèn)題
這篇文章主要介紹了mybatis判斷l(xiāng)ist不為空/大小的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01