自己实现LinkedList
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的更多相关文章
- To Java程序员:切勿用普通for循环遍历LinkedList
ArrayList与LinkedList的普通for循环遍历 对于大部分Java程序员朋友们来说,可能平时使用得最多的List就是ArrayList,对于ArrayList的遍历,一般用如下写法: p ...
- 计算机程序的思维逻辑 (39) - 剖析LinkedList
上节我们介绍了ArrayList,ArrayList随机访问效率很高,但插入和删除性能比较低,我们提到了同样实现了List接口的LinkedList,它的特点与ArrayList几乎正好相反,本节我们 ...
- 深入理解java中的ArrayList和LinkedList
杂谈最基本数据结构--"线性表": 表结构是一种最基本的数据结构,最常见的实现是数组,几乎在每个程序每一种开发语言中都提供了数组这个顺序存储的线性表结构实现. 什么是线性表? 由0 ...
- 用JavaScript来实现链表LinkedList
本文版权归博客园和作者本人共同所有,转载和爬虫请注明原文地址. 写在前面 好多做web开发的朋友,在学习数据结构和算法时可能比较讨厌C和C++,上学的时候写过的也忘得差不多了,更别提没写过的了.但幸运 ...
- ArrayList LinkedList源码解析
在java中,集合这一数据结构应用广泛,应用最多的莫过于List接口下面的ArrayList和LinkedList; 我们先说List, public interface List<E> ...
- ArrayList、Vector、LinkedList的区别联系?
1.ArrayList.Vector.LinkedList类都是java.util包中,均为可伸缩数组. 2.ArrayList和Vector底层都是数组实现的,所以,索引数据快,删除.插入数据慢. ...
- LinkedList<E>源码分析
LinkedList的数据结构就是双向链表,如下所示: private static class Node<E> { E item;//数据元素 Node<E> next;// ...
- Java集合之LinkedList
一.LinkedList概述 1.初识LinkedList 上一篇中讲解了ArrayList,本篇文章讲解一下LinkedList的实现. LinkedList是基于链表实现的,所以先讲解一下什么是链 ...
- 集合 LinkedList、ArrayList、Set、Treeset
LinkedList中特有的方法: 1:方法介绍 addFirst(E e) addLast(E e) getFirst() getLast() removeFirst() removeLast() ...
- ArrayList、Vector、LinkedList源码
List接口的一些列实现中,最常用最重要的就是这三个:ArrayList.Vector.LinkedList.这里我就基于JDK1.7来看一下源码. public class ArrayList< ...
随机推荐
- MyBatis核心对象之StatementHandler
MyBatis核心对象之StatementHandler StatementHandler ResultHandler ParameterHandler Executor org.apache.iba ...
- SpringData JPA实现增删改
一.创建实体类并自动生成数据库表 二.dao层继承JpaRepository 三.controller中增加操作 四.controller中删除操作 五.controller中修改操作
- Python爬虫的概括以及实战
第一章主要讲解爬虫相关的知识如:http.网页.爬虫法律等,让大家对爬虫有了一个比较完善的了解和一些题外的知识点. 今天这篇文章将是我们第二章的第一篇,我们从今天开始就正式进入实战阶段,后面将会有更 ...
- Python之format()函数
Python2.6开始,新增了一种格式化字符串的函数format(),它增强了字符串的格式化功能.(官方推荐) 基本语法是通过{}和:来代替以前的% format()函数可以接收不限个参数,位置可以不 ...
- Idea2019激活码
此教程仅用作个人学习,请勿用于商业获利,造成后果自负!!! 此教程已支持最新2019.2版本 此教程实时更新,请放心使用:如果有新版本出现猪哥都会第一时间尝试激活: idea官网下载地址:http:/ ...
- Java题库——Chapter12 异常处理和文本IO
异常处理 1)What is displayed on the console when running the following program? class Test { public stat ...
- C++ float vs double
精度 相比 float ,double 从其名字上已经展示出,它的精度是前者的两倍,他们的精度分别为: float: 7 位数字 double: 15 位数字 可通过如下的示例看出,在重复进行计算时, ...
- C#深入浅出之数据类型
基本数据类型 C#支持完整的BCL(基类库)名字,但是最好都统一使用关键字进行使用与开发,比如使用int而不是System.Int32,以及使用string类型时候应当使用string而 ...
- In .net 4.8,calculate the time cost of serialization in BinaryFormatter,NewtonSoft.json,and System.Text.Json.JsonSerializer.Serialize
using ConsoleApp390.Model; using Newtonsoft.Json; using System; using System.Collections.Generic; us ...
- openstack 搭建
#所有节点修改ip,主机名和hosts解析 controller 10.0.0.11 controller compute1 10.0.0.31 compute1 #所有节点准备本地repo源 rm ...