public class MyLinkedList<E> {

    private Node first;

    private int size;

    public int size(){
return size;
} @Override
public String toString() {
if(size == ){
return "[]";
}else{
StringBuilder sb = new StringBuilder("[");
Node current = first;
while (current != null){
sb.append(current.value).append(",");
current = current.next;
}
sb.replace(sb.length() - , sb.length(), "]");
return sb.toString();
}
} public static void main(String[] args) {
MyLinkedList list = new MyLinkedList();
list.addFirst("python").addFirst("java").addFirst("hello").addFirst("php");
System.out.println(list);
list.removeFirst();
System.out.println(list);
System.out.println(list.contains("python")); }
public E removeFirst(){
if(size == ){
return null;
}else {
Node<E> temp = first;
first = temp.next;
size--;
return temp.value;
}
} public boolean contains(E e){
Node current = first;
while(current != null){
if(current.value == e ){
return true;
}
current = current.next;
}
return false;
} public MyLinkedList addFirst(E e){
Node newNode = new Node(e);
newNode.next = first;
first = newNode;
size++;
return this;
} private static class Node<E>{
private E value;
private Node next; Node(E value){
this.value = value;
} @Override
public String toString() {
return value == null?"null":value+"";
}
}
}

自己实现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. Appium(一):java环境、AndroidSDK环境

    1. java环境 java的下载和安装可以看我以前写的Java基础:<java下载和安装>. 2. AndroidSDK环境 2.1 AndroidSDK下载 我们进入:https:// ...

  2. Python生成requirements.txt方法

    在查看别人的Python项目时,经常会看到一个requirements.txt文件,里面记录了当前程序的所有依赖包及版本号,其作用是用来在另一个环境上重新构建项目所需要的运行环境依赖. require ...

  3. cordova+vue混合式开发App

    应要求第一次使用cordova打包了一下vue写的app项目,期间遇到了不少问题,整理一下流程并记录一下常见问题吧.        cordova打包项目需要的环境配置啥的就不具体讲啦,百度一下很多教 ...

  4. 多个线程运行MR程序时hadoop出现的问题

    夜间多个任务同时并行,总有几个随机性有任务失败,查看日志: cat -n ads_channel.log |grep "Caused by" Caused by: java.uti ...

  5. ubuntu 查看端口被占用并删除端口

    做网络的同学,估计会经常用到这个功能,这里就做一个记录吧. 首先查看特定端口是占用了: sudo netstat -nplt 其次要删除特定端口并查看: kill -9 pid_num sudo ne ...

  6. SpringBoot源码学习系列之@PropertySource不支持yaml读取原因

    然后,为什么@PropertySource注解默认不支持?可以简单跟一下源码 @PropertySource源码: 根据注释,默认使用DefaultPropertySourceFactory类作为资源 ...

  7. Java学习笔记之面向对象、static关键字

    一周Java学习总结 今天就总结理清一下关于面向对象和面向过程的程序设计的一些不同特点,以及讲下static关键字. 面向对象 现在接触的Java是面向对象的,现在的程序开发几乎都是以面向对象为基础的 ...

  8. 【ST开发板评测】使用Python来开发STM32F411

    前言 板子申请了也有一段时间了,也快到评测截止时间了,想着做点有意思的东西,正好前一段时间看到过可以在MCU上移植MicroPython的示例,就自己尝试一下,记录移植过程. MicroPython是 ...

  9. JVM从入门开始深入每一个底层细节

    1 官网 1.1 寻找JDK文档过程 www.oracle.com -> 右下角Product Documentation -> 往下拉选择Java -> Java SE docum ...

  10. flutter_inner_drawer 使用

    版本: flutter_inner_drawer: "^0.2.2" github:  https://github.com/Dn-a/flutter_inner_drawer 这 ...