-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackArray.java
More file actions
58 lines (47 loc) · 1.11 KB
/
StackArray.java
File metadata and controls
58 lines (47 loc) · 1.11 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
public class StackArray{
private String[] s;
private int count;
public StackArray(int size){
if(size > 0)
s = new String[size];
else
s = new String[10];
count = 0;
}
public StackArray(){
this(10);
}
private boolean isEmpty(){
return count == 0;
}
public boolean isFull(){
return count == s.length;
}
public boolean push(String val){
if(!isFull()){
s[count++] = val;
return true;
}
return false;
}
public boolean pop(){
if(!isEmpty()){
s[count - 1] = null;
count--;
return true;
}
return false;
}
public String peek(){
return isEmpty()? null : s[count-1];
}
public void display(){
if(!isEmpty()){
for(int i = count - 1; i >= 0; i--){
System.out.println("[" + s[i] + "]");
}
}
else
System.out.println("Stack is empty");
}
}