-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram100.java
More file actions
59 lines (45 loc) · 1.1 KB
/
Program100.java
File metadata and controls
59 lines (45 loc) · 1.1 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
53
54
55
56
57
58
59
class Node {
int data;
Node next;
Node(int data) {
this.data = data;
next = null;
}
}
class LinkedListStack {
Node top;
void push(int data) {
Node node = new Node(data);
node.next = top;
top = node;
}
int pop() {
if (top == null) {
System.out.println("Underflow");
return -1;
}
int result = top.data;
top = top.next;
return result;
}
int peek() {
if (top == null) {
return -1;
}
return top.data;
}
boolean isEmpty() {
return top == null;
}
}
public class Program100 {
public static void main(String[] args) {
LinkedListStack stack = new LinkedListStack();
stack.push(10);
stack.push(20);
stack.push(30);
System.out.println("Top element: " + stack.peek()); // 30
System.out.println("Removed: " + stack.pop()); // 30
System.out.println("Is stack empty? " + stack.isEmpty()); // false
}
}