博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LinkedList源码分析
阅读量:6458 次
发布时间:2019-06-23

本文共 3134 字,大约阅读时间需要 10 分钟。

一、属性及获取属性:

1、size

transient int size = 0;/** * Pointer to first node. * Invariant: (first == null && last == null) || *            (first.prev == null && first.item != null) */transient Node
first;/** * Pointer to last node. * Invariant: (first == null && last == null) || * (last.next == null && last.item != null) */transient Node
last;

获取

public int size() {    return size;}

二、构造函数

//Constructs an empty listpublic LinkedList() {}public LinkedList(Collection
c) { this(); addAll(c);}

三、类

private static class Node
{ E item; Node
next; Node
prev; Node(Node
prev, E element, Node
next) { this.item = element; this.next = next; this.prev = prev; }}

四、方法

1、Node

Node
node(int index) { // assert isElementIndex(index); if (index < (size >> 1)) { Node
x = first; for (int i = 0; i < index; i++) x = x.next; return x; } else { Node
x = last; for (int i = size - 1; i > index; i--) x = x.prev; return x; }}

*、linkFirst

/** * Links e as first element. */private void linkFirst(E e) {    final Node
f = first; final Node
newNode = new Node<>(null, e, f); first = newNode; if (f == null) last = newNode; else f.prev = newNode; size++; modCount++;}void linkLast(E e) { final Node
l = last; final Node
newNode = new Node<>(l, e, null); last = newNode; if (l == null) first = newNode; else l.next = newNode; size++; modCount++;}

*、add、addFirst

public void addFirst(E e) {    linkFirst(e);}public boolean add(E e) {    linkLast(e);    return true;}

linkLast

2、set

public E set(int index, E element) {    checkElementIndex(index);    Node
x = node(index); E oldVal = x.item; x.item = element; return oldVal;}

3、get

public E get(int index) {    checkElementIndex(index);    return node(index).item;}

*、clear

public void clear() {    // Clearing all of the links between nodes is "unnecessary", but:    // - helps a generational GC if the discarded nodes inhabit    //   more than one generation    // - is sure to free memory even if there is a reachable Iterator    for (Node
x = first; x != null; ) { Node
next = x.next; x.item = null; x.next = null; x.prev = null; x = next; } first = last = null; size = 0; modCount++;}

*、Push Pop

public void push(E e) {    addFirst(e);}
public E pop() {    return removeFirst();}

*、node(int index)

Node
node(int index) { // assert isElementIndex(index); if (index < (size >> 1)) { Node
x = first; for (int i = 0; i < index; i++) x = x.next; return x; } else { Node
x = last; for (int i = size - 1; i > index; i--) x = x.prev; return x; }}

转载地址:http://psizo.baihongyu.com/

你可能感兴趣的文章
iOS 动态库和静态库
查看>>
爬虫python
查看>>
eclipse中servers(服务器)的配置
查看>>
ITOO-权限系统(一)
查看>>
二叉树的算法实现
查看>>
angular -- 自定义指令和模板
查看>>
angular -- get请求该如何使用?
查看>>
将 Java Spring Framework 应用程序迁移到 Windows Azure
查看>>
现实世界的Windows Azure:采访Soluto的创始人Tomer Dvir
查看>>
linux内存随笔
查看>>
13-初识指针
查看>>
UVA 11991 Easy Problem from Rujia Liu?【STL】
查看>>
科技助农、航空报国、产业兴邦——钱旺正式发布“雨田一号”农用无人机
查看>>
xsxsxsxsxsxsxsxs
查看>>
[转] Java中的ReentrantLock和synchronized两种锁定机制的对比
查看>>
Vector(同步)和ArrayList(异步)异同
查看>>
python基础和进阶思维导图(转)
查看>>
Axure XMind整理交互思路
查看>>
12.5
查看>>
Windows Phone 7 应用程序生命周期与导航
查看>>