【LinkedList】常用方法大全

1、添加元素:

push、offerFirst都调用了addFirst函数,只不过push和addFirst一样没有返回值,offer会返回true;add背后通过linkLast(e)源码实现且有返回值

具体方法实现备注
addpublic boolean add(E e) {linkLast(e); return true;}有返回值
offerpublic boolean offer(E e) {return add(e); }有返回值
addFirst public void addFirst(E e) {linkFirst(e);}无返回值
pushpublic void push(E e) {addFirst(e);}等价于addFirst
offerFirst public boolean offerFirst(E e) {addFirst(e);return true;}有返回值
addLastpublic void addLast(E e) {linkLast(e);}无返回值
offerLastpublic boolean offerLast(E e) {addLast(e);return true;}有返回值

2、删除元素:

remove、pop都调用了removeFirst函数,removeFirst函数内部又调用了unlinkFirst;poll内部也调用了unlinkFirst;poll与remove和pop不同的是poll在链表为空调用时返回null,另外两个抛出NoSuchElementException异常

具体方法实现备注
removeFirstpublic E removeFirst() { final Node<E> f = first; if (f == null) throw new NoSuchElementException();return unlinkFirst(f);}会抛出NoSuchElementException异常
removeLastpublic E removeLast() {final Node<E> l = last;if (l == null)throw new NoSuchElementException();return unlinkLast(l);}会抛出NoSuchElementException异常
removepublic E remove() { return removeFirst(); }会抛出NoSuchElementException异常
removepublic E remove(int index) {checkElementIndex(index);return unlink(node(index));}会抛出IndexOutOfBoundsException异常
poppublic E pop() {return removeFirst(); }会抛出异常
poll·public E poll() {final Node<E> f = first;return (f == null) ? null : unlinkFirst(f); }为空是返回null
pollFirstpublic E pollFirst() { final Node<E> f = first; return (f == null) ? null : unlinkFirst(f);}为空是返回null
pollLastpublic E pollLast() {final Node<E> l = last;return (l == null) ? null : unlinkLast(l);}为空是返回null

3、测试代码

public class testLinkedList {
    public static void main(String[] args) {
        LinkedList<Integer> list=new LinkedList<>();
        /*
        增加元素
         */
        //1、add向末尾增加一个元素,添加成功返回为true
        boolean result1=list.add(1);
        boolean result2=list.add(2);
        list.add(2);
        list.add(2);
        list.add(2);
        System.out.println("测试1插入1、2、2、2、2:");
        System.out.println(list.toString());
        System.out.println(result1);
        //2、add向指定位置插入元素,没有返回值
        list.add(1,1000);
        System.out.println("测试2在下标1处插入1000:");
        System.out.println(list.toString());
        //3、addFirst向链表头插入元素,没有返回值
        list.addFirst(3);
        System.out.println("测试3在表头插入3:");
        System.out.println(list.toString());
        //4、offerFirst,如果成功返回true
        boolean result8=list.offerFirst(5);
        System.out.println("测试4在表头插入5:");
        System.out.println(list.toString());
        System.out.println(result8);
        //5、向链表中第一个位置插入元素,无返回值
        list.push(8);
        System.out.println("测试5在表头插入8:");
        System.out.println(list.toString());
        //6、addLast向链表尾插入元素,没有返回值
        list.addLast(4);
        System.out.println("测试6在表头插入4:");
        System.out.println(list.toString());
        //7、offer向链表尾部插入元素,插入成功返回true
        boolean result10=list.offer(7);
        System.out.println("测试7在表尾插入7:");
        System.out.println(result10);
        System.out.println(list.toString());
        //8、offerLast,向链表尾部插入元素,插入成功返回true
        boolean result9=list.offerLast(6);
        list.offerLast(2);
        list.offerLast(2);
        list.offerLast(2);
        System.out.println("测试8在表尾插入6、2、2、2:");
        System.out.println(result9);
        System.out.println(list.toString());

         /*
        获取元素
         */
        //9、contains检查list里是否包含某些元素,如果包含返回true
        boolean result3=list.contains(1000);
        boolean result4=list.contains(10000);
        System.out.println("测试9检查链表中是否包含1000、10000:");
        System.out.println(result3);
        System.out.println(result4);
        //10、根据下标获取元素
        Integer result5=list.get(2);
        System.out.println("测试10获取下标2的元素:");
        System.out.println(result5);
        //11、获取链表中第一个元素
        Integer result6=list.getFirst();
        System.out.println("测试11获取第1个元素:");
        System.out.println(result6);
        //12、获取链表中最后一个元素
        Integer result7=list.getLast();
        System.out.println("测试12获取最后1个元素:");
        System.out.println(result7);
        //13、获取链表的第1个元素
        Integer result11=list.peek();
        System.out.println("测试13获取链表的第1个元素:");
        System.out.println(result11);
        //14、获取链表的第1个元素
        Integer result12=list.peekFirst();
        System.out.println("测试14获取链表的第1个元素:");
        System.out.println(result12);
        //15、获取链表的最后1个元素
        Integer result13=list.peekLast();
        System.out.println("测试15获取链表的最后1个元素:");
        System.out.println(result13);

        /*
        删除元素
         */
        //16、pop 删除链表中第一个元素,链表为空执行删除时抛异常
        list.pop();
        System.out.println(list.toString());
        System.out.println("测试16删除链表中第1个元素:");
        //17、poll 删除链表中第一个元素,链表为空时,返回null
        list.poll();
        System.out.println(list.toString());
        System.out.println("测试17删除链表中第1个元素:");
        //18、pollFirst 删除链表中第一个元素,链表为空时,返回null
        list.pollFirst();
        System.out.println(list.toString());
        System.out.println("测试18删除链表中第1个元素:");
        //19、pollLast 删除链表中最后1个元素,链表为空时,返回null
        list.pollLast();
        System.out.println("测试19删除链表中第1个元素:");
        System.out.println(list.toString());
        //20、remove 删除链表中第1个元素
        list.remove();
        System.out.println("测试20删除链表中第1个元素:");
        System.out.println(list.toString());
        //21、removeFirst 删除链表中第1个元素
        list.removeFirst();
        System.out.println("测试21删除链表中第1个元素:");
        System.out.println(list.toString());
        //22、remove 删除链表中第1个元素
        list.removeLast();
        System.out.println("测试22删除链表中第1个元素:");
        System.out.println(list.toString());
        //23、IndexOutOfBoundsException
        boolean result14=list.remove(Integer.valueOf(2));
        System.out.println("测试23删除链表中下标为2元素:");
        System.out.println(result14);
        System.out.println(list.toString());
        //24、remove(index)会报java.lang.IndexOutOfBoundsException异常
        list.remove(1);
        System.out.println("测试24删除链表中下标为1的元素:");
        System.out.println(list.toString());
        //25、removeFirstOccurrence删除元素在链表中第一次出现的位置
        list.removeFirstOccurrence(Integer.valueOf(2));
        System.out.println("测试25删除链表中2第一次出现位置的元素:");
        System.out.println(list.toString());
        //26、removeLastOccurrenceremoveLastOccurrence
        list.removeLastOccurrence(Integer.valueOf(2));
        System.out.println(list.toString());
        //27、清空链表
        list.clear();
        System.out.println("测试26清空链表:");
        System.out.println(list.toString());
        list.removeLastOccurrence(Integer.valueOf(2));
        list.pop();
//        list.removeFirst();

    }
}

4、输出结果

测试1插入1、2、2、2、2:
[1, 2, 2, 2, 2]
true
测试2在下标1处插入1000:
[1, 1000, 2, 2, 2, 2]
测试3在表头插入3:
[3, 1, 1000, 2, 2, 2, 2]
测试4在表头插入5:
[5, 3, 1, 1000, 2, 2, 2, 2]
true
测试5在表头插入8:
[8, 5, 3, 1, 1000, 2, 2, 2, 2]
测试6在表头插入4:
[8, 5, 3, 1, 1000, 2, 2, 2, 2, 4]
测试7在表尾插入7:
true
[8, 5, 3, 1, 1000, 2, 2, 2, 2, 4, 7]
测试8在表尾插入6、2、2、2:
true
[8, 5, 3, 1, 1000, 2, 2, 2, 2, 4, 7, 6, 2, 2, 2]
测试9检查链表中是否包含1000、10000:
true
false
测试10获取下标2的元素:
3
测试11获取第1个元素:
8
测试12获取最后1个元素:
2
测试13获取链表的第1个元素:
8
测试14获取链表的第1个元素:
8
测试15获取链表的最后1个元素:
2
[5, 3, 1, 1000, 2, 2, 2, 2, 4, 7, 6, 2, 2, 2]
测试16删除链表中第1个元素:
[3, 1, 1000, 2, 2, 2, 2, 4, 7, 6, 2, 2, 2]
测试17删除链表中第1个元素:
[1, 1000, 2, 2, 2, 2, 4, 7, 6, 2, 2, 2]
测试18删除链表中第1个元素:
测试19删除链表中第1个元素:
[1, 1000, 2, 2, 2, 2, 4, 7, 6, 2, 2]
测试20删除链表中第1个元素:
[1000, 2, 2, 2, 2, 4, 7, 6, 2, 2]
测试21删除链表中第1个元素:
[2, 2, 2, 2, 4, 7, 6, 2, 2]
测试22删除链表中第1个元素:
[2, 2, 2, 2, 4, 7, 6, 2]
测试23删除链表中下标为2元素:
true
[2, 2, 2, 4, 7, 6, 2]
测试24删除链表中下标为1的元素:
[2, 2, 4, 7, 6, 2]
测试25删除链表中2第一次出现位置的元素:
[2, 4, 7, 6, 2]
[2, 4, 7, 6]
测试26清空链表:
[]
Exception in thread “main” java.util.NoSuchElementException
at java.util.LinkedList.removeFirst(LinkedList.java:270)
at java.util.LinkedList.pop(LinkedList.java:801)
at lrrtcode.testLinkedList.main(testLinkedList.java:141)

  • 18
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值