Workspace Stacks And Queues
0d
DSA Cheat Sheets

Stack and queue operations

Library examples place two values into a FIFO queue and remove the older value.

Stack push / popOne active endUse for nested or deferred work
Queue enqueue / dequeueOpposite endsUse for layer-by-layer exploration
DequeBoth endsCan retain useful extremum candidates
Ring bufferAdvance index modulo capacityTrack size or reserve a slot to distinguish full and empty

Python

from collections import deque
q = deque()
q.append(3)
q.append(9)
first = q.popleft()  # 3

C++

#include <queue>
int firstInLine() {
  std::queue<int> q;
  q.push(3); q.push(9);
  int first = q.front(); q.pop(); return first;
}

Java

static int firstInLine() {
  java.util.ArrayDeque<Integer> q = new java.util.ArrayDeque<>();
  q.addLast(3); q.addLast(9);
  return q.removeFirst();
}