java實現(xiàn)簡單單鏈表
本文實例為大家分享了java實現(xiàn)簡單單鏈表的具體代碼,供大家參考,具體內(nèi)容如下
一、定義:單鏈表是一種鏈式存取的數(shù)據(jù)結(jié)構(gòu),用一組地址任意的存儲單元存放線性表中的數(shù)據(jù)元素。鏈表中的數(shù)據(jù)是以結(jié)點來表示的,每個結(jié)點的構(gòu)成:元素(數(shù)據(jù)元素的映象) + 指針(相當于JAVA中的引用,指示后繼元素存儲位置,),元素就是存儲數(shù)據(jù)的存儲單元,指針就是連接每個結(jié)點的地址數(shù)據(jù)。
二、結(jié)構(gòu):如圖所示,data就是當前節(jié)點的數(shù)據(jù),next是指針,指針存放的是內(nèi)存地址,是當前結(jié)點的下一結(jié)點內(nèi)存地址,順著這個地址就能找到下一個結(jié)點。
三、代碼實現(xiàn):package com.example.demo.linkedlist; /** * 結(jié)點 * 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; } /** * 獲取鏈表的長度 * @return 鏈表長度 */ public Integer length() { int length = 0; Node temp = head; while (temp != null) { temp = temp.next; length ++; } return length; } /** * 從指定下標處添加 * @param index 指定下標 * @param data 待添加的數(shù)據(jù) */ public void addByIndex(int index, Integer data) { if (index < 0 || index > length()) { System.out.println('插入下標不合規(guī),請檢查!'); 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; } /** * 指定下標刪除 * @param index 指定下標 */ public void deleteByIndex(int index) { if (index < 0 || index > length()) { System.out.println('刪除下標不合規(guī),請檢查!'); 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; } /** * 通過下標獲取結(jié)點 * @param index 下標 * @return 結(jié)點 */ public Node getByIndex(Integer index) { if (index < 0 || index > length() - 1) { System.out.println('不存在此下標結(jié)點'); } 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; } } /** * 打印某個節(jié)點之后的所有值 * @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é)點 */ 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; } /** * 測試 * @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('獲取下標為1的結(jié)點值: ' + byIndex1.value); linkNode.addByIndex(2, 8); System.out.println('下標2添加后打印單鏈表: '); linkNode.printLink(); linkNode.addByIndex(0, 11); System.out.println('下標0添加后打印單鏈表: '); linkNode.printLink(); linkNode.deleteByIndex(0); System.out.println('下標0刪除后打印單鏈表: '); linkNode.printLink(); Node node = linkNode.reverseLink(linkNode.head); System.out.println('反轉(zhuǎn)后打印單鏈表: '); printAfterNode(node); } }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. python如何實現(xiàn)word批量轉(zhuǎn)HTML2. python excel和yaml文件的讀取封裝3. python3實現(xiàn)往mysql中插入datetime類型的數(shù)據(jù)4. 詳解Java實現(xiàn)拓撲排序算法5. moment轉(zhuǎn)化時間戳出現(xiàn)Invalid Date的問題及解決6. python爬蟲實戰(zhàn)之制作屬于自己的一個IP代理模塊7. php實現(xiàn)當前用戶在線人數(shù)8. 關(guān)于 Android WebView 的內(nèi)存泄露問題9. java虛擬機詳述-第三章(二)10. Python中內(nèi)建模塊collections如何使用
