java数据结构与算法:单链表 SinglyLinkedList

单链表 SinglyLinkedList

创建实现类并实现方法

package com.lhs;public class SinglyLinkedList<E> implements List<E>{// 头节点private Node<E> first;// 尾节点private Node<E> last;// 节点数量private int size;public static class Node<E> {E data;Node<E> next;Node(E data, Node<E> next) {this.data = data;this.next = next;}}// 返回节点数量@Overridepublic int size() {return size;}// 判断链表是否为空@Overridepublic boolean isEmpty() {return size == 0;}// 判断链表中是否包含某个元素@Overridepublic boolean contains(Object o) {if(o == null) {for(Node<E> node = first;node != null;node = node.next){if (node.data == null) {return true;}}return false;}else{for(Node<E> node = first;node != null;node = node.next){if(o.equals(node.data)){return true;}}}return  false;}// 向链表中添加元素@Overridepublic boolean add(E e) {Node<E> l = last;Node<E> eNode = new Node<>(e, null);if(l != null){l.next = eNode;}else{first = eNode;}last = eNode;size++;return true;}// 获取链表中指定索引位置的元素@Overridepublic E get(int index) {if(index >= size || index < 0){throw new IndexOutOfBoundsException(index + "is out of bounds");}Node<E> node = first;for (int i = 0; i < index; i++) {node = node.next;}return node.data;}// 设置链表中指定索引位置的元素@Overridepublic E set(int index, E e) {if(index >= size || index < 0){throw new IndexOutOfBoundsException(index + "is out of bounds");}Node<E> node = first;for (int i = 0; i < index; i++) {node = node.next;}E oldVal = node.data;node.data = e;return oldVal;}// 移除链表中指定索引位置的元素@Overridepublic E remove(int index) {if(index >= size || index < 0){throw new IndexOutOfBoundsException(index + "is out of bounds");}Node<E> node = first;Node<E> before = null;E oldVal;if(index == 0){oldVal = first.data;first = first.next;}else{for (int i = 0; i < index; i++) {if(i == index - 1){before = node;}node = node.next;}oldVal = node.data;before.next = node.next;}size--;return oldVal;}// 向链表中添加元素到头部@Overridepublic void addFirst(E e) {Node<E> oldVal = first;Node<E> eNode = new Node<>(e, oldVal);first = eNode;size++;}// 向链表中添加元素到尾部@Overridepublic void addLast(E e) {add(e);}// 从链表中移除头部元素@Overridepublic E removeFirst() {return remove(0);}// 从链表中移除尾部元素@Overridepublic E removeLast() {return remove(size);}
}

测试

package com.lhs;import org.junit.Test;import static junit.framework.TestCase.*;
import static org.junit.Assert.assertThrows;/*** SinglyLinkedList Test* * @since 1.0.0 2020年5月4日* @author <a href="https://waylau.com">Way Lau</a>*/
public class SinglyLinkedListTest {@Testpublic void testSize() {// 实例化SinglyLinkedListList<String> list = new SinglyLinkedList<String>();assertTrue(list.size() == 0);list.add("Java");assertTrue(list.size() == 1);}@Testpublic void testIsEmpty() {// 实例化SinglyLinkedListList<String> list = new SinglyLinkedList<String>();assertTrue(list.isEmpty());list.add("Java");assertFalse(list.isEmpty());}@Testpublic void testContains() {// 实例化SinglyLinkedListList<String> list = new SinglyLinkedList<String>();list.add("Java");list.add("C++");list.add("C");list.add("Python");list.add("TypeScript");// 判断存在assertTrue(list.contains("Java"));// 判断不存在assertFalse(list.contains("Java++"));}@Testpublic void testAdd() {// 实例化SinglyLinkedListList<Integer> list = new SinglyLinkedList<Integer>();list.add(1);list.add(2);list.add(3);list.add(4);list.add(5);assertFalse(list.isEmpty());}@Testpublic void testGet() {// 实例化SinglyLinkedListList<String> list = new SinglyLinkedList<String>();list.add("Java");list.add("C++");list.add("C");// 判断存在assertEquals("C++", list.get(1));// 判断不存在int index = 6;Throwable excpetion = assertThrows(IndexOutOfBoundsException.class, () -> {list.get(index);// 抛异常});assertEquals(index + "is out of bounds",excpetion.getMessage());}@Testpublic void testSet() {// 实例化SinglyLinkedListList<String> list = new SinglyLinkedList<String>();list.add("Java");list.add("C++");list.add("C");// 判断存在assertEquals("C", list.set(2, "Python"));// 判断不存在int index = 6;Throwable excpetion = assertThrows(IndexOutOfBoundsException.class, () -> {list.set(index, "Python");// 抛异常});assertEquals(index + "is out of bounds",excpetion.getMessage());}@Testpublic void testRemove() {// 实例化SinglyLinkedListList<String> list = new SinglyLinkedList<String>();list.add("Java");list.add("C++");list.add("C");// 判断存在assertEquals("C", list.remove(2));assertEquals("Java", list.get(0));assertEquals("C++", list.get(1));// 判断不存在int index = 6;Throwable excpetion = assertThrows(IndexOutOfBoundsException.class, () -> {list.remove(index); // 抛异常});assertEquals(index + "is out of bounds",excpetion.getMessage());}@Testpublic void testAddFirst() {// 实例化SinglyLinkedListList<String> list = new SinglyLinkedList<String>();list.addFirst("Java");list.addFirst("C++");list.addFirst("C");// 判断存在assertEquals("C", list.get(0));assertEquals("C++", list.get(1));assertEquals("Java", list.get(2));}@Testpublic void testAddLast() {// 实例化SinglyLinkedListList<String> list = new SinglyLinkedList<String>();list.addLast("Java");list.addLast("C++");list.addLast("C");// 判断存在assertEquals("Java", list.get(0));assertEquals("C++", list.get(1));assertEquals("C", list.get(2));}@Testpublic void testRemoveFirst() {// 实例化SinglyLinkedListList<String> list = new SinglyLinkedList<String>();list.add("Java");list.add("C++");list.add("C");// 判断存在assertEquals("Java", list.removeFirst());assertEquals("C++", list.removeFirst());assertEquals("C", list.removeFirst());}@Testpublic void testRemoveLast() {// 实例化SinglyLinkedListList<String> list = new SinglyLinkedList<String>();list.add("Java");list.add("C++");list.add("C");// 判断存在assertEquals("C", list.removeLast());assertEquals("C++", list.removeLast());assertEquals("Java", list.removeLast());}
}

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/620193.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

微信小程序开发学习笔记《12》下拉刷新事件

微信小程序开发学习笔记《12》下拉刷新事件 博主正在学习微信小程序开发&#xff0c;希望记录自己学习过程同时与广大网友共同学习讨论。建议仔细阅读官方文档 一、什么是下拉刷新 下拉刷新是移动端的专有名词&#xff0c;指的是通过手指在屏幕上的下拉滑动操作&#xff0c;…

php反序列化漏洞基础

一、序列化 serialize(): 序列化是将对象或类转换为字符串的过程,以便在程序运行过程中对其进行持久化存储或传输的操作。在PHP中,序列化主要用于将类对象或数组转换成字节流的形式,以便于存储在磁盘或传输到其他系统。 通过序列化,可以将对象或类转换成一串字符串,然后可…

linux相关操作

1&#xff1a;掌握虚拟机快照的制作和还原 然后转到就欧克了&#xff0c;相当于游戏存档。 2&#xff1a;linux基础命令 1&#xff1a;掌握linux系统的目录结构 linux没有盘符概念&#xff0c;只有一个根目录/&#xff0c;所有文件都在它之下 路径的描述方式&#xff1a; 在l…

uniapp中uview组件库丰富的Table 表格的使用方法

目录 #平台差异说明 #基本使用 #兼容性 #API #Table Props #Td Props #Th Props 表格组件一般用于展示大量结构化数据的场景 #平台差异说明 AppH5微信小程序支付宝小程序百度小程序头条小程序QQ小程序√√√√√√√ #基本使用 本组件标签类似HTML的table表格&#…

redis被攻击:安全问题

1 必须修改端口号 port 323422 2 必须设置密码&#xff0c;并且有一定的复杂度 requirepass dske#123 3 绑定bind ip bind 127.0.0.1 4 使用无登录权限的用户运行redis 配置文件路径 /www/server/redis/redis.conf 一般都是安装文件里 如果忘记密码和端口号也可以去配置文件…

计算机毕设项目(一)基于flask+mongo+angular实现爬取加密货币信息并使用LSTM模型预测价格的论坛可视化平台

文章目录 加密货币平台项目介绍技术栈1. 用户管理2. 新闻和帖子管理3. 加密货币数据4. 对话获取5. 数据获取 服务端代码完整代码 加密货币平台项目介绍 这个项目是一个基于 Flask 和 MongoDB 的深度学习应用程序&#xff0c;通过爬虫爬取加密货币行情和介绍信息&#xff0c;并…

(Java企业 / 公司项目)分布式事务Seata详解(含Seata+Nacos组合使用)

一. Seata介绍 Seata 是一款开源的分布式事务解决方案&#xff0c;致力于在微服务架构下提供高性能和简单易用的分布式事务服务。在 Seata 开源之前&#xff0c;其内部版本在阿里系内部一直扮演着应用架构层数据一致性的中间件角色&#xff0c;帮助经济体平稳的度过历年的双11&…

【Vue3/Vue2】判断设备是移动端还是pc端跳转不同路由router

Vue3代码 APP文件中写入js代码 1、首先&#xff0c;通过isMobile()函数判断用户的设备类型。该函数使用正则表达式匹配navigator.userAgent字符串&#xff0c;以确定用户是在移动设备上访问网页还是在桌面设备上访问网页 2、然后&#xff0c;在onMounted()钩子函数中&#…

vue3 - 自定义弹框组件

写了一个弹框组件 <template><transition name"modal-fade"><div v-if"showFlag" class"myModal"><div class"content"><div class"topBox"><div class"leftTitle"><spa…

线性代数——行列式按行(列)展开

目录 一、余子式&#xff1a;将行列式某元素所在行和列的元素全去掉 剩余部分所构成的行列式&#xff0c;称为该元素的余子式 二、代数余子式 三、行列式等于它的任一行&#xff08;列&#xff09;的各元素与对应代数余子式乘积之和 四、行列式某行元素&#xff08;列&…

单机物理机部署Datax

一、概述 DataX 是阿里巴巴开源的一个异构数据源离线同步工具&#xff0c;致力于实现包括关系型数据库(MySQL、Oracle等)、HDFS、Hive、ODPS、HBase、FTP等各种异构数据源之间稳定高效的数据同步功能。 源码地址&#xff1a;https://github.com/alibaba/DataX 为了解决异构数据…

什么是云服务器?云服务器的工作原理是介绍

阿里云服务器ECS英文全程Elastic Compute Service&#xff0c;云服务器ECS是一种安全可靠、弹性可伸缩的云计算服务&#xff0c;阿里云提供多种云服务器ECS实例规格&#xff0c;如经济型e实例、通用算力型u1、ECS计算型c7、通用型g7、GPU实例等&#xff0c;阿里云百科aliyunbai…

Linux shell jq工具操作文档(jq --help使用示例)

文章目录 jq工具介绍jq --help解读英文中文 使用示例1. 使用最简单的过滤器。将输入复制到输出&#xff0c;不做任何修改&#xff08;除了格式化&#xff09;2. 使用 -c 选项进行紧凑输出而非美化输出3. 使用 -n 选项以 null 作为单一输入值&#xff08;用于创建新json&#xf…

STL——stack容器和queue容器详解

目录 &#x1f4a1;stack &#x1f4a1;基本概念 常用接口 &#x1f4a1;queue &#x1f4a1;基本概念 &#x1f4a1;常用接口 &#x1f4a1;stack &#x1f4a1;基本概念 栈&#xff08;stack&#xff09;&#xff1a;一种特殊的线性表&#xff0c;其只允许在固定的一端…

OpenGL 网格拾取坐标(Qt)

文章目录 一、简介二、代码实现三、实现效果参考资料一、简介 有时候我们希望通过鼠标来拾取某个网格中的坐标,这就涉及到一个很有趣的场景:光线投射,也就是求取一条射线与网格的交点,这里如果我们采用普通遍历网格中的每个面片的方式,当网格的面片数据量很大时计算效率就…

pyside6 捕捉主窗口关闭后,进行释放相关的资源

import sys from PySide6 import QtGui from PySide6.QtWidgets import QWidget,QApplication,QMessageBoxclass Message(QWidget):def __init__(self):# 如果希望窗口内嵌于其他部件&#xff0c;可添加parent参数super(Message, self).__init__()# 调用初始化方法self.initUI(…

Python基本语法与变量的相关介绍

python基本语法与变量 python语句的缩进 Python代码块使用缩进对齐表示代码逻辑&#xff0c;Python每段代码块缩进的空白数量可以任意&#xff0c;但要确保同段代码块语句必须包含相同的缩进空白数量。建议在代码块的每个缩进层次使用单个制表符或两个空格或四个空格 , 切记不…

flutter使用get库管理路由,并设页面跳转动画和常见动画

get库还是非常强大的一个仓库&#xff0c;里面包含了非常常用的一些方法&#xff0c;比如路由管理&#xff0c;这是最常见和最常用的一个功能了&#xff0c;我们可以先配置一个路由对象&#xff0c;然后在里面配置路由列表&#xff0c;并且设置路由跳转方式。 第一种方式&…

教师资格证照片分辨率怎么调?教师资格证上传照片要求

最近教师资格证考试开始报名了&#xff0c;在报名之前需要准备好一些必备的材料&#xff0c;比如证件照片&#xff0c;报名考试平台一般都会对上传的证件照有具体的要求&#xff0c;比如考生本人近6个月以内的免冠正面证件照&#xff1b;照片格式及大小&#xff1a;JPG/JPEG格式…

Springboot读取配置文件

多种配置文件格式 springboot项目中不同配置文件的优先加载顺序 为&#xff1a;properties> yml >yaml>自定义核心类配置 自定义配置文件的加载 一般系统会加载默认的application.properties或者application.yml,但如果使用自定义配置文件&#xff0c;可使用下面方…