-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntStack.java
More file actions
221 lines (180 loc) · 3.47 KB
/
Copy pathIntStack.java
File metadata and controls
221 lines (180 loc) · 3.47 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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import java.util.Scanner;
//CHANGE
class IntStack
{
//todo:
//write resize
//declarations:
int[] stack;
int top=0;
IntStack(int size)
{
stack = new int[size];
}
int pop()
{
if(isEmpty()) return -1;
return stack[--top];
}
void push(int i)
{
stack[top++]=i;
if(top==stack.length) resize();
}
int peek()
{
if(isEmpty()) return -1;
return stack[top-1];
}
boolean isEmpty()
{
return top==0;
}
private void resize()
{
int[] newStack = new int[stack.length*2];
for(int i= 0; i< top; i++) {
newStack[i]=stack[i];
}
//System.out.println("resizing...");
stack=newStack;
}
/*
* Size
* gives the current size of the stack.
* @author Charlie Forster
*/
int size()
{
return top-1;
}
/*
* Mode
* Gives the number that occurs most often.
* If given paramater returns the number of times that number occurs in the stack.
* This is achieved by overloading the method.
* @author Zachary Buttenwieser
* @author Adam Phillips
* @param num
*/
int mode()
{
int maxValue, maxCount;
maxValue = -1;
maxCount = -1;
for (int i = 0; i < stack.length; ++i)
{
int count = 0;
for (int j = 0; j < stack.length; ++j)
{
if (stack[j] == stack[i]) ++count;
}
if (count > maxCount)
{
maxCount = count;
maxValue = stack[i];
}
}
return maxValue;
}
int mode(int num)
{
int counter = 0;
for (int i = 0; i<top; i++)
{
if(stack[i]==num)
{
counter++;
}
}
return counter;
}
/*
* Peek
* Shows a number at a specific position.
* Given depth paramter, finds and returns the number at that position
* @author Grace Monk
* @author Elana Simon
* @param depth
*/
int peek(int depth)
{
if(depth > top) return -1;
//System.out.println("Here");
//System.out.println(top);
//for (int i = 0; i < stack.length; i++) {
// System.out.println(stack[i]);
//}
return stack[depth];
}//close method
//popUntil - Aleks Kiprovski, Matt Durkin, Richard Chen
void popUntil(int f)
{
int position = 0;
while (peek () != f)
{
pop();
}
}
double mean()
{
double tot=0;
for(int i=0; i < top; i++)
{
tot += stack[i];
}
tot = tot/top;
return tot;
}
/*
* le ginger's work
*
*/
/*
* PopAll
* Pops the entire stack
* @author Allegra Simon and Alex Fisher
*/
int[] popAll()
{
int[] fullStack = new int[this.size()];
for(int i=0; i<this.size(); i++)
{
fullStack[i] = stack[i];
this.pop();
}
return fullStack;
}
/*
* Casey, Sonia, Jenna, Rachel
* Searches through the stack for certain number.
* Return: true or false.
*/
boolean peekNum(int num)
{
boolean peekNum = false;
for(int i = 0; i<stack.length; i++)
if(stack[i]==num)
peekNum = true;
return peekNum;
}
/*
* Pop X number of ints
* Ask for X
* Run pop X number of times
* Move the popped ints into new array
* return new array
* @author James Shao
* @author Ben Ginzberg
*/
int[] pop(int x)
{
int[] popX = new int[x]; //set return array length to equal number of items popped
for(int i=0; i<x; i++) //run loop x number of times
{
popX[i]=stack[i]; //put pop int into return array
this.pop();
}
return popX;
}
}