mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-27 04:36:29 +08:00
41 lines
1.1 KiB
Dart
41 lines
1.1 KiB
Dart
|
/**
|
||
|
* File: deque.dart
|
||
|
* Created Time: 2023-03-28
|
||
|
* Author: liuyuxin (gvenusleo@gmail.com)
|
||
|
*/
|
||
|
|
||
|
import 'dart:collection';
|
||
|
|
||
|
void main() {
|
||
|
/* 初始化双向队列 */
|
||
|
final Queue<int> deque = Queue();
|
||
|
|
||
|
/* 元素入队 */
|
||
|
deque.addLast(2);
|
||
|
deque.addLast(5);
|
||
|
deque.addLast(4);
|
||
|
deque.addFirst(3);
|
||
|
deque.addFirst(1);
|
||
|
print("双向队列 deque = $deque");
|
||
|
|
||
|
/* 访问元素 */
|
||
|
final int peekFirst = deque.first;
|
||
|
print("队首元素 peekFirst = $peekFirst");
|
||
|
final int peekLast = deque.last;
|
||
|
print("队尾元素 peekLast = $peekLast");
|
||
|
|
||
|
/* 元素出队 */
|
||
|
final int popFirst = deque.removeFirst();
|
||
|
print("队首出队元素 popFirst = $popFirst,队首出队后 deque = $deque");
|
||
|
final int popLast = deque.removeLast();
|
||
|
print("队尾出队元素 popLast = $popLast,队尾出队后 deque = $deque");
|
||
|
|
||
|
/* 获取双向队列的长度 */
|
||
|
final int size = deque.length;
|
||
|
print("双向队列的长度 size = $size");
|
||
|
|
||
|
/* 判断双向队列是否为空 */
|
||
|
final bool isEmpty = deque.isEmpty;
|
||
|
print("双向队列是否为空 isEmpty = $isEmpty");
|
||
|
}
|