Junior — Middle
Millist teavet sisaldab andmestruktuur virn?
sobes.tech AI
Vastus AI-lt
A stack is a data structure that stores elements in LIFO (Last In, First Out) order, meaning the last added element is the first to be removed. A stack contains a collection of elements where two main operations can be performed:
- push — add an element to the top of the stack;
- pop — remove and retrieve the element from the top of the stack.
Additionally, the peek (or top) operation is often implemented, which allows viewing the top element without removing it.
Example of using a stack in Swift:
var stack = [Int]()
stack.append(10) // push
stack.append(20)
let top = stack.last // peek, top = 20
let popped = stack.popLast() // pop, popped = 20
Thus, a stack contains an ordered collection of elements with access only to the last added element.