国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

Java 8實(shí)現(xiàn)任意參數(shù)的單鏈表

瀏覽:7日期:2022-08-21 18:57:01

本文實(shí)例為大家分享了Java 8實(shí)現(xiàn)任意參數(shù)的單鏈表,供大家參考,具體內(nèi)容如下

1、實(shí)現(xiàn)功能

1)add():鏈表末尾添加元素;2)pop():移除鏈表尾部元素;3)insert():指定索引處添加元素;4)delete():指定索引處刪除元素;5)getSize():獲取鏈表當(dāng)前長度;6)display():展示鏈表當(dāng)前元素。

2、代碼

package DataStructure;/** * @author: Inki * @email: inki.yinji@qq.com * @create: 2020 1024 * @last_modify: 2020 1025 */public class MySingleLinkedList <AnyType> { /** * Only used to store the head node. */ private SingleNode<AnyType> head = new SingleNode(new Object()); /** * The single linked list current size. */ private int size = 0; /** * Add element to the end of the list. * @param: * paraVal: The given value. */ public void add(AnyType paraVal) { insert(size, paraVal); }//Of add /** * Pop the last element. * @return: * The popped value. */ public AnyType pop(){ return delete(size - 1); }//Of pop /** * Insert element at specified index. * @param: * paraIdx: The given index. * paraVal: The given value. */ public void insert(int paraIdx, AnyType paraVal) { if (paraIdx > size) { throw new IndexOutOfBoundsException('The index error.'); }//Of if SingleNode <AnyType> tempNode = head; int i = 0; while (i++ < paraIdx) { tempNode = tempNode.next; }//Of while SingleNode<AnyType> paraNode = new SingleNode <>(paraVal); paraNode.next = tempNode.next; tempNode.next = paraNode; size++; }//of add /** * Delete the element at specified index. * @param: * paraIdx: The given index of element to delete. * @return: * The deleted value. */ public AnyType delete(int paraIdx) { if (size == 0) { throw new RuntimeException('The single linked list is empty.'); }//Of if if (size <= paraIdx) { throw new IndexOutOfBoundsException('The index error.'); }//Of if SingleNode <AnyType> retNode = head; int i = 0; while (i++ < paraIdx) { retNode = retNode.next; }//Of while retNode.next = retNode.next.next; size--; return retNode.val; }//Of delete /** * Get the current size of the single linked list. * @return: * The current size of the single linked list. */ public int getSize() { return size; }//Of getSize /** * Display the single linked list. */ public void display() { if (size == 0) { throw new RuntimeException('The single linked list is empty.'); }//Of if System.out.print('The single linked list is:n['); SingleNode <AnyType> tempNode = head; int i = 0; while (i++ < size - 1) { tempNode = tempNode.next; System.out.printf('%s, ', tempNode.val); }//Of while System.out.printf('%s]n', tempNode.next.val); }//Of display /** * The main function. */ public static void main(String[] args) { MySingleLinkedList <Character> test = new MySingleLinkedList<>(); test.add(’a’); test.add(’b’); test.insert(0, ’c’); test.add(’d’); test.insert(0, ’5’); test.delete(4); test.pop(); test.add(’+’); test.display(); System.out.println(test.getSize()); }//Of main}//Of class MySingleLinkedListclass SingleNode <AnyType>{ /** * The value. */ AnyType val; /** * The next node. */ SingleNode<AnyType> next; /** * The first constructor. * @param * paraVal: The given value. */ SingleNode (AnyType paraVal) { val = paraVal; }//The first constructor}//Of class SingleNode

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 来安县| 大同县| 常德市| 定日县| 郧西县| 南汇区| 宁晋县| 遂平县| 芦山县| 禹州市| 浙江省| 遵化市| 河南省| 安远县| 海门市| 边坝县| 河东区| 荥阳市| 会昌县| 潞城市| 右玉县| 延长县| 金寨县| 得荣县| 富平县| 罗甸县| 东港市| 富阳市| 克东县| 白朗县| 桐乡市| 郑州市| 克东县| 锦屏县| 博白县| 和田县| 宁夏| 曲阜市| 海安县| 武义县| 天水市|