-
Notifications
You must be signed in to change notification settings - Fork 0
/
DaltonStack.java
258 lines (202 loc) · 6.17 KB
/
DaltonStack.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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import java.util.ArrayList;
/*
This is the Dalton Stack assignment for the CSA class
Authors:
Xander Chase
Charles Forster
Jeffrey Stern
Emily Lovett
Jeffrey Stern
Taylor Wright
Harry DiPirinzio
Josh Kaye
Alex Mayer
Maya Klabin
Michael Dubin
Michael Zhao
/*delete all(Xander), substack(jeff, alex), convert(), delete one(michael, josh), reverse (taylor, harry), frequency(emily), length/size (maya), randomize(maya/dubin)*/
/*
DONE:
- Length
- Reverse
- Clear All
-Convert
*/
public class DaltonStack extends java.util.Stack
{
/*
Function: subStack
Description: takes a portion (from i to j) of the original stack
and returns that portion
Author: Jeffrey Stern
*/
public DaltonStack subStack (int i, int j)
{
DaltonStack topstack = new DaltonStack(); //creates a top stack
DaltonStack substack = new DaltonStack(); //creates the sub stack
//get the topstack off the mainstack:
for(int x=0; x<i; x++)
{
topstack.push(this.pop());
}
//creates the substack from i to j:
for (int c=i; c<=j; c++)
{
substack.push(this.pop());
}
substack.reverse();
//puts the topstack back into the original stack:
while(!topstack.isEmpty())
{
this.push(topstack.pop());
}
return substack;
}//subStack
/* Function: reveress the order of the stack
Description: reverses the order of the stack
Author: Harry DiPrinizio & Taylor Wright
Output: A stack w/ the contents reversed
Input: a Stack
*/
public void reverse()
{
java.util.Queue<Object> myQueue = new java.util.LinkedList<Object>();
//pop the stack into a queue (until it is empty):
while(!this.isEmpty())
{
myQueue.add(this.pop());
}
//get the queue into the stack
while(! myQueue.isEmpty())
{
this.push(myQueue.remove());
}
}
/*
Function: converts the Stack to a Queue
Description: Makes it so that the last item into the stack will be the first item out of the queue.
Author: Harry DiPrinizio & Taylor Wright
Output: a Queue w/ the contents of the stack
Input: a Stack
*/
public void convert()
{
java.util.Queue<Object> myQueue = new java.util.LinkedList<Object>();
//pop the stack into a queue (until it is empty):
while(!this.isEmpty())
{
myQueue.add(this.pop());
}
}
/*
Function: deleteTop
description: pops the top item and doesn't return it.
Author: Charlie Forster
Input: nothing
Output: nothing
Notes: This is an example from charlie
*/
public void deleteTop()
{
this.pop();
}
/* Function: shows length of stack
Description: Pops everything out of the stack onto a new stack, counting the items, and returns them to the original stack
Author: Maya Klabin
Input: Nothing
Output: The number of items in the stack
Notes:
*/
public int length()
{
return this.size();
}//length
/*
/*
Fuction: Randomize
Description: Changes the order of the items in the stack randomly
Author: Maya Klabin and Michael Dubin
Input: Nothing
Output: Nothing
*/
public void randomize()
{
ArrayList list = new ArrayList();
while (length() > 0)
{
list.add(this.pop());
}//converts the stack into the arraylist
java.util.Collections.shuffle(list);//randomizes the arraylist
while (!list.isEmpty())
{
this.push(list.get(0));
list.remove(0);
}//converts the randomized arraylist into a stack
}//randomize
public void clearAll()
{
int x = 0;
while (x<this.size())
{
this.pop();
x++;
}//ends while
}//ends clear all
/*
Function: DelOne
description: Deletes an item from anywhere in the stack & replaces all that were above it
Authors: Josh Kaye and Michael Zhao
Input: Desired item to delete
Output: none
Notes: none
*/
public void deleteOne(Object blah)
{//method
java.util.Stack popped = new java.util.Stack();
for (int i=0; i<this.size(); i++)
{//for
if (this.peek()!=blah)
{//if
popped.push(this.pop());
this.pop();
}//if
else
{//else
this.pop();
for (int j=0; j<popped.size(); j++)
{//for
this.push(popped.size()-j);
popped.pop();
}//for
}//else
}//for
}//method
/**
Authors: Emily and Alex
Input: Object number (user is searching for its frequency)
Output: int counter (# of times number appears)
Purpose: Find the frequency of number
**/
int frequency(Object number)
{
Object[] original_array = new Object[this.size()]; //new array that allows us to search through the data
int counter = 0; //incrementer
int temp_array = this.size(); //size of the stack, need it to stay constant
while(this.isEmpty() == false) //this pops everything in the stack and puts it in our new array
{
for (int i = 0; i < temp_array; i++)
{
original_array[i] = this.pop();
}//while
}//while
for (int x = 0; x<original_array.length; x++) //goes through the new array and counts the frequency, incrementing buddha
{
if(original_array[x]==number) counter++;
}//for
for (int j = temp_array-1; j >= 0; j--) //pushes everything back into the stack in original order
{
this.push(original_array[j]);
}//for
return counter;
}//frequency
}//class