-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathStack.java
More file actions
50 lines (43 loc) · 903 Bytes
/
Stack.java
File metadata and controls
50 lines (43 loc) · 903 Bytes
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
class Stack {
int[] arr;
int top;
static int Max = 100;
Stack() {
arr=new int[Max];
top = -1;
}
void push(int val) {
if (top == Max - 1) {
System.out.println("Stack overflow");
} else {
arr[++top] = val;
}
}
int pop() {
if (top == -1) {
System.out.println("Stack underflow");
return 0;
} else {
int x = arr[top--];
return x;
}
}
boolean isEmpty() {
return top < 0;
}
void display() {
while (!isEmpty()) {
System.out.println(arr[top] + "\t");
top--;
}
}
public static void main(String[] args) {
Stack s1=new Stack();
s1.push(10);
s1.push(20);
s1.push(30);
s1.push(40);
s1.pop();
s1.display();
}
}