Junior — Middle
What information is usually stored in a stack data structure?
sobes.tech AI
Answer from AI
A stack is a data structure that operates on the Last In, First Out (LIFO) principle, meaning the last added element is retrieved first.
Typically, data that needs to be processed in reverse order is stored in a stack, for example:
- Function calls and their local variables (call stack).
- Operands and intermediate calculation results.
- Return addresses during program jumps.
Example: When calling a function in C#, the return address and parameters are placed on the stack, and when the function finishes, this data is retrieved.
Example code using a stack in C#:
Stack<int> stack = new Stack<int>();
stack.Push(10);
stack.Push(20);
int top = stack.Pop(); // top = 20