双向链表Q16

根据指定的结构 和 方法要求 实现 LinkedList

假设 LinkedList 里存储的元素 是 Int

LinkedListNode

结构

prev: LinkedListNode

指向上一个节点的指针

next: LinkedListNode

指向下一个节点的指针

value: Int

当前节点所存储的值

LinkedList

结构

head: LinkedListNode

指向头节点的指针

tail: LinkedListNode

指向尾节点的指针

size: Int

对链表尺寸进行缓存

API

add(value: Int)

往集合的最后一个位置 增加一个元素 value

insert(index: Int, value: Int)

往集合的 index 位置插入元素 value

样例
list is <6, 7, 3, 8, 4>
list.insert(2, 9);
list is <6, 7, 9, 3, 8, 4>
list.insert(6, 1);
list is <6, 7, 9, 3, 8, 4, 6>
合法性检查

需要检测 index 的合法性。

index 应该 大于等于 0,且小于等于 当前元素个数

如果 index 非法,则不做任何操作

get(index: Int): Int

返回集合中 index 的位置

size(): Int

返回 ArrayList 里有多少个元素

toString(): String

返回字符串

格式

如果数组内有一些元素,返回

"<6, 7, 3, 8, 4>"

如果数组内没有元素,返回

"<>"

ZZAX 微信公众

文档一更新,立刻告诉你