因为在学习数据结构,准备把java的集合框架底层源码,好好的过一遍,所以先按照自己的想法把单链表的类给写出来了;

写该类的目的:

1.练习递归

2.为深入理解java集合框架底层源码打好基础

学习的视频看的慕课网liuyubobo老师的课程:Java玩转数据结构 从入门到进阶

废话不多说,以下为源代码:

 public class LinkedList<E extends Comparable<E>> {
//链表节点内部类
private class Node{
E e;
Node next;
public Node(E _e,Node _next){
this.e = _e;
this.next = _next;
}
public Node(E _e){
this(_e, null);
}
public Node(){
this(null, null);
}
}
//头节点
private Node head;
//链表实际存储大小
private int size;
//无参构造函数
public LinekedList(){
this.head = null;
this.size = 0;
}
//判断链表是否为空
public boolean isEmpty(){
return this.size == 0;
}
public int size(){
return this.size;
}
//在链表头添加节点
public void addFirst(E e){
add(0, e);
}
//在链表尾添加节点
public void addLast(E e){
add(size, e);
}
//在链表指定位置添加节点
public void add(int index,E e){
if (index < 0 || index > size) {
throw new IllegalArgumentException("Index is error.");
}
head = add(index, e, head);
}
//通过递归实现链表的添加
private Node add(int index,E e,Node node){
if (index == 0 && node == null) {
this.size++;
return new Node(e);
}
if (index == 0) {
this.size++;
Node newNode = new Node(e);
//newNode.next = node.next;
newNode.next = node;
//node.next = newNode;
return newNode;
}
node.next = add(index - 1, e, node.next);
return node;
}
/**
* 用于根据索引查找对应的节点,
* @param index
* @param node
* @return 相应的节点对象
*/
private Node getElementByIndex(int index,Node node){
if (index == 0 && node != null) {
return node;
}
Node ret = getElementByIndex(index - 1, node.next);
return ret;
}
//删除链表头节点
public E removeFirst(){
return remove(0);
}
//删除链表尾结点
public E removeLast(){
return remove(size - 1);
}
//删除指定位置节点,并返回删除节点值
public E remove(int index){
if (isEmpty()) {
throw new IllegalArgumentException("List is empty.");
}
if (index < 0 || index >= size) {
throw new IllegalArgumentException("Index is error.");
}
Node ret = getElementByIndex(index, head);
head = remove(index, head);
return ret.e;
}
//递归删除节点值
private Node remove(int index,Node node){
if (node == null) {
return node;
}
if (index == 0) {
Node delNode = node;
node = delNode.next;
delNode.next = null;
this.size--;
return node;
}
node.next = remove(index - 1, node.next);
return node;
}
//修改指定位置节点的值,通过递归实现
public void set(int index,E e){
if (isEmpty()) {
throw new IllegalArgumentException("List is empty.");
}
if (index < 0 || index >= size) {
throw new IllegalArgumentException("Index is error.");
}
set(index, e, head);
}
private void set(int index,E e,Node node){
if (node == null) {
return;
}
if (index == 0) {
node.e = e;
}
set(index - 1, e, node.next);
return;
}
//获取链表头节点的值
public E getFirst(){
return get(0);
}
//获取链表尾结点的值
public E getLast(){
return get(size - 1);
}
//获取链表制定位置的值,通过递归实现
public E get(int index){
if (isEmpty()) {
throw new IllegalArgumentException("List is empty.");
}
if (index < 0 || index >= size) {
throw new IllegalArgumentException("Index is error.");
}
return get(index, head).e;
}
private Node get(int index,Node node){
if (node == null) {
return node;
}
if (index == 0) {
return node;
}
Node ret = get(index - 1, node.next);
return ret;
}
//查询链表中的节点是否包含值e
public boolean contains(E e){
return contains(e, head);
}
private boolean contains(E e,Node node){
if (node == null) {
return false;
}
if (node.e == e) {
return true;
}
return contains(e, node.next);
}
//重写Object类的toString方法:也是通过链表天然的递归性,来访问
//链表的每一个节点
@Override
public String toString() {
StringBuilder res = new StringBuilder();
res.append("Font -> ");
linkToString(head, res);
return res.toString();
}
private void linkToString(Node node,StringBuilder res){
if (node == null) {
res.append("NULL");
return;
}
res.append(node.e + " -> ");
linkToString(node.next, res);
return;
} /*public static void main(String[] args) {
LinekedList<Integer> ll = new LinekedList<>();
for (int i = 0; i < 13; i++) {
ll.addLast(i);
System.out.println(ll.toString());
}
System.out.println("------------------");
for (int i = 0; i < 11; i++) {
Integer ii = ll.removeFirst();
System.out.println(ii);
System.out.println(ll.toString());
} }*/
}

有一些方法测了,但是有些方法没测,有需要可以自行参考(不保证准确啊...)

该类主要包含链表的增删改查,里面加了索引主要是为了好理解(注:链表是没有索引的).

因为,才刚开始熟悉递归以及数据结构,所以需要各位大佬的多多指正

有建议的可以在评论中指出.

关于单链表的增删改查方法的递归实现(JAVA语言实现)的更多相关文章

  1. 史上最全单链表的增删改查反转等操作汇总以及5种排序算法(C语言)

    目录 1.准备工作 2.创建链表 3.打印链表 4.在元素后面插入元素 5.在元素前面增加元素 6.删除链表元素,要注意删除链表尾还是链表头 7.根据传入的数值查询链表 8.修改链表元素 9.求链表长 ...

  2. python全栈开发day61-django简单的出版社网站展示,添加,删除,编辑(单表的增删改查)

    day61 django内容回顾: 1. 下载: pip install django==1.11.14 pip install -i 源 django==1.11.14 pycharm 2. 创建项 ...

  3. hibernate对单表的增删改查

    ORM: 对象关系映射(英语:Object Relational Mapping,简称ORM,或O/RM,或O/R mapping) 实现对单表的增删改查 向区域表中增加数据: 第一步: 新建一个Da ...

  4. Django学习笔记(10)——Book单表的增删改查页面

    一,项目题目:Book单表的增删改查页面 该项目主要练习使用Django开发一个Book单表的增删改查页面,通过这个项目巩固自己这段时间学习Django知识. 二,项目需求: 开发一个简单的Book增 ...

  5. 通用mapper的增删改查方法 留存 备忘

    Mybatis通用Mapper介绍与使用   前言 使用Mybatis的开发者,大多数都会遇到一个问题,就是要写大量的SQL在xml文件中,除了特殊的业务逻辑SQL之外,还有大量结构类似的增删改查SQ ...

  6. java实现单链表的增删改以及排序

    使用java代码模拟单链表的增删改以及排序功能 代码如下: package com.seizedays.linked_list; public class SingleLinkedListDemo { ...

  7. Mybatis(一)实现单表的增删改查

    1.1 什么是Mybatis MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并 ...

  8. Spring Data CrudRepository增删改查方法(八)

    CrudRepository   的主要方法 long count(); boolean exists(Integer arg0); <S extends StudentPO> S sav ...

  9. Django REST framework 五种增删改查方法

    Django-DRF-视图的演变   版本一(基于类视图APIView类) views.py: APIView是继承的Django View视图的. 1 from .serializers impor ...

随机推荐

  1. 前端教程(1)http协议的深刻理解

    一 HTTP协议简介 作为学习前端开发的开始,我们必须搞明白以下几件事 1.什么是互联网      互联网=物理连接介质+互联网协议     2.互联网建立的目的? 数据传输打破地域限制,否则的话,我 ...

  2. TensorFlow实现分布式计算

    摘要: 1.代码例子 内容: 1.代码例子 <TensorFlow实战>实现CNN处理CIFAR10数据,并模拟单机多个CPU同步数据并行计算 <TensorFlow实战>实现 ...

  3. mybatis-generator自動逆向生成文件

    首先在maven里面添加插件 <plugins> <plugin> <groupId>org.mybatis.generator</groupId> & ...

  4. Scala 编码习惯

    1. 不用var.var是可以被不断修改的,而val是不能被修改的.使用val而不是var能让你的程序更强壮,bug更少,更好调试,更容易测试,在并发条件下,更容易调优而获得更好的性能.数学证明我们不 ...

  5. 斯坦福CS224n课程作业

    斯坦福CS224n作业一 softmax 作业要求如下: 解析:题目要求我们证明\(softmax\)函数具有常数不变性. 解答:对于\(x+c\)的每一维来说,有如下等式成立: \[softmax( ...

  6. 工程文件csproj使用编译条件指定属性

    csproj工程文件中有很多xml格式的属性,比如PropertyGroup.ItemGroup,某些属性操作默认是全部的或者是当前编译条件的而已,当我们想指定某些属性只在某个编译条件下发生时就可以通 ...

  7. netty之NioEventLoopGroup源码分析二

    大家好,今天我准备死磕NioEventLoopGroup的源码,首先讲下概念,NioEventLoopGroup 它是一个线程池,存放NioEventLoop,一个数组,今天打算先看下这行代码的初始化 ...

  8. How to build ffmpeg with hardware accelerated codecs for Android x86

    In order to build a complete ffmpeg with hardware acceleration for Intel platform (XXX lake + Atom), ...

  9. sql server replace的替换字符,replace的使用

    sql server replace的替换字符,replace的使用 select REPLACE(name,'张','') * from entity_5c7a578c05c7042958d9148 ...

  10. Redis内存模型(2):存储细节

    1. 概述 先看一下执行set hellow world时,所涉及的数据模型: (1)dictEntry:Redis是Key-Value数据库,因此对每个键值对都会有一个dictEntry,里面存储了 ...