-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstackArray.java
More file actions
58 lines (49 loc) · 1.08 KB
/
stackArray.java
File metadata and controls
58 lines (49 loc) · 1.08 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
import java.util.*;
public class stackArray{
private String[] s;
private int counter;
public stackArray(int size){
s = new String[size];
counter = 0;
}
public stackArray(){
this(10);
}
private boolean isEmpty(){
return counter == 0;
}
private boolean isFull(){
return counter == s.length;
}
public boolean push(String value){
if(!isFull()){
s[counter++] = value;
return true;
}else
return false;
}
public boolean pop(){
if(!isEmpty()){
s[counter - 1] = null;
counter --;
return true;
}else
return false;
}
public String peek(){
if(!isEmpty()){
return s[counter -1];
}
return null;
}
public void displayStack(){
if(!isEmpty()){
for(int i = counter - 1; i >= 0; i--){
System.out.println(" [ " + s[i] + " ] ");
}
}
else{
System.out.println(" Stack is Empty.");
}
}
}