-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.java
More file actions
40 lines (31 loc) · 1.41 KB
/
Stack.java
File metadata and controls
40 lines (31 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// You will need to provide a package name (usually folder name) like the one below.
import java.util.ArrayDeque;
import java.util.Deque;
// A stack is a LIFO (last-in-first-out) data structure.
// It is simple and commonly used for things like keeping
// track of data, memory stacks, browser history, undo/redo,
// backtracking, syntax parsing, string reversals, parentheses matching,
// task management, and serves as a foundation for implementing algorithms.
// Prefer using `Deque` instead of `Stack` class in Java.
// Must know operations:
// push/addFirst: O(1) - push an element to the top of the stack
// pop/removeFirst: O(1) - remove an element from the top of the stack
// peek/peekFirst: O(1) - view the element at the top of the stack
public class Stack {
// A `main` function is required.
public static void main(String[] args) {
// Instantiate a stack with no size limit.
Deque<Integer> stack = new ArrayDeque<>();
stack.push(1);
stack.push(2);
stack.push(3);
System.out.printf("Is stack empty? %s\n", stack.isEmpty());
System.out.printf("The top element is %s\n", stack.peek());
stack.pop();
stack.pop();
System.out.printf("Is stack empty? %s\n", stack.isEmpty());
System.out.printf("The top element is %s\n", stack.peek());
stack.pop();
System.out.printf("Is stack empty? %s\n", stack.isEmpty());
}
}