A collection that provides last-in first-out access (LIFO) to elements.
Pros | Cons |
---|---|
Useful to handle recursive flows | Limited access patterns |
s = deque()
s.append(1);
s.append(2); // [1, 2]
s.append(3); // [1, 2, 3]s.pop(); // [1, 2]
s.pop(); // [1]
std::stack s;
s.push(1); // [1]
s.push(2); // [1, 2]
s.push(3); // [1, 2, 3]s.pop(); // [1, 2]
s.pop(); // [1]
Stack<Integer> s = new Stack<>();
s.push(1); // [1]
s.push(2); // [1, 2]
s.push(3); // [1, 2, 3]s.pop(); // [1, 2]
s.pop(); // [1]
Operations | Worst | Average |
---|---|---|
Delete (Pop) | O(1) | |
Insert (Push) | O(1) |