数据结构之线性表:单链表
生活随笔
收集整理的這篇文章主要介紹了
数据结构之线性表:单链表
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
代碼實現(xiàn)
package top.gldwolf.java.datastructure.linkedtable;/*** @author: Gldwolf* @email: ZengqiangZhao@sina.com* @date: 2020/4/15 12:39*//*** 線性表的鏈式存儲結(jié)構(gòu)** @param <T> 存儲的數(shù)據(jù)類型*/ public class LinkedTable<T> {private Node head = new Node();private Node last; // 最后一個節(jié)點private int length = 1; // 初始值為 0,只有一個頭節(jié)點public LinkedTable() {}/*** 添加一個新的數(shù)據(jù)到表尾** @param data 新添加的數(shù)據(jù)* @return 成功返回 true,失敗返回 false*/public boolean add(T data) {Node<T> node = new Node<>(data);// 現(xiàn)在最后一個元素為新添加的元素if (head.next == null) { // 如果頭節(jié)點指針為空,則表示表中無數(shù)據(jù),新添加的數(shù)據(jù)為第一個節(jié)點head.next = node; // 頭節(jié)點指向第一個節(jié)點} else { // 如果原來不是空表的話last.next = node; // 原來的最后一個元素指針指向新添加的元素}last = node; // 最后一個節(jié)點為新添加的數(shù)據(jù)length++;return true;}/*** 獲取指定索引位置的數(shù)據(jù),通過遍歷實現(xiàn)** @param index* @return 該節(jié)點的數(shù)據(jù)* @throws Exception 當(dāng)下標越界時拋出異常*/public T get(int index) throws Exception { // 下標從 1 開始if (index <= 0 || index >= length) { // 如果索引大于或等于表長度: 如果有一個數(shù)據(jù),則表長度為 2,取索引大于或等于 2 的數(shù)據(jù)則拋出異常throw new Exception("下標越界");} else { // 如果索引不越界/*如果索引不越界,則會從頭開始遍歷數(shù)據(jù),每循環(huán)一遍,游標 +1,得到游標所在節(jié)點的數(shù)據(jù),當(dāng)游標達到指定索引時,返回這個數(shù)據(jù),并退出循環(huán)*/Node cursor = head;for (int i = 1; i <= index; i++) { // 遍歷數(shù)據(jù),每執(zhí)行一步獲取一個節(jié)點,當(dāng)個數(shù)達到索引值時,返回這個節(jié)點的數(shù)據(jù)cursor = cursor.next;}return (T) cursor.getData();}}/*** 在指定位置插入數(shù)據(jù)* @param index 數(shù)據(jù)要插入的位置* @param data 要插入的數(shù)據(jù)* @return 成功返回 true,否則返回 false* @throws Exception*/public boolean insert(int index, T data) throws Exception {if (index < 1 || index > length) {throw new Exception("下標越界");} else if (index == length) { // 如果在最末尾插入數(shù)據(jù)Node node = new Node(data);last.next = node;last = node;} else if (index == 1) { // 如果放在第一個節(jié)點Node node = new Node(data);node.next = head.next;head.next = node;} else { // 如果不在最末尾插入數(shù)據(jù)Node node = new Node(data);Node cursor = head;for (int i = 1; i < index; i++) { // 通過 cursor 得到這個索引的前一個數(shù)據(jù)cursor = cursor.next;}node.next = cursor.next;cursor.next = node;}length ++;return true;}public boolean delete(int index) throws Exception {if (index < 1 || index > length) {throw new Exception("下標越界");} else if (index == 1) { // 如果刪除的為第 1 個節(jié)點head.next = head.next.next;} else if (index == length - 1) { // 如果刪除的是最后一個節(jié)點Node cursor = head;for (int i = 1; i < length; i++) { // 找到要刪的節(jié)點的前一個節(jié)點cursor = cursor.next;}cursor.next = null;} else { // 如果刪除其它節(jié)點Node cursor = head;for (int i = 1; i < index; i++) { // 找到要刪的節(jié)點的前一個節(jié)點cursor = cursor.next;}cursor.next = cursor.next.next;}length--;return true;}@Overridepublic String toString() {StringBuffer res = new StringBuffer();Node cursor = head;for (int i = 1; i < length; i++) {cursor = cursor.next;res.append(cursor.getData() + ", ");}return res.toString() + " length: " + (length - 1);} }class Node<N> {protected Node next; // 指向下一個 Node// private Node pre; // 指向上一個 Nodeprivate N data; // 用來存儲數(shù)據(jù)protected Node() {} // 僅用于創(chuàng)建頭節(jié)點public Node(N data) { // 用于添加后續(xù)數(shù)據(jù)節(jié)點this.data = data;}public N getData() {return data;} }測試類
package top.gldwolf.java.datastructure.linkedtable;/*** @author: Gldwolf* @email: ZengqiangZhao@sina.com* @date: 2020/4/15 12:40*/public class LinkedTableDriver {public static void main(String[] args) throws Exception {LinkedTable<String> table = new LinkedTable<>();table.add("2");table.add("3");table.add("4");table.add("5");table.add("6");String s = table.get(5);System.out.println(s);table.insert(1, "1");System.out.println(table.get(1));System.out.println(table.get(5));table.insert(4, "9");System.out.println(table.get(4));System.out.println(table.toString());table.delete(4);System.out.println(table);table.delete(1);System.out.println(table);table.delete(5);System.out.println(table);table.add("6");System.out.println(table);System.out.println(table.get(5));System.out.println(table.get(3));table.delete(5);table.delete(3);System.out.println(table);} }總結(jié)
以上是生活随笔為你收集整理的数据结构之线性表:单链表的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 图解TCP四次握手断开连接
- 下一篇: shutdown()函数:优雅地断开TC