Java ?????LinkedList??????
???????????? ???????[ 2015/1/15 10:51:25 ] ????????Java LinkedList
Node<E>node(intindex){
//assertisElementIndex(index);
if(index<(size>>1)){
Node<E>x=first;
for(inti=0;i<index;i++)
x=x.next;
returnx;
}else{
Node<E>x=last;
for(inti=size-1;i>index;i--)
x=x.prev;
returnx;
}
}
|
???????????????????????????λ???????????????????????????????????pred;?????????????????????????????????鯔????????????????????????λ????????????????????Сsize?????
????b:get(int)
????public E get(int index) {
????checkElementIndex(index);
????return node(index).item;
????}
????private void checkElementIndex(int index) {
????if (!isElementIndex(index))
????throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
????}
????????????????????????????????ж?????????????????????node()???????????????????
????c:set(int??E)
????public E set(int index?? E element) {
????checkElementIndex(index);
????Node<E> x = node(index);
????E oldVal = x.item;
????x.item = element;
????return oldVal;
????}
???????????????????????????????????????????????????????????node?????????????????????????????????????ɡ?
????d:add(int??E)
public void add(int index?? E element) {
checkPositionIndex(index);
if (index == size)
linkLast(element);
else
linkBefore(element?? node(index));
}
private void checkPositionIndex(int index) {
if (!isPositionIndex(index))
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
void linkLast(E e) {
final Node<E> l = last;
final Node<E> newNode = new Node<>(l?? e?? null);
last = newNode;
if (l == null)
first = newNode;
else
l.next = newNode;
size++;
modCount++;
}
void linkBefore(E e?? Node<E> succ) {
// assert succ != null;
final Node<E> pred = succ.prev;
final Node<E> newNode = new Node<>(pred?? e?? succ);
succ.prev = newNode;
if (pred == null)
first = newNode;
else
pred.next = newNode;
size++;
modCount++;
}
|
??????????????????????????μ????????????????????????±???????????????????ж????????????β????????????в????????β??????????linkLast(E)?????????????????в????? linkBefore(element?? node(index));??????
????linkLast(E):??????????β????????????β??????????????????????????
????linkBefore(E e?? Node<E> succ)?????????????????????????棬??????????????λ???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
????e??emove(int)
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11