Java 8实现任意参数的单链表
#代码知识 发布时间: 2026-01-12
本文实例为大家分享了Java 8实现任意参数的单链表,供大家参考,具体内容如下

1、实现功能
1)add():链表末尾添加元素;
2)pop():移除链表尾部元素;
3)insert():指定索引处添加元素;
4)delete():指定索引处删除元素;
5)getSize():获取链表当前长度;
6)display():展示链表当前元素。
2、代码
package DataStructure;
/**
* @author: Inki
* @email: inki.yinji@qq.com
* @create: 2025 1024
* @last_modify: 2025 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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
代码知识SEO上一篇 : SpringBoot配置MongoDB多数据源的方法步骤
下一篇 : 谈谈MySQL中的隐式转换
-
SEO外包最佳选择国内专业的白帽SEO机构,熟知搜索算法,各行业企业站优化策略!
SEO公司
-
可定制SEO优化套餐基于整站优化与品牌搜索展现,定制个性化营销推广方案!
SEO套餐
-
SEO入门教程多年积累SEO实战案例,从新手到专家,从入门到精通,海量的SEO学习资料!
SEO教程
-
SEO项目资源高质量SEO项目资源,稀缺性外链,优质文案代写,老域名提权,云主机相关配置折扣!
SEO资源
-
SEO快速建站快速搭建符合搜索引擎友好的企业网站,协助备案,域名选择,服务器配置等相关服务!
SEO建站
-
快速搜索引擎优化建议没有任何SEO机构,可以承诺搜索引擎排名的具体位置,如果有,那么请您多注意!专业的SEO机构,一般情况下只能确保目标关键词进入到首页或者前几页,如果您有相关问题,欢迎咨询!