mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-30 19:02:46 +08:00
Migrate init_array, hash_map_operations, hash_map_traversal, queue_operations, deque_operations and insert_remove_node: zh and ru demo sources gain region markers and the snippets' comments, en, ja and ko are regenerated from reviewed translations, zh-hant via OpenCC. 70 blocks become region references; builtin_hash and five blocks without reviewed translations stay literal. The Java, Kotlin and Rust deque demos follow the book's order, and a few demo comments and typos are aligned with the snippets. Co-Authored-By: Claude Opus 5.5 <[email protected]>
53 lines
1.4 KiB
Java
53 lines
1.4 KiB
Java
/**
|
|
* File: queue.java
|
|
* Created Time: 2022-11-25
|
|
* Author: krahets ([email protected])
|
|
*/
|
|
|
|
package chapter_stack_and_queue;
|
|
|
|
import java.util.*;
|
|
|
|
public class queue {
|
|
public static void main(String[] args) {
|
|
// [region]{init_queue}
|
|
/* Initialize the queue */
|
|
Queue<Integer> queue = new LinkedList<>();
|
|
// [endregion]{init_queue}
|
|
|
|
// [region]{push}
|
|
/* Enqueue elements */
|
|
queue.offer(1);
|
|
queue.offer(3);
|
|
queue.offer(2);
|
|
queue.offer(5);
|
|
queue.offer(4);
|
|
// [endregion]{push}
|
|
System.out.println("Queue queue = " + queue);
|
|
|
|
// [region]{peek}
|
|
/* Access the front element */
|
|
int peek = queue.peek();
|
|
// [endregion]{peek}
|
|
System.out.println("Front element peek = " + peek);
|
|
|
|
// [region]{pop}
|
|
/* Dequeue an element */
|
|
int pop = queue.poll();
|
|
// [endregion]{pop}
|
|
System.out.println("Dequeued element pop = " + pop + ", after dequeuing, queue = " + queue);
|
|
|
|
// [region]{size}
|
|
/* Get the queue size */
|
|
int size = queue.size();
|
|
// [endregion]{size}
|
|
System.out.println("Queue size = " + size);
|
|
|
|
// [region]{is_empty}
|
|
/* Check whether the queue is empty */
|
|
boolean isEmpty = queue.isEmpty();
|
|
// [endregion]{is_empty}
|
|
System.out.println("Is the queue empty = " + isEmpty);
|
|
}
|
|
}
|