自己实现数据结构系列二---LinkedList
一.先上代码:
1.方式一:
public class LinkedList<E> {
//节点,用来存放数据:数据+下一个元素的引用
private class Node{
private E e;
private 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);
}
public String toString(){
return e.toString();
}
}
private Node head;
private int size;
/**
* 构造方法
*/
public LinkedList(){
head = null;
size = 0;
}
/**
* 获取链表中元素的个数
* @return
*/
public int getSize(){
return size;
}
/**
* 判断链表是否为空
* @return
*/
public boolean isEmpty(){
return size == 0;
}
/**
* 链表头添加新元素
*/
public void addFirst(E e){
Node node = new Node();
node.next = head;
head = node;
size ++;
}
/**
* 链表中间添加元素
* @param index
* @param e
*/
public void add(int index,E e){
if (index < 0 || index > size){
throw new IllegalArgumentException("Add Failed");
}
if (index == 0){
addFirst(e);
}else {
Node prev = head;
for (int i = 0 ; i < index-1; i++){
prev = prev.next;
}
Node node = new Node(e);
node.next = new Node(e);
prev.next = node;
}
size++;
}
/**
* 链表末尾添加元素
* @param e
*/
public void addList(E e){
add(size,e);
}
}
2.方式二:
public class LinkedListPlus<E> {
//节点,用来存放数据:数据+下一个元素的引用
private class Node{
private E e;
private 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);
}
public String toString(){
return e.toString();
}
}
private Node dummyHead;//虚拟头节点
private int size;
public LinkedListPlus(){
dummyHead = new Node(null,null);
size = 0;
}
public int getSize(){
return size;
}
public boolean isEmpty(){
return size == 0;
}
public void add(int index,E e){
if (index < 0 || index > size){
throw new IllegalArgumentException("Add Failed");
}
Node prev = dummyHead;
for (int i = 0; i < index; i++) {
prev = prev.next;
}
Node node = new Node(e);
node.next = prev.next;
prev.next = node;
size++;
}
public void addFirst(E e){
add(0,e);
}
public void addLast(E e){
add(size,e);
}
public E get(int index){
if (index < 0 || index > size){
throw new IllegalArgumentException("Add Failed");
}
Node cur = dummyHead.next;
for (int i = 0; i <size ; i++) {
cur = cur.next;
}
return cur.e;
}
public E getFirst(){
return get(0);
}
public E getLast(){
return get(size-1);
}
public void set(int index,E e){
if (index < 0 || index > size){
throw new IllegalArgumentException("Add Failed");
}
Node cur = dummyHead.next;
for (int i = 0; i <size ; i++) {
cur = cur.next;
}
cur.e = e;
}
public boolean contains(E e){
Node cur = dummyHead.next;
while (cur != null){
if (cur.e.equals(e)){
return true;
}
cur = cur.next;
}
return false;
}
public E remove(int index){
if (index < 0 || index > size){
throw new IllegalArgumentException("Add Failed");
}
Node prev = dummyHead;
for (int i = 0; i <index ; i++) {
prev = prev.next;
}
Node retNode = prev.next;
prev.next = retNode.next;
retNode.next = null;
size--;
return retNode.e;
}
public E removeFirst(){
return remove(0);
}
public E removeLast(){
return remove(size-1);
}
@Override
public String toString() {
StringBuilder res = new StringBuilder();
Node cur = dummyHead.next;
while (cur != null){
res.append(cur+"->");
cur = cur.next;
}
res.append("null");
retu
自己实现数据结构系列二---LinkedList的更多相关文章
- D&F学数据结构系列——二叉堆
二叉堆(binary heap) 二叉堆数据结构是一种数组对象,它可以被视为一棵完全二叉树.同二叉查找树一样,堆也有两个性质,即结构性和堆序性.对于数组中任意位置i上的元素,其左儿子在位置2i上,右儿 ...
- 【JavaScript数据结构系列】05-链表LinkedList
[JavaScript数据结构系列]05-链表LinkedList 码路工人 CoderMonkey 转载请注明作者与出处 ## 1. 认识链表结构(单向链表) 链表也是线性结构, 节点相连构成链表 ...
- 数据结构之链表(LinkedList)(二)
数据结构之链表(LinkedList)(一) 双链表 上一篇讲述了单链表是通过next 指向下一个节点,那么双链表就是指不止可以顺序指向下一个节点,还可以通过prior域逆序指向上一个节点 示意图: ...
- Redis系列(二):Redis的数据类型及命令操作
原文链接(转载请注明出处):Redis系列(二):Redis的数据类型及命令操作 Redis 中常用命令 Redis 官方的文档是英文版的,当然网上也有大量的中文翻译版,例如:Redis 命令参考.这 ...
- 【JavaScript数据结构系列】00-开篇
[JavaScript数据结构系列]00-开篇 码路工人 CoderMonkey 转载请注明作者与出处 ## 0. 开篇[JavaScript数据结构与算法] 大的计划,写以下两部分: 1[JavaS ...
- Java 集合系列 04 LinkedList详细介绍(源码解析)和使用示例
java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java ...
- JAVA数据结构系列 栈
java数据结构系列之栈 手写栈 1.利用链表做出栈,因为栈的特殊,插入删除操作都是在栈顶进行,链表不用担心栈的长度,所以链表再合适不过了,非常好用,不过它在插入和删除元素的时候,速度比数组栈慢,因为 ...
- SQL Server 2008空间数据应用系列二:空间索引(Spatial Index)基础
原文:SQL Server 2008空间数据应用系列二:空间索引(Spatial Index)基础 在前一篇博文中我们学习到了一些关于地理信息的基础知识,也学习了空间参照系统,既地球椭球体.基准.本初 ...
- ETL利器Kettle实战应用解析系列二
本系列文章主要索引如下: 一.ETL利器Kettle实战应用解析系列一[Kettle使用介绍] 二.ETL利器Kettle实战应用解析系列二 [应用场景和实战DEMO下载] 三.ETL利器Kettle ...
随机推荐
- ]remove-duplicates-from-sorted-list-ii (删除)
题意略: 思路都在注解里: #include<iostream> #include<cstdio> using namespace std; struct ListNode { ...
- vue - 状态管理器 Vuex
状态管理 vuex是一个专门为vue.js设计的集中式状态管理架构.状态?我把它理解为在data中的属性需要共享给其他vue组件使用的部分,就叫做状态.简单的说就是data中需要共用的属性.
- CSS三栏布局
一.绝对定位 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> < ...
- go标准库的学习-crypto/sha1
参考:https://studygolang.com/pkgdoc 导入方式: import "crypto/sha1" sha1包实现了SHA1哈希算法,参见RFC 3174. ...
- 'style-loader', 'css-loader'使用
'style-loader', 'css-loader'使用 1.安装 npm install style-loader css-loader --save-dev 2.配置 webpack.conf ...
- 浅淡个人学习嵌入式Linux过程
我专业是电子信息工程,在初入大学的时候,我们的班主任便要我们多多去了解一些关于电子方面的知识.后来我了解到了嵌入式,继而了解到了嵌入式Linux.其实我们学习linux差不多就学习linux内核,但是 ...
- Lua 中的条件表达式
下面这代码段看上去很熟悉,就是C#里面的条件表达式,很多其它语言也都有这么一个条件表达式. ; ; string c = "c"; string d = "d" ...
- Idea Live Template代码片段总结
目录 Idea Live Template总结 一.演示 二.详细介绍 2.1 类型 2.2设置(win默认快捷键win+alt+s) 2.3 快捷键 2.4 实战 Idea Live Templat ...
- POJ3292&&2115
这两道题还是比较简单的,没有什么难度 不过归在数论这个专题里我还是比较认同的,多少有些关系 3292 题目大意:给你一个范围n,让你求出这个范围内所有形式类似\(4k+1(k为正整数)\)的数中的H- ...
- 给echarts加个“全屏展示”
echarts的工具箱并没有提供放大/全屏的功能, 查找文档发现可自定义工具https://www.echartsjs.com/option.html#toolbox.feature show代码 t ...