-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayStackOfIntegers.java
More file actions
68 lines (62 loc) · 1.6 KB
/
ArrayStackOfIntegers.java
File metadata and controls
68 lines (62 loc) · 1.6 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
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* Implement the ArrayStack ADT using arrays. Look at the API in the class
* website. Design and implement a client to calculate the following postfix
* expression: 8 4 -3 * 1 5 + / *
*
* @mseskar
* @10/10/17
*/
public class ArrayStackOfIntegers implements Iterable {
private Integer[] items; // holds the items
private int n; // number of items in stack
//creates new stack of capca
public ArrayStackOfIntegers(int capacity) {
this.n = 0;
this.items = new Integer[capacity];
}
//is the stack empty?
public boolean isEmpty() {
return this.n == 0;
}
//is the stack full?
public boolean isFull() {
return this.n == this.items.length;
}
public void push(Integer item) {
if (!this.isFull()) {
items[n] = item;
n++;
return;
}
System.out.println("Unable to add " + item.toString()
+ " to ArrayStackOfIntegers because it is full.");
return;
}
public Integer pop() {
n--;
return items[n];
}
public Iterator iterator() {
return new ReverseArrayIterator();
}
//mseskar
//10/10/17
private class ReverseArrayIterator implements Iterator<Integer> {
int pos;
public ReverseArrayIterator() {
pos = n - 1;
}
public boolean hasNext() {
return pos != -1;
}
@Override
public Integer next() {
pos--;
return items[pos + 1];
}
public void remove(){
}
}
}