mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-26 02:26:28 +08:00
9a861140d8
Start to translate codes from Java to Python.
43 lines
1.3 KiB
Java
43 lines
1.3 KiB
Java
/*
|
|
* File: deque.java
|
|
* Created Time: 2022-11-25
|
|
* Author: Krahets (krahets@163.com)
|
|
*/
|
|
|
|
package chapter_stack_and_queue;
|
|
|
|
import java.util.*;
|
|
|
|
public class deque {
|
|
public static void main(String[] args) {
|
|
/* 初始化队列 */
|
|
Deque<Integer> deque = new LinkedList<>();
|
|
|
|
/* 元素入队 */
|
|
deque.offerLast(2);
|
|
deque.offerLast(5);
|
|
deque.offerLast(4);
|
|
deque.offerFirst(3);
|
|
deque.offerFirst(1);
|
|
System.out.println("队列 deque = " + deque);
|
|
|
|
/* 访问队首元素 */
|
|
int peekFirst = deque.peekFirst();
|
|
System.out.println("队首元素 peekFirst = " + peekFirst);
|
|
int peekLast = deque.peekLast();
|
|
System.out.println("队尾元素 peekLast = " + peekLast);
|
|
|
|
/* 元素出队 */
|
|
int pollFirst = deque.pollFirst();
|
|
System.out.println("队首出队元素 pollFirst = " + pollFirst + ",队首出队后 deque = " + deque);
|
|
int pollLast = deque.pollLast();
|
|
System.out.println("队尾出队元素 pollLast = " + pollLast + ",队尾出队后 deque = " + deque);
|
|
|
|
/* 获取队列的长度 */
|
|
int size = deque.size();
|
|
System.out.println("队列长度 size = " + size);
|
|
|
|
/* 判断队列是否为空 */
|
|
boolean isEmpty = deque.isEmpty();
|
|
}
|
|
}
|