LinkedList的自定义实现
一、背景
LinkedList双向链表;
代码:
Node.java:
package com.cy.collection;
public class Node {
Node previous; //上一个node
Object obj; //node上值
Node next; //下一个node
public Node(Object obj) {
this.obj = obj;
}
}
LinkedList.java:
package com.cy.collection; /**
* 自定义实现链表LinkedList
*/
public class LinkedList {
private Node head;
private Node tail;
private int size; //add
public void add(Object obj){
Node n = new Node(obj);
if(head==null){
head = n;
tail = n;
}else{
n.previous = tail;
tail.next = n;
tail = n;
}
size++;
} //在指定下标位置,插入值
public void add(int index, Object obj){
rangeCheck(index); Node temp = node(index);
Node newNode = new Node(obj); if(temp!=null){
Node up = temp.previous;
if(up==null){ //如果是在head节点前面插入
newNode.next = temp;
temp.previous = newNode;
head = newNode;
}else{
up.next = newNode;
newNode.previous = up; newNode.next = temp;
temp.previous = newNode;
}
size++;
}
} //get
public Object get(int index){
rangeCheck(index); Node temp = node(index);
if(temp!=null){
return temp.obj;
} return null;
} public void remove(int index){
Node temp = node(index); if(temp!=null){
Node up = temp.previous;
Node down = temp.next; if(up==null){ //如果remove的是head节点
down.previous = null;
head = down;
}else if(down==null){ //如果remove的是tail节点
up.next = null;
tail = up;
}else{ //remove的是中间节点
up.next = down;
down.previous = up;
}
size--;
}
}
//找到指定位置index的node
public Node node(int index){
Node temp = null;
if(head!=null){
if(index < (size>>1)){ //从head开始遍历
temp = head;
for(int i=0;i<index;i++){
temp = temp.next;
}
}else{ //从tail开始遍历
temp = tail;
for(int i=size-1;i>index;i--){
temp = temp.previous;
}
}
}
return temp;
} //size
public int size(){
return size;
} //range check
public void rangeCheck(int index){
if(index<0 || index>=size){
throw new RuntimeException("IndexOutOfBoundsException");
}
}
}
Test.java测试:
package com.cy.collection;
public class Test {
public static void main(String[] args) {
LinkedList list = new LinkedList();
list.add("aaa");
list.add("bbb");
list.add("ccc");
System.out.println(list.size());
list.add(2, "ddd");
System.out.println(list.get(3));
System.out.println(list.size());
}
}
输出:
3
ccc
4
LinkedList的自定义实现的更多相关文章
- 自定义listView添加滑动删除功能
今天研究了一下android里面的手势,结合昨天学习的自定义View,做了一个自定义的listview,继承自listView,添加了条目的滑动手势操作,滑动后出现一个删除按钮,点击删除按钮,触发一个 ...
- 深入Collection集合
List集合 一.ArraryList: 最基本的集合不多做介绍 二.Vector Vector cn=new Vector(); A:有特有功能 a:添加 public void ad ...
- java12
1:List的子类(掌握) (1)List的子类特点 ArrayList: 底层数据结构是数组,查询快,增删慢 线程不安全,效率高 Vector: 底层数据结构是数组,查询快,增删慢 线程安全,效率低 ...
- java16
1:List的子类(掌握) (1)List的子类特点 ArrayList: 底层数据结构是数组,查询快,增删慢 线程不安全,效率高 Vector: 底层数据结构是数组,查询快,增删慢 线程安全,效率低 ...
- java中Collection类及其子类
1:对象数组(掌握) (1)数组既可以存储基本数据类型,也可以存储引用类型.它存储引用类型的时候的数组就叫对象数组. 2:集合(Collection)(掌握) (1)集合的由来? 我们学习的是Java ...
- javaSE第十六天
第十六天 140 1:List的子类(掌握) 140 (1)List的子类特点 140 (2)ArrayList 141 A:没有特有功能需要学习 141 B:案例 ...
- java基础(十五)集合(二)
这里有我之前上课总结的一些知识点以及代码大部分是老师讲的笔记 个人认为是非常好的,,也是比较经典的内容,真诚的希望这些对于那些想学习的人有所帮助! 由于代码是分模块的上传非常的不便.也比较多,讲的也是 ...
- Java学习笔记之集合
集合(Collection)(掌握) (1)集合的由来? 我们学习的是Java -- 面向对象 -- 操作很多对象 -- 存储 -- 容器(数组和StringBuffer) -- 数组而数组的长度固定 ...
- Unity3D游戏GC优化总结---protobuf-net无GC版本优化实践
protobuf-net优化效果图 protobuf-net是Unity3D游戏开发中被广泛使用的Google Protocol Buffer库的c#版本,之所以c#版本被广泛使用,是因为c++版本的 ...
随机推荐
- HPU组队赛B:问题(二进制枚举)
时间限制1 Second 内存限制 512 Mb 题目描述 你有n个问题,你已经估计了第i个问题的难度为Ci,现在你想使用这些问题去构造一个问题集.比赛的问题集必须包含至少两个问题,而且比赛的总难度必 ...
- SQL-常用命令
1.基本概念 SQL(Structured Query Language)结构化查询语言:一种对数据库进行操作的语言. DBMS:数据库管理系统. MySQL:一个数据库管理系统. 约束值:通过对表的 ...
- [原]android 链接错误
由于没有使用NDK的makefile, 而是把NDK的toolchain集成到现有的build system, 所以出现了诡异的错误: unsupported dynamic reloc R_ARM_ ...
- SQL Server 并发死锁解决案例备忘
SET @sql = ' SET TRANSACTION ISOLATION LEVEL REPEATABLE READ; SET DEADLOCK_PRIORITY 10 BEGIN TRAN DE ...
- linux二进制/十六进制日志文件如何查看和编辑
使用cat查看二进制,显示乱码 [root@localhost ~]# cat /var/log/wtmp ~~~reboot3.10.0-514.el7.x86_64 �YO#5~~~runleve ...
- Hasura GraphQL 内部表结构
Hasura 使用pg 数据库存储引擎的元数据信息,在hdb_catalog schema 下面,是在初始化的时候生成的 对于表的管理.权限的信息存储都在这个schema下 hdb_table 这个表 ...
- phpdocumentor安装和使用总结
为了解决一校友在安装和使用phpDocumentor过程中遇到的问题,自己闲时也折腾了一下这个东西,总结见下: 一.定义: 自己刚听到这个词时还不知道这个是什么东西,干啥用的,就去百度了一下,说道: ...
- Explicit
Prefixing the explicit keyword to the constructor prevents the compiler from using that constructor ...
- UWA 技术分享连载 转载
技术分享连载1 Q1:Texture占用内存总是双倍,这个是我们自己的问题,还是Unity引擎的机制? Q2:我现在发现两个因素直接影响Overhead,一个是Shader的复杂度,一个是空Updat ...
- revit api 使用过滤器
1. Door在Revit里面的element类型是FamilyInstance. 2. Door在Revit里面的category类型是OST_Doors. 3. 想要过滤特定类型的element需 ...