java實現簡單單鏈表
本文實例為大家分享了java實現簡單單鏈表的具體代碼,供大家參考,具體內容如下
一、定義:單鏈表是一種鏈式存取的數據結構,用一組地址任意的存儲單元存放線性表中的數據元素。鏈表中的數據是以結點來表示的,每個結點的構成:元素(數據元素的映象) + 指針(相當于JAVA中的引用,指示后繼元素存儲位置,),元素就是存儲數據的存儲單元,指針就是連接每個結點的地址數據。
二、結構:如圖所示,data就是當前節點的數據,next是指針,指針存放的是內存地址,是當前結點的下一結點內存地址,順著這個地址就能找到下一個結點。
三、代碼實現:package com.example.demo.linkedlist; /** * 結點 * 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 待添加數據 */ public void addHead(Integer data) { Node node = new Node(data); node.next = head; head = node; } /** * 從尾部添加 * @param data 待添加數據 */ 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 待添加的數據 */ public void addByIndex(int index, Integer data) { if (index < 0 || index > length()) { System.out.println('插入下標不合規,請檢查!'); 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('刪除下標不合規,請檢查!'); 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; } /** * 通過下標獲取結點 * @param index 下標 * @return 結點 */ public Node getByIndex(Integer index) { if (index < 0 || index > length() - 1) { System.out.println('不存在此下標結點'); } 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; } } /** * 打印某個節點之后的所有值 * @param node */ public static void printAfterNode(Node node) { while (node != null) { System.out.println(node.value); node = node.next; } } /** * 清除單鏈表 */ public void clearLink() { head = null; } /** * 單鏈表反轉 * @param head 頭節點 */ 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的結點值: ' + 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('反轉后打印單鏈表: '); printAfterNode(node); } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章:
1. MyBatis JdbcType 與Oracle、MySql數據類型對應關系說明2. jsp網頁實現貪吃蛇小游戲3. ASP中if語句、select 、while循環的使用方法4. 存儲于xml中需要的HTML轉義代碼5. phpstudy apache開啟ssi使用詳解6. .NET SkiaSharp 生成二維碼驗證碼及指定區域截取方法實現7. IntelliJ IDEA導入項目的方法8. ASP中實現字符部位類似.NET里String對象的PadLeft和PadRight函數9. CentOS郵件服務器搭建系列—— POP / IMAP 服務器的構建( Dovecot )10. django創建css文件夾的具體方法
