Описание тега stack
A stack is a data structure, often used as an abstract data type (ADT) in many programming languages, to store data in a last in, first out (LIFO) manner. You could imagine it like a stack of cards. If you put a card onto the top of the stack, and then take the top card off the stack, you'll get the same card you put onto it.
Putting data onto a stack is called pushing. Taking something from a stack is called popping. Checking what's on top of a stack without removing it is called peeking.
Here is an example program:
a = new stack()
a.push(5)
b = a.peek()
print(b)
a.push(6)
b = a.pop()
print(b)
b = a.pop()
print(b)
would give the output:
5
6
5
The callstack is a stack data structure that's used to keep track of local variables and allows easy nested function calls (including for recursive functions). The current top-of-stack is usually tracked by a dedicated stack-pointer register. Different CPU architectures and calling conventions manage the stack differently, but it's common to push a return address as part of calling a function. Returning pops the return address and jumps to it, allowing a function to return to wherever it was called from.
Related tags:
- stack-overflow an error from using
push()
on a full stack - stackunderflow an error from using
pop()
on an empty stack