跳到主要内容
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

什么是哈希表?

哈希表是一种将键值对存储在多个桶(bucket)组成的数组中的数据结构,它使用哈希函数计算每个键对应的桶索引,从而使插入、查找和删除操作能够达到接近常数级的速度。

什么是哈希冲突(hash collision)?

哈希冲突是指两个不同的键被映射到同一个桶中的情况。本工具通过链地址法(separate chaining)处理冲突——每个桶内维护一个存放条目的链表(linked list)。

哈希表各项操作的时间复杂度是多少?

插入、查找和删除的平均时间复杂度均为 O(1)。当大量键冲突集中到同一个桶时,最坏情况下会达到 O(n)。

本工具使用的是哪种哈希函数?

为了便于观察,本工具使用了一个简单的取模哈希函数:index = value % 7。实际生产环境中的哈希表会采用更强的哈希算法,并自动扩容以保持链表长度较短。