-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomStack.java
More file actions
86 lines (73 loc) · 1.75 KB
/
CustomStack.java
File metadata and controls
86 lines (73 loc) · 1.75 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package com.company;
public class CustomStack {
private int[] stackArray;
private int top = 0;
private int minElement;
private int stackSize;
public CustomStack(int stackSize){
this.stackSize = stackSize;
this.stackArray = new int[stackSize];
this.top = 0;
}
public int peek(){
if(top > 0) {
return this.stackArray[top - 1];
}
return -1;
}
public void push(int num){
if(this.empty()){
minElement = num;
}
if(top < stackSize){
stackArray[top] = num;
top++;
}
else{
System.out.println("CustomStack Overflow");
}
if(num < minElement){
minElement = num;
}
}
public int min(){
if(this.empty()){
System.out.println("Empty Stack");
return -1;
}
else {
return this.minElement;
}
}
public boolean empty(){
if(top == 0){
return true;
}
else{
return false;
}
}
public int pop(){
int topNum = stackArray[top];
stackArray[top] = -1;
top--;
return topNum;
}
public static void main(String[] args){
CustomStack stack = new CustomStack(5);
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(-9999);
System.out.println(stack.peek());
stack.pop();
System.out.println(stack.peek());
stack.pop();
System.out.println(stack.peek());
stack.pop();
System.out.println(stack.peek());
System.out.println(stack.min());
stack.pop();
System.out.println(stack.min());
}
}