-
Notifications
You must be signed in to change notification settings - Fork 0
/
IntStack.java
executable file
·207 lines (169 loc) · 4.11 KB
/
IntStack.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
import java.util.ArrayList;
public class IntStack {
public static void main(String[] args) {
IntStack is = new IntStack();
System.out.println(is.isEmpty());
is.push(3);
is.push(4);
is.push(5);
System.out.println("size: " + is.size());
System.out.println(is.isEmpty());
System.out.println(is.pop());
System.out.println(is.peek());
//print testing
System.out.println("Print");
IntStack printStack = new IntStack();
printStack.push(3);
printStack.push(4);
printStack.push(5);
printStack.push(50);
printStack.push(54);
printStack.push(154);
printStack.print();
//Henry's Resize test code
IntStack test = new IntStack();
System.out.println(test.isEmpty());
for (int i = 0; i < 204; i++) {
test.push(1);
}
System.out.print("Resized");
//Count testing
//stack count works!
is.push(4);
System.out.println(is.count(4));
//testing code for popping multiple items
IntStack mine = new IntStack();
mine.push(3);
mine.push(4);
mine.push(5);
int[] tester = mine.pop(2);
for (int i = 0; i < tester.length; i++) {
System.out.println(tester[i]);
}
IntStack mine2 = new IntStack();
mine2.push(3);
mine2.push(4);
mine2.push(5);
int[] tester2 = mine2.pop(4);
for (int i = 0; i < tester2.length; i++) {
System.out.println(tester2[i]);
}
}
int[] stack;
int top;
public IntStack() {
stack = new int[100];
top = 0;
}
boolean isEmpty() {
return top==0;
}
void push(int i) {
if(top==stack.length) resize();
stack[top++]=i;
}
int pop() {
if(!isEmpty()) return stack[--top];
return -1;
}
int peek() {//sometimes
if(!isEmpty()) return stack[top-1];
return -1;
}
/*
make a new larger implementing array
*/
private void resize() {
int[] biggerstack = new int[stack.length*2];
for (int i = 0; i < stack.length; i++) {
biggerstack[i] = stack[i];
}
stack = biggerstack;
}
/*
how large is the stack?
*/
public int size() {
return top;
}
/*
sort the contents of the stack
*/
public void sort() {
//String[] arr = new String[stack.size()];
//arr.sort();
}
/*
print the Stack pretty-like
*/
public void print() {
for (int i = top -1; i >= 0; i--) {
if (stack[i]<10)System.out.println("| " + stack[i] + " |");
else if (stack[i]> 100)System.out.println("| " + stack[i] + " |");
else System.out.println("| " + stack[i] + " |");
}
System.out.println("|_____|");
}
/*
return the item depth distance from the top
*/
public int peek(int depth) {
return stack[top-depth];
}
/*
return multiple items from the top in a new array
*/
public int[] pop(int multiple) {
int[] answer = new int[multiple];
if(multiple>(top)) answer = new int[top];
for (int i = 0; i < multiple; i++) {
if(!isEmpty()) answer[i] = (stack[--top]);
if(top<0) return answer;
}
return answer;
}
/*
push multiple items onto the stack
*/
public void push(int[] nums) {
}
/*
how many [num]'s are n the stack?
*/
public int count(int num) {
int counter = 0;
for(int i = 0; i < stack.length; i ++){
if (stack[i] == num){
counter++;
}
}
return counter;
}
/*
remove depth items
*/
public void dump(int depth) {
}
/*
return the contents of the stack as an array
*/
public int[] toArray() {
return null;
}
/*
make the bottom of the stack the top
*/
public void flip() {
}
/*
return a new stack that includes the top size items.
*/
public IntStack subStack(int size) {
return null;
}
/*
divide every item in the stack by mult
*/
public void divStack(int mult) {
}
}