集合第二步: 模拟LinkedList底层实现
2017-08-21 14:33
495 查看
package cn.sdut.chapter5; /* * LinkedList 底层是一个双向链表 模拟实现 */ public class MyLinkedList { private int size = 0; private node head; private node last; class node { node pre; node next; String data; node(String data) { this.data = data; } } // 添加 public void add(String str) { node node = new node(str); if (size == 0) { this.head = node; this.last = node; } else { this.last.next = node; node.pre = this.last; this.last = node; } size++; } public void print() { node current = this.head; while (current != null) { System.out.println(current.data); current = current.next; } } //删除 public void remove(String str) { node current = this.head; while (current != null) { if (current.data.equals(str)) { if (current == this.head) { this.head = current.next; this.head.pre = null; } else if (current == this.last) { this.last = current.pre; this.last.next = null; } else { current.next.pre = current.pre; current.pre.next = current.next; } size--; break; } else { current = current.next; } } } }
相关文章推荐
- Java集合,LinkedList底层实现和原理
- Java基础之集合List-ArrayList、LinkedList、Vector的底层实现和区别
- java集合LinkedList的底层实现源码详解
- java集合List的实现类LinkedList源码分析
- java 集合框架-利用LinkedList实现堆栈数据结构
- 参照JDK源码实现一个LinkedList,分析常用的List集合及其适用场景
- ArrayList和LinkedList底层实现的区别
- c#利用链表模拟实现泛型集合List﹤T﹥
- List集合的子实现类的特点(ArrayList,Vector,LinkedList),JDK5以后提供的新特性
- java集合学习之List(三)以LinkedList为例,debug看下迭代器的实现
- LinkedList底层实现
- Java 集合:Collection,List,ArrayList,Vector,LinkedList(实现方式,对比)
- ArrayList集合实现RandomAccess接口有何作用?为何LinkedList集合却没实现这接口?
- Java集合---LinkedList底层原理
- ArrayList、LinkedList、HashMap的底层实现
- 集合框架源码分析三(实现类篇ArrayList,LinkedList,HashMap)
- LinkedList的底层实现
- 集合:List实现类LinkedList
- 深入Java集合学习系列:LinkedList的实现原理
- Java集合之ArrayList和LinkedList的实现原理以及Iterator详解