~/ 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/44a5afe7-0d82-43d1-81b6-e13cd9d4ab57/flashcard utf-8 LF