-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFixedCapacityStackOfStrings.java
More file actions
53 lines (44 loc) · 1.28 KB
/
FixedCapacityStackOfStrings.java
File metadata and controls
53 lines (44 loc) · 1.28 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
/* *****************************************************************************
* Name: Ada Lovelace
* Coursera User ID: 123456
* Last modified: October 16, 1842
**************************************************************************** */
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;
public class FixedCapacityStackOfStrings {
private String[] a;
private int N;
public FixedCapacityStackOfStrings(int cap) {
a = new String[cap];
}
public boolean isEmpty() {
return N == 0;
}
public int size() {
return N;
}
public boolean isFull() {
return N == a.length;
}
public void push(String item) {
a[N++] = item;
}
public String pop() {
return a[--N];
}
//client
public static void main(String[] args) {
FixedCapacityStackOfStrings s;
s = new FixedCapacityStackOfStrings(10);
while (!StdIn.isEmpty()) {
String item = StdIn.readString();
if (!item.equals("-") && !s.isFull()) {
s.push(item);
}
else if (!s.isEmpty()) {
StdOut.println(s.pop() + " ");
}
}
StdOut.println(s.size() + " left on stack");
}
}