LinkedList源码实现:

public class LinkedList<E> {

    private class Node{
public E e;
public 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);
} @Override
public String toString(){
return e.toString();
}
} private Node dummyHead;
private int size; public LinkedList(){
dummyHead = new Node();
size = 0;
} // 获取链表中的元素个数
public int getSize(){
return size;
} // 返回链表是否为空
public boolean isEmpty(){
return size == 0;
} // 在链表的index(0-based)位置添加新的元素e
// 在链表中不是一个常用的操作,练习用:)
public void add(int index, E e){
if(index < 0 || index > size)
throw new IllegalArgumentException("Add failed. Illegal index.");
Node prev = dummyHead;
for(int i = 0 ; i < index ; i ++)
prev = prev.next;
prev.next = new Node(e, prev.next);
size ++;
} // 在链表头添加新的元素e
public void addFirst(E e){
add(0, e);
} // 在链表末尾添加新的元素e
public void addLast(E e){
add(size, e);
} // 获得链表的第index(0-based)个位置的元素
// 在链表中不是一个常用的操作,练习用:)
public E get(int index){
if(index < 0 || index >= size)
throw new IllegalArgumentException("Get failed. Illegal index.");
Node cur = dummyHead.next;
for(int i = 0 ; i < index ; i ++)
cur = cur.next;
return cur.e;
} // 获得链表的第一个元素
public E getFirst(){
return get(0);
} // 获得链表的最后一个元素
public E getLast(){
return get(size - 1);
} // 修改链表的第index(0-based)个位置的元素为e
// 在链表中不是一个常用的操作,练习用:)
public void set(int index, E e){
if(index < 0 || index >= size)
throw new IllegalArgumentException("Set failed. Illegal index.");
Node cur = dummyHead.next;
for(int i = 0 ; i < index ; i ++)
cur = cur.next;
cur.e = 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;
} // 从链表中删除index(0-based)位置的元素, 返回删除的元素
// 在链表中不是一个常用的操作,练习用:)
public E remove(int index){
if(index < 0 || index >= size)
throw new IllegalArgumentException("Remove failed. Index is illegal.");
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);
} // 从链表中删除元素e
public void removeElement(E e){
Node prev = dummyHead;
while(prev.next != null){
if(prev.next.e.equals(e))
break;
prev = prev.next;
}
if(prev.next != null){
Node delNode = prev.next;
prev.next = delNode.next;
delNode.next = null;
size --;
}
} @Override
public String toString(){
StringBuilder res = new StringBuilder();
Node cur = dummyHead.next;
while(cur != null){
res.append(cur + "->");
cur = cur.next;
}
res.append("NULL");
return res.toString();
}
}

LinkedList实现的更多相关文章

  1. To Java程序员:切勿用普通for循环遍历LinkedList

    ArrayList与LinkedList的普通for循环遍历 对于大部分Java程序员朋友们来说,可能平时使用得最多的List就是ArrayList,对于ArrayList的遍历,一般用如下写法: p ...

  2. 计算机程序的思维逻辑 (39) - 剖析LinkedList

    上节我们介绍了ArrayList,ArrayList随机访问效率很高,但插入和删除性能比较低,我们提到了同样实现了List接口的LinkedList,它的特点与ArrayList几乎正好相反,本节我们 ...

  3. 深入理解java中的ArrayList和LinkedList

    杂谈最基本数据结构--"线性表": 表结构是一种最基本的数据结构,最常见的实现是数组,几乎在每个程序每一种开发语言中都提供了数组这个顺序存储的线性表结构实现. 什么是线性表? 由0 ...

  4. 用JavaScript来实现链表LinkedList

    本文版权归博客园和作者本人共同所有,转载和爬虫请注明原文地址. 写在前面 好多做web开发的朋友,在学习数据结构和算法时可能比较讨厌C和C++,上学的时候写过的也忘得差不多了,更别提没写过的了.但幸运 ...

  5. ArrayList LinkedList源码解析

    在java中,集合这一数据结构应用广泛,应用最多的莫过于List接口下面的ArrayList和LinkedList; 我们先说List, public interface List<E> ...

  6. ArrayList、Vector、LinkedList的区别联系?

    1.ArrayList.Vector.LinkedList类都是java.util包中,均为可伸缩数组. 2.ArrayList和Vector底层都是数组实现的,所以,索引数据快,删除.插入数据慢. ...

  7. LinkedList<E>源码分析

    LinkedList的数据结构就是双向链表,如下所示: private static class Node<E> { E item;//数据元素 Node<E> next;// ...

  8. Java集合之LinkedList

    一.LinkedList概述 1.初识LinkedList 上一篇中讲解了ArrayList,本篇文章讲解一下LinkedList的实现. LinkedList是基于链表实现的,所以先讲解一下什么是链 ...

  9. 集合 LinkedList、ArrayList、Set、Treeset

    LinkedList中特有的方法: 1:方法介绍 addFirst(E e) addLast(E e) getFirst() getLast() removeFirst() removeLast() ...

  10. ArrayList、Vector、LinkedList源码

    List接口的一些列实现中,最常用最重要的就是这三个:ArrayList.Vector.LinkedList.这里我就基于JDK1.7来看一下源码. public class ArrayList< ...

随机推荐

  1. knative入门指南

    尽管Knative自2018年以来一直由社区维护,但最近一直有关于该项目的传言,因为谷歌最近将Knative提交给了云原生计算基金会(CNCF),作为一个孵化项目考虑. 太酷了!但Knative到底是 ...

  2. 017tcpflow的简单用法

    tcpflow tcpflow是服务器上经常使用的一个小程序,它能够捕获tcp的数据流,并将其存储为方便分析和调试的格式.每一条tcp流都会被存储到独立的文件中,因此,典型的tcp流将会被分别存储为进 ...

  3. 个人网站tqqj.top

    建站历程 就在这里记录自己的建站历程吧!:) 2022-3-21 今天写这个建站历程实际上是有点晚了,因为我已经把网站完全上线了,意思就是说网站已经在运行了. 这个网站是准备建立自己的博客使用的,但是 ...

  4. kubernetes 的ingress controller 的nginx configuration配置参数

    下列列举一些参数其中常用的大家可根据实际情况自行添加(影响全局) kubectl edit cm nginx-configuration -n ingress-nginx 配置文件cm的定义: htt ...

  5. centos7升级openssh8.7&openssl1.1.1l脚本

    脚本下载地址或首页联系本人获取 https://files.cnblogs.com/files/blogs/705493/update_ssh.sh 需搭配openssh8.7和openssl1.1. ...

  6. Linux 查询文件内容重复数 uniq、sort命令

    前提:uniq只能查询数据相邻的重复次数,而sort可以查询乱序的重复次数. 原谅我,以下内容都是复制菜鸟驿站的!!! Linux uniq 命令用于检查及删除文本文件中重复出现的行列,一般与 sor ...

  7. [转载]从phpinfo中能获取哪些敏感信息

    phpinfo()想必的最熟悉的了,在搭建环境之后都会随后写一个 phpinfo()来测试环境是否正常,很多人测试完毕忘记删除就开始部署环境了,这就造成了一些敏感信息的泄漏.那么我们能从 phpinf ...

  8. wifi钓鱼

    无线网络的加密方式和破解方式 1.WEP加密及破解 1).WEP加密方式 有线等效保密(wired euivalent pricacy,WEP)协议的使用RC4(rivest cipher4)串流加密 ...

  9. netcore后台任务注意事项

    开局一张图,故事慢慢编!这是一个后台任务打印时间的德莫,代码如下: using BackGroundTask; var builder = WebApplication.CreateBuilder() ...

  10. 记-Windows环境下Prometheus+alertmanager+windows_exporter+mtail监控部署

    1.概述 最近因项目需要统计服务的负载情况及机器的负载情况,但是项目里面却没有相关统计而服务所在的机器也没有相关的监控,因为工期原因就选择了相对轻量级的prometheus方案.其中windows_e ...