DSA, Database System & Operating System — Data Structure, Lists, Linked Lists and Trees, NEC licence examination syllabus (Nepal Engineering Council).
Neither is a new structure. Each is a list with a rule about which end you are allowed to touch — and the rule is the whole point.
RuleLast In, First Out. Add and remove at the SAME end.
Operationspush (add), pop (remove), peek (look without removing).
CostAll O(1) — one end, no shifting.
RuleFirst In, First Out. Add at one end (rear), remove from the other (front).
Operationsenqueue (add at rear), dequeue (remove from front).
CostO(1) each — if implemented carefully. See below.
The fix is a circular queue: when the rear pointer reaches the end of the array it wraps to the beginning, reusing the vacated slots. Both operations return to O(1), and no element ever moves.
Stack — anywhere the most recent thing must be handled first. Function calls, because the innermost call must return before its caller. Undo, because the last action is the one to reverse. Expression evaluation and bracket matching, because the most recent unclosed bracket is the one a closing bracket belongs to. Depth-first traversal, for the same reason.
Queue — anywhere fairness matters. Print jobs, process scheduling, and breadth-first traversal, where every node at one depth must be handled before any node deeper.
Stack the structure and the call stack. The second is an instance of the first — the memory region holding call frames, managed exactly LIFO.
Queue and priority queue. A queue serves in arrival order. A priority queue serves by priority regardless of arrival, and is usually a heap rather than a list.
Peek and pop. Peek reads the top; pop removes it.
Stack: one end, LIFO. Queue: two ends, FIFO.
A naive array queue is O(n) per dequeue or leaks slots — use a circular queue.
Full and empty look identical in a circular queue: keep a count or waste one slot.
Depth-first → stack. Breadth-first → queue.
Create a free account to tick topics off, take notes as you read, watch the video lessons and get a day-by-day study plan built around your exam date.
Loading…