Junior — Middle
Can you explain what is meant by the term 'stack' in the context of programming?
sobes.tech AI
Answer from AI
In programming, a "stack" is a data structure that operates on the Last In, First Out (LIFO) principle, meaning the last element added is the first to be removed.
A stack can be visualized as a stack of plates: you place a new plate on top and also remove from the top.
Main operations with a stack:
- push — add an element to the top of the stack
- pop — remove and return the top element
- peek (top) — view the top element without removing it
Example in C#:
Stack<int> stack = new Stack<int>();
stack.Push(1);
stack.Push(2);
int top = stack.Pop(); // top = 2
Stacks are widely used for managing function calls, expression processing, and in algorithms.