Chuyển tới nội dung chính
Z

Stack vs Queue

Stack vs queue compared — LIFO vs FIFO, operations and use cases, with interactive visualizers for both.

A stack is last-in, first-out (LIFO); a queue is first-in, first-out (FIFO). Both add and remove in O(1) — the difference is which end you take from.

Stack vs Queue at a glance

Stack Queue
Order LIFO FIFO
Add / remove Same end (top) Opposite ends
Used in Undo, recursion, DFS Scheduling, buffering, BFS
Complexity O(1) O(1)

When to use Stack

Use a stack when the most recent item should be handled first — undo, call stacks, DFS.

When to use Queue

Use a queue when items should be handled in arrival order — task scheduling, BFS, buffers.

Tools for Stack & Queue

Stack vs Queue

Hash table là gì?

Hash table lưu trữ các cặp key/value trong một mảng gồm nhiều bucket, sử dụng hash function để tính chỉ số bucket tương ứng với mỗi key, giúp các thao tác insert, search và delete đạt tốc độ gần như hằng số.

Va chạm hash (hash collision) là gì?

Là khi hai key khác nhau lại được băm ra cùng một bucket. Công cụ này xử lý va chạm bằng separate chaining — mỗi bucket chứa một danh sách liên kết (linked list) các entry.

Độ phức tạp thời gian của các thao tác hash table là gì?

Trung bình là O(1) cho insert, search và delete. Trường hợp xấu nhất là O(n) khi nhiều key cùng va chạm vào một bucket.

Công cụ này dùng hash function nào?

Một hash function modulo đơn giản, index = value % 7, để các bucket dễ theo dõi. Hash table thực tế dùng hàm băm mạnh hơn và tự resize để giữ chuỗi ngắn.