-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomStack.java
More file actions
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
package com.sarvesh.javabasics;
public class CustomStack {
private int[] arr;
private int top;
private int capacity;
public CustomStack(int size) {
this.arr = new int[size];
this.capacity = size;
this.top = -1;
}
public void push(int x) {
if (top == capacity - 1) {
System.out.println("System Crash: Stack Overflow");
return;
}
arr[++top] = x;
}
public int pop() {
if (top == -1) {
System.out.println("System Crash: Stack Underflow");
return -1;
}
return arr[top--];
}
public int peek() {
if (top == -1) return -1;
return arr[top];
}
public static void main(String[] args) {
CustomStack engine = new CustomStack(5);
engine.push(10);
engine.push(20);
System.out.println("Popped: " + engine.pop());
System.out.println("Peek: " + engine.peek());
}
}