-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResizingArrayDeque
More file actions
100 lines (83 loc) · 2.37 KB
/
ResizingArrayDeque
File metadata and controls
100 lines (83 loc) · 2.37 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//file: ResizingArrayDeque.java
//author: Connor Maloney
//date: 2/10/2014
//
// This program implements the Deque array using a resizing array
public class ResizingArrayDeque<T> implements Deque<T>
{
private T[] d = (T[])new Object[1];
private int N = 0;
public int size() {return N;}
public boolean isEmpty() {return N == 0;}
private void resize(int max)
{
assert max >= N;
T[] temp = (T[]) new Object[max];
for (int i = 0; i < N; i++)
{
temp[i] = d[(0 + i) % d.length];
}
d = temp;
}
public String toString()
{
StringBuilder sb = new StringBuilder("The Deque has " + d.length + " spaces in it" );
return sb.toString();
}
public void pushRight(T t)
{
if(N == d.length)
resize(2*N);
d[N++] = t;
}
public T popLeft()
{
T t = d[0];
for(int i=1; i<N-1; i++){d[i-1] = d[i];}
d[--N] = null;
if(N > 0 && N == d.length/4){resize(d.length/2);}
return t;
}
public void pushLeft(T t)
{
if(N == d.length)
resize(2*N);
for(int i=N-1; i>0; i--)
{
d[i+1] = d[i];
}
d[0] = t;
N++;
}
public T popRight()
{
T t = d[--N];
d[N] = null;
if(N > 0 && N == d.length/4)
{
resize(d.length/2);
}
return t;
}
public static void main(String[] args)
{
Deque<String> d = new ResizingArrayDeque<String>();
String a = "Boston";
String c = "College";
System.out.println(d.isEmpty());
d.pushLeft(c);
d.pushRight(c);
d.pushLeft(c);
d.pushRight(a);
d.pushRight(a);
d.pushRight(a);
System.out.println(d.isEmpty());
System.out.println(d.popRight());
System.out.println(d.popLeft());
System.out.println(d.toString());
System.out.println(d.popLeft());
System.out.println(d.popRight());
System.out.println(d.popRight());
System.out.println(d.popRight());
}
}