-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntDeque.java
executable file
·192 lines (155 loc) · 3.18 KB
/
IntDeque.java
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import com.sun.org.apache.xalan.internal.xsltc.util.IntegerArray;
import java.util.Arrays;
public class IntDeque {
public static void main(String[] args) {
//ava testing
IntDeque ava = new IntDeque(14);
ava.putFirst(3);
ava.putFirst(4);
ava.putFirst(5);
ava.putFirst(8);
//ryan putboth testing
System.out.println(Arrays.toString(ava.q));
ava.putBoth(1);
ava.putBoth(2);
System.out.println(Arrays.toString(ava.q));
System.out.println(ava.getFirst());
//Claire's Testing Area:
IntDeque cla = new IntDeque(2);
cla.putLast(1);
cla.putLast(2);
cla.putLast(3);
cla.putLast(4);
cla.putLast(5);
//cheat print:
cla.print();
//Ryo tests:
IntDeque ryo = new IntDeque(10);
ryo.q[0]=9;
System.out.println(ryo.peekFirst());
//Jared test
IntDeque jared = new IntDeque(5);
jared.putFirst(2);
jared.putFirst(1);
System.out.println("JARED TEST");
System.out.println(jared.peekLast());
}
int[] q;
int head, tail;
public IntDeque(int defaultsize) {
q = new int[defaultsize];
head=defaultsize-1;
tail=0;
}
/*
put the first item
*/
public void putFirst(int item) {
q[head] = item;
head--;
if (head==-1) head = q.length-1;
if (head==tail) resize();
}
/*
put the last item
*/
public void putLast(int item) {
q[tail] = item;
tail++;
if (tail==q.length) tail=0;
if (tail == head) resize();
}
/*
return the first item
*/
public int peekFirst() {
return q[head];
}
/*
return the last item
*/
public int peekLast() {
return q[tail];
}
/*
get the first item
*/
public int getFirst(){
return q[++head];
}
/*
get the last item
*/
public int getLast() { return 0; }
/*
move num items from first to last
*/
public void circ(int num) {
}
public void resize() {
//needs to be done!
int[] resizedArr = new int[q.length*2];
int newarr = 0;
for (int i = head; i < q.length; i++) {
resizedArr[newarr++] = q[i];
}
for (int i = 0; i < tail; i++) {
resizedArr[newarr++] = q[i];
}
//set head and tail:
tail = newarr;
head = 0;
//reassign:
q=resizedArr;
print();
}
/*
return a Stack from this deque
*/
public IntStack toStack() {
return null;
}
/*
multiply every two items in the queue together,
such that the array is half the size
*/
public void multQueue() {
}
/*
return a Deque which includes only every odd item in this queue
*/
public IntDeque oddQueue() {
return null;
}
/*
print the Deque to the screen
*/
public void print() {
System.out.println(Arrays.toString(q));
int i = head;
System.out.print("HEAD > ");
while(i!=tail) {
System.out.print(q[i++] + " > " );
if(i==q.length) i=0;
}
System.out.println("TAIL");
}
/*
add an item to the middle
*/
public void middle(int i) {
}
/*
put an item on both sides
*/
public void putBoth(int i) {
this.putFirst(i );
this.putLast(i);
}
/*
get an item from both sides
*/
public int[] getBoth() {
return null;
}
}