Java 8實(shí)現(xiàn)任意參數(shù)的單鏈表
本文實(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)前長(zhǎ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 MySingleLinkedList
class 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
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Java實(shí)現(xiàn)單鏈表反轉(zhuǎn)的多種方法總結(jié)
- Java如何實(shí)現(xiàn)單鏈表的增刪改查
- Java單鏈表反轉(zhuǎn)圖文教程
- java實(shí)現(xiàn)簡(jiǎn)單單鏈表
- 用JAVA實(shí)現(xiàn)單鏈表,檢測(cè)字符串是否是回文串
- Java單鏈表的簡(jiǎn)單操作實(shí)現(xiàn)教程
- Java 單鏈表數(shù)據(jù)結(jié)構(gòu)的增刪改查教程
- java實(shí)現(xiàn)單鏈表增刪改查的實(shí)例代碼詳解
- Java數(shù)據(jù)結(jié)構(gòu)之簡(jiǎn)單鏈表的定義與實(shí)現(xiàn)方法示例
- java 數(shù)據(jù)結(jié)構(gòu)單鏈表的實(shí)現(xiàn)
- Java實(shí)現(xiàn)單鏈表翻轉(zhuǎn)實(shí)例代碼
- java 實(shí)現(xiàn)單鏈表逆轉(zhuǎn)詳解及實(shí)例代碼
- Java實(shí)現(xiàn)單鏈表的各種操作
- Java單鏈表的實(shí)現(xiàn)代碼
- Java數(shù)據(jù)結(jié)構(gòu)之單鏈表詳解
相關(guān)文章
Java利用Jackson序列化實(shí)現(xiàn)數(shù)據(jù)脫敏
這篇文章主要介紹了利用Jackson序列化實(shí)現(xiàn)數(shù)據(jù)脫敏,首先在需要進(jìn)行脫敏的VO字段上面標(biāo)注相關(guān)脫敏注解,具體實(shí)例代碼文中給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-10-10
Java @Transactional與synchronized使用的問(wèn)題
這篇文章主要介紹了Java @Transactional與synchronized使用的問(wèn)題,了解內(nèi)部原理是為了幫助我們做擴(kuò)展,同時(shí)也是驗(yàn)證了一個(gè)人的學(xué)習(xí)能力,如果你想讓自己的職業(yè)道路更上一層樓,這些底層的東西你是必須要會(huì)的2023-01-01
工作中禁止使用Executors快捷創(chuàng)建線程池原理詳解
這篇文章主要為大家介紹了工作中禁止使用Executors快捷創(chuàng)建線程池原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
Java 實(shí)現(xiàn)加密數(shù)據(jù)庫(kù)連接的步驟
這篇文章主要介紹了Java 實(shí)現(xiàn)加密數(shù)據(jù)庫(kù)連接的步驟,幫助大家更好的理解和使用Java處理數(shù)據(jù)庫(kù),感興趣的朋友可以了解下2020-11-11
利用maven命令指定配置文件打包springboot項(xiàng)目
這篇文章主要介紹了利用maven命令指定配置文件打包springboot項(xiàng)目,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
idea一招搞定同步所有配置(導(dǎo)入或?qū)С鏊信渲?
使用intellij idea很長(zhǎng)一段時(shí)間,軟件相關(guān)的配置也都按照自己習(xí)慣的設(shè)置好,如果需要重裝軟件,還得需要重新設(shè)置,本文就詳細(xì)的介紹了idea 同步所有配置,感興趣的可以了解一下2021-07-07
idea中springboot整合mybatis找不到mapper接口的原因分析
這篇文章主要介紹了idea中springboot整合mybatis找不到mapper接口的原因分析及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01

