刷leetcode时,重新认识LinkedList实现栈、队列或者双端队列

简介: LinkedList实现了Deque和Queue接口,可以按照队列、栈和双端队列的方式进行操作。

如果想进大厂免不了要leetcode,而leetcode时免不了很多题跟栈,队列有关,重新认识一下LinkedList也许能在不时之需时,助你进入大厂。LinkedList实现了Deque和Queue接口,可以按照队列、栈和双端队列的方式进行操作。


0x01:Queue接口


Queue里面的方法,Queue扩展了Collection,它的主要操作有三个功能操作。每个操作有2个方法,针对队列长度是否受限制,对应是否抛异常。有些队列的是有长度限制的,本例的LinkedList实现queue没长度限制。


  • 在尾部添加元素 (add, offer):add()会在长度不够时抛出IllegalStateException异常;offer()则不会,只返回false;


  • 查看头部元素 (element, peek):返回头部元素,但不改变队列。element()会在没元素时抛出NoSuchElementException异常,而peek()返回null;


  • 删除头部元素 (remove, poll):返回头部元素,并且从队列中删除。remove()会在没元素时抛出NoSuchElementException异常; 而poll()返回null;


LinkedList模拟队列


import java.util.LinkedList;
import java.util.Queue;
/**
* 用LinkedList模拟队列,因为链表擅长插入和删除
*/
public class QueueDemo{
public static void main(String [] args) { 
//做剑指offer遇见过这个数结
        Queue<String> queue = new LinkedList<String>();
//追加元素
        queue.add("zero");
        queue.offer("one");
        queue.offer("two");
        queue.offer("three");
        queue.offer("four");
        System.out.println(queue);//[zero, one, two, three, four]
//从队首取出元素并删除
        System.out.println(queue.poll()); // zero
        System.out.println(queue.remove()); // one
        System.out.println(queue); //[two, three, four]
//从队首取出元素但是不删除
        String peek = queue.peek();
        System.out.println(peek); // two
//遍历队列,这里要注意,每次取完元素后都会删除,整个
//队列会变短,所以只需要判断队列的大小即可
while(queue.size() > 0) {
            System.out.println(queue.poll());
        }//two three four
    }
}


0x02:Deque接口


Java中有一个类Stack,用于表示栈,但该类已经过时了。Java中没有单独的栈接口,栈相关方法包括在了表示双端队列的接口Deque中,主要有三个方法。


  • push()表示入栈,在头部添加元素,栈的空间可能是有限的,如果栈满了,push会抛出异常IllegalStateException;


  • pop()表示出栈,返回头部元素,并且从栈中删除,如果栈为空,会抛出NoSuchElementException异常;


  • peek()查看栈头部元素,不修改栈,如果栈为空,返回null;


栈和队列只是双端队列的特殊情况,它们的方法都可以使用双端队列的方法替代,使用不同的名称和方法,概念上更为清晰。具体参考Deque接口里面的方法。


LinkedList模拟栈


import java.util.Deque;
import java.util.LinkedList;
public class StackDemo{
public static void main(String[] args) {
/*模拟栈,这是从头开始进来的*/
        Deque<String> deque = new LinkedList<String>();
/*Pushes an element onto the stack at the head of this dequeue */
        deque.push("a");
        deque.push("b");
        deque.push("c");
        System.out.println(deque); //[c, b, a]
//获取栈首元素后,元素不会出栈
        System.out.println(deque.peek());//c
while(deque.size() > 0) {
//获取栈首元素后,元素将会出栈
            System.out.println(deque.pop());//c b a
        }
        System.out.println(deque);//[]
/*模拟栈*/
        deque.offerLast("a");
        deque.offerLast("b");
        deque.offerLast("c");// [a, b, c]
while(!deque.isEmpty()){
            System.out.println(deque.pollLast());
        }
    }   // 先输出c再b最后a
}


相关文章
|
3月前
|
存储 算法 测试技术
力扣经典150题第五十四题:最小栈
力扣经典150题第五十四题:最小栈
32 0
|
4月前
|
存储 算法 索引
力扣每日一题 6/24 模拟 数组 单调栈
力扣每日一题 6/24 模拟 数组 单调栈
31 0
|
12天前
【LeetCode 24】225.用队列实现栈
【LeetCode 24】225.用队列实现栈
8 0
|
13天前
|
算法
【LeetCode 23】232.用栈实现队列
【LeetCode 23】232.用栈实现队列
14 0
|
2月前
|
Python
【Leetcode刷题Python】剑指 Offer 30. 包含min函数的栈
本文提供了实现一个包含min函数的栈的Python代码,确保min、push和pop操作的时间复杂度为O(1)。
22 4
|
2月前
|
Python
【Leetcode刷题Python】946. 验证栈序列
LeetCode题目“946. 验证栈序列”的Python解决方案,通过模拟栈的压入和弹出操作来验证给定的两个序列是否能通过合法的栈操作得到。
24 6
|
2月前
|
Python
【Leetcode刷题Python】剑指 Offer 09. 用两个栈实现队列
使用两个栈实现队列的Python解决方案,包括初始化两个栈、实现在队列尾部添加整数的appendTail方法和在队列头部删除整数的deleteHead方法,以及相应的示例操作。
36 2
|
2月前
|
Python
【Leetcode刷题Python】641.循环双端队列
文章介绍了如何实现一个循环双端队列,包括其操作如插入、删除、获取队首和队尾元素,以及检查队列是否为空或已满,并提供了Python语言的实现代码。
21 0
|
2月前
|
Python
【Leetcode刷题Python】232. 用栈实现队列
如何使用Python语言通过两个栈来实现队列的所有基本操作,包括入队(push)、出队(pop)、查看队首元素(peek)和判断队列是否为空(empty),并提供了相应的代码实现。
18 0
|
4月前
|
存储 算法 Python
二刷力扣--栈和队列
二刷力扣--栈和队列