mirror of
https://github.com/krahets/hello-algo.git
synced 2026-12-01 03:09:59 +08:00
Cite demo-source regions from the remaining A-class snippets (batch 2) (#2035)
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]>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
849b6a95b9
commit
eeb8a3b790
@@ -10,37 +10,51 @@ import java.util.*;
|
||||
|
||||
public class deque {
|
||||
public static void main(String[] args) {
|
||||
// [region]{init_deque}
|
||||
/* Initialize the deque */
|
||||
Deque<Integer> deque = new LinkedList<>();
|
||||
deque.offerLast(3);
|
||||
deque.offerLast(2);
|
||||
// [endregion]{init_deque}
|
||||
|
||||
// [region]{push}
|
||||
/* Enqueue elements */
|
||||
deque.offerLast(2); // Add to the rear
|
||||
deque.offerLast(5);
|
||||
deque.offerLast(4);
|
||||
deque.offerFirst(3); // Add to the front
|
||||
deque.offerFirst(1);
|
||||
// [endregion]{push}
|
||||
System.out.println("Deque deque = " + deque);
|
||||
|
||||
// [region]{peek_first}
|
||||
/* Access an element */
|
||||
int peekFirst = deque.peekFirst();
|
||||
int peekFirst = deque.peekFirst(); // Front element
|
||||
// [endregion]{peek_first}
|
||||
System.out.println("Front element peekFirst = " + peekFirst);
|
||||
int peekLast = deque.peekLast();
|
||||
// [region]{peek_last}
|
||||
int peekLast = deque.peekLast(); // Rear element
|
||||
// [endregion]{peek_last}
|
||||
System.out.println("Rear element peekLast = " + peekLast);
|
||||
|
||||
/* Enqueue elements */
|
||||
deque.offerLast(4);
|
||||
System.out.println("After enqueuing element 4 at the rear, deque = " + deque);
|
||||
deque.offerFirst(1);
|
||||
System.out.println("After enqueuing element 1 at the front, deque = " + deque);
|
||||
|
||||
// [region]{pop_first}
|
||||
/* Dequeue an element */
|
||||
int popLast = deque.pollLast();
|
||||
System.out.println("Element dequeued from the rear = " + popLast + ", after dequeuing from the rear, deque = " + deque);
|
||||
int popFirst = deque.pollFirst();
|
||||
int popFirst = deque.pollFirst(); // Dequeue the front element
|
||||
// [endregion]{pop_first}
|
||||
System.out.println("Element dequeued from the front = " + popFirst + ", after dequeuing from the front, deque = " + deque);
|
||||
// [region]{pop_last}
|
||||
int popLast = deque.pollLast(); // Dequeue the rear element
|
||||
// [endregion]{pop_last}
|
||||
System.out.println("Element dequeued from the rear = " + popLast + ", after dequeuing from the rear, deque = " + deque);
|
||||
|
||||
// [region]{size}
|
||||
/* Get the deque size */
|
||||
int size = deque.size();
|
||||
// [endregion]{size}
|
||||
System.out.println("Deque size = " + size);
|
||||
|
||||
// [region]{is_empty}
|
||||
/* Check whether the deque is empty */
|
||||
boolean isEmpty = deque.isEmpty();
|
||||
// [endregion]{is_empty}
|
||||
System.out.println("Is the deque empty = " + isEmpty);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user