-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInteractiveStack.java
More file actions
57 lines (46 loc) · 1.21 KB
/
InteractiveStack.java
File metadata and controls
57 lines (46 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
52
53
54
55
56
57
/*
Implement stack with user inputs for add and pop (add more methods)
*/
import java.lang.reflect.Array;
import java.util.ArrayList;
public class InteractiveStack {
protected int[] data;
private static final int default_size = 10;
int index = 0;
public InteractiveStack(){
this.data = new int[default_size];
}
public InteractiveStack(int size){
this.data = new int[size];
}
public void push(int num){
if(isFull()){
System.out.println("Stack is Full!");
return;
}
data[index] = num;
index++;
}
public int pop(){
if (isEmpty()) {
System.out.println("Stack is empty!");
}
int removed = data[index-1];
index--;
return removed;
}
public boolean isFull() {
return index == data.length;
}
public boolean isEmpty(){
return index == 0;
}
public String display(){
System.out.print("Current stack: ");
int[] display={};
for (int i = index-1; i >= 0; i--) {
System.out.print(+data[i]+"->");
}
return "END";
}
}