-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack using Deque in Java
More file actions
52 lines (43 loc) · 1.24 KB
/
Stack using Deque in Java
File metadata and controls
52 lines (43 loc) · 1.24 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
41
42
43
44
45
46
47
48
49
50
51
52
import java.util.ArrayDeque;
import java.util.Deque;
public class StackUsingDeque {
Deque<Integer> deque;
public StackUsingDeque() {
deque = new ArrayDeque<>();
}
public void push(int data) {
deque.addLast(data);
}
public int pop() {
if (deque.isEmpty()) {
System.out.println("Stack is empty");
return -1;
}
return deque.removeLast();
}
public int peek() {
if (deque.isEmpty()) {
System.out.println("Stack is empty");
return -1;
}
return deque.getLast();
}
public boolean isEmpty() {
return deque.isEmpty();
}
public void display() {
System.out.println("Stack Content: " + deque);
}
public static void main(String[] args) {
StackUsingDeque stack = new StackUsingDeque();
stack.push(3);
stack.push(4);
stack.push(2);
stack.push(1);
stack.push(10);
stack.display(); // Output: Stack Content: [3, 4, 2, 1, 10]
System.out.println("Pop: " + stack.pop()); // Output: Pop: 10
stack.display(); // Output: Stack Content: [3, 4, 2, 1]
System.out.println("Peek: " + stack.peek()); // Output: Peek: 1
}
}