Java LinkedList作为栈和队列的使用

2 篇文章 0 订阅

最近用java写一些算法时,经常遇到要使用栈和队列结构(比如树的各种遍历等等),使用栈的话,Stack已经不被推荐使用了,所以栈和队列我们通常都是用LinkedList这种双链表结构实现,用的多了自然就开始好奇它的各种操作具体是怎么实现的?

先下面分别从用法和源码角度简单记录下如何用LinkedList实现栈和队列

作为使用时,push是入栈,pop / poll是出栈,peek是获取栈顶元素

具体使用如下示例:

LinkedList<String> stack = new LinkedList<>();
stack.push("a");
stack.push("b");
stack.push("c");
stack.push("d");
System.out.println(stack.peek());//栈为d c b a,输出:d
System.out.println(stack.poll());//栈为d c b a,输出:
System.out.println(stack.pop());//栈为c b a,输出c

push源码分析:

在使用LinkedList作为栈时,我们可以去看一下源码中push方法的实现,可以看到,push使用的时双链表的头插法,也就是在链表头部插入元素

public void push(E e) {
    addFirst(e);
}
public void addFirst(E e) {
    linkFirst(e);
}
private void linkFirst(E e) {
    final Node<E> f = first;
    final Node<E> newNode = new Node<>(null, e, f);
    first = newNode;
    if (f == null)
        last = newNode;
    else
        f.prev = newNode;
    size++;
    modCount++;
}

 

作为队列使用时,offer是入队列,poll是出队列,peek是获取队头元素

但LinkedList实现队列有两种用法

①直接使用LinkedList提供的offer和poll来实现出队、入队、取队头

LinkedList<String> queue = new LinkedList<>();
queue.offer("a");
queue.offer("b");
queue.offer("c");
queue.offer("d");
System.out.println(queue.peek());//队列为:a b c d,输出:a
System.out.println(queue.poll());//队列为:a b c d,输出:a
System.out.println(queue.pop());//队列为:b c d,输出:

②LinkedList实现了Deque接口,Deque继承自Queue,所以可以使用父类指向子类的方式来访问子类方法从而实现队列。

注意:Queue接口中出队列只提供了poll方法,所以LinkedList的pop方法就不能使用了。

Queue<String> queue = new LinkedList<>();
queue.offer("a");
queue.offer("b");
queue.offer("c");
queue.offer("d");
System.out.println(queue.peek());//队列为:a b c d,输出:a
System.out.println(queue.poll());//队列为:a b c d,输出:a

offer源码分析:

使用LinkedList作为队列时,offer方法就是从双向链表尾部添加元素,然后取出时调用pop/poll/peek从链表头部取出,从而实现FIFO。

public boolean offer(E e) {
    return add(e);
}
public boolean add(E e) {
    linkLast(e);
    return true;
}
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++;
}

综合上面的再补充一些总结:

push:头插,在双链表头部插入元素。

offer / add:尾插,在双链表尾部插入元素。

pop / poll:在双链表头部取出元素(取出时会删除该元素)。

pop和poll的区别是,如果获取元素为空时pop会抛出异常,poll会返回null。限于篇幅这里就不粘贴源码了,有兴趣的可以自己去翻一翻。

peek:在双链表头部读取元素(不会删除元素)。

 

水平有限,如有错误,麻烦评论或私信修改,谢谢了

 

  • 12
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Java 中,栈和队列都是常见的数据结构,可以通过使用内置的类或自定义类来实现。 是一种后进先出(LIFO)的数据结构,类似于一堆盘子,只能从最上面取盘子。Java 中的 Stack 类就是一个典型的实现,它提供了 push()、pop()、peek() 等方法。例如: ```java Stack<Integer> stack = new Stack<>(); stack.push(1); stack.push(2); stack.push(3); System.out.println(stack.pop()); // 输出 3 System.out.println(stack.peek()); // 输出 2 ``` 队列是一种先进先出(FIFO)的数据结构,类似于排队等候。Java 中的 Queue 接口提供了队列的基本操作,例如 offer()、poll()、peek() 等方法。LinkedList 类实现了 Queue 接口,可以用来实现队列。例如: ```java Queue<Integer> queue = new LinkedList<>(); queue.offer(1); queue.offer(2); queue.offer(3); System.out.println(queue.poll()); // 输出 1 System.out.println(queue.peek()); // 输出 2 ``` 除了内置的类,我们也可以自定义栈和队列的实现。例如,使用数组实现: ```java class MyStack { private int[] arr; private int top; public MyStack(int size) { arr = new int[size]; top = -1; } public void push(int val) { if (top == arr.length - 1) { throw new RuntimeException("Stack overflow"); } arr[++top] = val; } public int pop() { if (top == -1) { throw new RuntimeException("Stack underflow"); } return arr[top--]; } public boolean isEmpty() { return top == -1; } } ``` 使用链表实现队列: ```java class MyQueue { private ListNode head; private ListNode tail; public MyQueue() { head = null; tail = null; } public void offer(int val) { ListNode node = new ListNode(val); if (tail == null) { head = node; } else { tail.next = node; } tail = node; } public int poll() { if (head == null) { throw new RuntimeException("Queue underflow"); } int val = head.val; head = head.next; if (head == null) { tail = null; } return val; } public boolean isEmpty() { return head == null; } private class ListNode { int val; ListNode next; public ListNode(int val) { this.val = val; this.next = null; } } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值