- package src.com.zhang.likedlist;
-
- import java.util.Iterator;
- import java.util.LinkedList;
-
- public class doubleLinkedList {
- public static void main(String[] args) {
- LinkedList linkedList=new LinkedList<>();//此时linkedlist的first和last都为null
- linkedList.add(1);
- linkedList.add(2);
- linkedList.add(3);
- linkedList.remove();
- // remove方法底层源码
- // 默认删除读一个节点
- // public E remove() {
- // return removeFirst();
- // }
- //removeFirst底层源码
- // public E removeFirst() {
- // final Node<E> f = first;
- // if (f == null)
- // throw new NoSuchElementException();
- // return unlinkFirst(f);
- // }
-
- // private E unlinkFirst(Node<E> f) {
- // // assert f == first && f != null;
- // final E element = f.item;
- // final Node<E> next = f.next;
- // f.item = null;
- // f.next = null; // help GC
- // first = next;
- // if (next == null)
- // last = null;
- // else
- // next.prev = null;
- // size--;
- // modCount++;
- // return element;
- // }
-
- // add方法底层源码
- // public boolean add(E e) {
- // linkLast(e);
- // return true;
- // }
-
- // linkLast底层源码:
- // void linkLast(E e) {
- // final Node<E> l = last;
- // final Node<E> newNode = new Node<>(l, e, null);
- // last = newNode;//使last指向newNode节点
- // if (l == null)
- // first = newNode;
- // else
- // l.next = newNode;
- // size++;
- // modCount++;
- // }
- //修改某个节点
- linkedList.set(1,999);
- //获取某个节点对象
-
- System.out.println(linkedList.get(0));
- //迭代器遍历
- Iterator iterator=linkedList.iterator();
- while (iterator.hasNext()){
- Object next = iterator.next();
- System.out.println(next);
-
-
- }
-
- for (Object o :linkedList) {
- System.out.println(o);
- }
-
- }
- }