Notes - Queue (Hàng đợi) - FIFO
Skills:
Queue
June 24, 2025
04:32 AM
Loading content...
Related Posts
Theory Data Structure And Algorithms
Related Posts
Theory Data Structure And Algorithms
Deque là một cấu trúc dữ liệu (double-ended queue - hàng đợi 2 đầu). Nó cho phép thêm và xóa các phần tử ở cả hai đầu của hàng đợi một cách hiệu quả. Điều này giúp trong việc thực hiện các thao tác như thêm vào đầu hoặc cuối hàng đợi,xóa ở đầu hoặc cuối hàng đợi một cách nhanh chóng và hiệu quả hơn so với các cấu trúc dữ liệu khác như array hoặc Singly Linked List
1class DeQue<T> {
2 private items: T[] = []
3
4 addFront(item: T): void {
5 this.items.unshift(item)
6 }
7
8 addBack(item: T): void {
9 this.items.push(item)
10 }
11
12 removeFront(item: T): T | undefined {
13 return this.item.shift()!
14 }
15
16 removeBack(): T | undefined {
17 return this.items.pop();
18 }
19
20 peekFront(): T | undefined {
21 return this.items[0];
22 }
23
24 peekBack(): T | undefined {
25 return this.items[this.items.length - 1];
26 }
27
28 isEmpty(): boolean {
29 return this.items.length === 0;
30 }
31
32 size(): number {
33 return this.items.length;
34 }
35}