-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyStack.java
More file actions
51 lines (47 loc) · 1.21 KB
/
MyStack.java
File metadata and controls
51 lines (47 loc) · 1.21 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
public class MyStack {
int capacity = 3;
int stackArray[] = new int[capacity];
static int top = -1;
void push(int element) {
if (top < capacity-1) {
top = top + 1;
stackArray[top] = element;
} else {
System.out.println("It's overflowing");
}
}
int peek() {
if (top >= 0) {
return stackArray[top];
} else {
System.out.println("Exception! Array is empty in a sense");
}
return -1;
}
int pop() {
if (top >= 0) {
top = top - 1;
return stackArray[top];
} else {
System.out.println("Exception! Array is empty in a sense");
}
return -1;
}
public static void main(String[] args) {
MyStack obj = new MyStack();
obj.push(10);
obj.push(20);
obj.push(30);
//obj.push(40);
System.out.println(obj.peek());
obj.pop();
System.out.println(obj.peek());
obj.push(80);
System.out.println(obj.peek());
obj.pop();
System.out.println(obj.peek());
obj.push(70);
obj.push(90);
System.out.println(obj.peek());
}
}