-
Notifications
You must be signed in to change notification settings - Fork 17
/
ArrayImplementation.java
62 lines (61 loc) · 1.48 KB
/
ArrayImplementation.java
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
60
61
62
package SummerTrainingGFG.Stack;
/**
* @author Vishal Singh
*/
public class ArrayImplementation {
static class Stack{
private final int size;
private final int[] arr;
private int top;
Stack(int size){
this.size = size;
arr = new int[size];
top = -1;
}
void push(int data){
if(top == size){
System.out.println("Overflow");
}
else{
arr[top] = data;
top++;
}
}
int pop(){
if(top == 0){
System.out.println("Underflow");
return -1;
}
else{
int res = arr[top-1];
top--;
return res;
}
}
void print(){
if(top == -1) {
System.out.println("No element present");
}
else{
for (int i = 0; i < top; i++) {
System.out.print(arr[i]+" ");
}
System.out.println("");
}
}
}
public static void main(String[] args) {
Stack s = new Stack(3);
s.push(5);
s.push(4);
s.push(3);
s.print();
s.push(31);
System.out.println(s.pop());
System.out.println(s.pop());
System.out.println(s.pop());
System.out.println(s.pop());
s.push(1);
s.print();
}
}