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. 在命令行运行 python 抛出 ModuleNotFoundError 的解决方法

    所要运行的 py 文件在子目录中,并且该文件引用了另一个子目录中的 py 模块.类似这样: 原因 在运行环境下, a.py 找不到 b.py 所以抛出 ModuleNotFoundError. 解决 ...

  2. Python 实现 JWT 生成

    Python 实现 JWT 生成 JWT 简介:https://www.jianshu.com/p/576dbf44b2ae Json web token (JWT), 是为了在网络应用环境间传递声明 ...

  3. 数据库篇:mysql日志类型之 redo、undo、binlog

    前言 可以说mysql的多数特性都是围绕日志文件实现,而其中最重要的有以下三种 redo 日志 undo 日志 binlog 日志 关注公众号,一起交流:微信搜一搜: 潜行前行 1 redo日志 in ...

  4. Ubuntu16.04 搭建samba服务器

    1昨天花了一天时间弄了NFS服务器,结果搭建完之后出现各种问题,要么挂载不上,要么就是字符乱码.今天在看到一个关于树莓派的介绍的时候,提到Samba服务器的搭建,我尝试了一下,结果发现很顺利地就能够正 ...

  5. 学习廖雪峰的Git教程1

    我是在Ubuntu上学习的,所以配置之类的进行的很快. 一.创建版本库 mkdir learngit cd learngit git init 用git init变成git可以管理的库 二.git a ...

  6. 如何实现 Spring Boot 应用程序的安全性?

    为了实现 Spring Boot 的安全性,我们使用 spring-boot-starter-security 依赖项,并且必须添加安全配置.它只需要很少的代码.配置类将必须扩展WebSecurity ...

  7. SpringBoot和SpringCloud?

    SpringBoot是Spring推出用于解决传统框架配置文件冗余,装配组件繁杂的基于Maven的解决方案,旨在快速搭建单个微服务而SpringCloud专注于解决各个微服务之间的协调与配置,服务之间 ...

  8. SpringBoot 自定义配置文件不会自动提示问题

    参阅:https://www.jianshu.com/p/ec3f0b0371e6

  9. 转载:TCP协议如何保证可靠传输

    转载至:https://www.cnblogs.com/xiaokang01/p/10033267.html TCP协议如何保证可靠传输 概述: TCP协议保证数据传输可靠性的方式主要有: (校 序  ...

  10. centos 7环境下安装rabbitmq

    以 前在windows 7下面成功安装过rabbitmq,但是在windows 10下面安装失败,各种问题,各种解决方法都试过,还是不成功,最终放弃治疗. 后来经人指点,在linux下安装rabbit ...