- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomStack.java
More file actions
Latest commit
43 lines (36 loc) · 991 Bytes
/
Copy pathCustomStack.java
File metadata and controls
43 lines (36 loc) · 991 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
packagecom.sarvesh.javabasics;
publicclassCustomStack {
privateint[] arr;
privateinttop;
privateintcapacity;
publicCustomStack(intsize) {
this.arr = newint[size];
this.capacity = size;
this.top = -1;
}
publicvoidpush(intx) {
if (top == capacity - 1) {
System.out.println("System Crash: Stack Overflow");
return;
}
arr[++top] = x;
}
publicintpop() {
if (top == -1) {
System.out.println("System Crash: Stack Underflow");
return -1;
}
returnarr[top--];
}
publicintpeek() {
if (top == -1) return -1;
returnarr[top];
}
publicstaticvoidmain(String[] args) {
CustomStackengine = newCustomStack(5);
engine.push(10);
engine.push(20);
System.out.println("Popped: " + engine.pop());
System.out.println("Peek: " + engine.peek());
}
}