Junior — Middle
Can you explain the concept of a stack and its main properties?
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 the first to be removed. The main properties of a stack:
- The push operation adds an element to the top of the stack.
- The pop operation removes and returns the top element.
- The peek (or top) operation allows viewing the top element without removing it.
An example of using a stack is undoing actions in an application, where the most recent action is undone first.