-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackArray.java
More file actions
53 lines (45 loc) · 1.02 KB
/
Copy pathStackArray.java
File metadata and controls
53 lines (45 loc) · 1.02 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: Rafael Neves Moraes
*
* Description: Stack algorithm Object Implementation.
*
* Written: 5/06/2019
*
* % javac StackArray.java
* % java StackArray
* Uses between ~ 8 N and ~ 32 N bytes to represent a stackwith N items
*
**************************************************************************** */
public class StackArray<Item>
{
private Item[] s;
private int N = 0;
public FixedCapacityStackOfStrings(int capacity)
{
s = new Item[capacity];
}
public boolean isEmpty()
{
return N == 0;
}
public void push(Item item)
{
if (N == s.length) resize(2 * s.length);
s[N++] = item;
}
public Item pop()
{
Item item = s[--N];
s[N] = null;
return item;
}
public ResizingArrayStackOfStrings()
{
s = new Item[1];
}
private void resize(int capacity)
{
Item[] copy = new Item[capacity];
for (int i = 0; i < N; i++) copy[i] = s[i];
s = copy;
}
}