~/ learn/ comp-372/ cards/ Data Structures Supporting Algorithms
1 of 28

PUSH and POP on an array-backed stack (CLRS, 1-indexed)

PUSH and POP on an array-backed stack (CLRS, 1-indexed)

Answer

PUSH(S, x): S.top = S.top + 1 S[S.top] = x POP(S): S.top = S.top - 1 return S[S.top + 1]

PUSH increments top then stores; POP decrements then returns the slot just above the new top. Both are O(1) — a stack is among the cheapest structures there is.

space flip · ← → navigate · esc to exit
NORMAL ~/memra/library/64b0e3c9-468d-4ffb-aad0-ccc23b210922/flashcard utf-8 LF