-
Notifications
You must be signed in to change notification settings - Fork 0
/
IntStack.java
executable file
·174 lines (140 loc) · 3.04 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
import java.util.*;
import java.util.Arrays;
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(is.isEmpty());
System.out.println(is.pop());
System.out.println(is.peek());
//kate's tests:
IntStack kate = new IntStack();
kate.push(4);
kate.push(5);
kate.push(6);
kate.push(7);
kate.print();
// Sam's test code
IntStack sam = new IntStack();
System.out.println(sam.isEmpty());
sam.push(new int[] {1, 2 , 3});
System.out.println(sam.peek());
//Test for size:
System.out.println(is.size());
//test for peek @ depth:
System.out.println(is.peek(0));
is.resize();
System.out.println(is.size());
}
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[] temp = new int[100*stack.length];
for (int i = 0; i<stack.length; i++) {
temp[i] = stack[i];
stack = temp;
}
}
/*
how large is the stack?
*/
public int size() {
//returns the top of the array which will be the top
return top;
}
/*
sort the contents of the stack
*/
public void sort() {
Arrays.sort(stack);
System.out.println(Arrays.toString(stack));
}
/*
print the Stack pretty-like
*/
public void print() {
for (int i = top;i>0;i--){
System.out.println("|" + peek() + "|");
pop();
}
System.out.println("----------");
}
/*
return the item depth distance from the top
*/
public int peek(int depth) {
return stack[top-depth-1];
}
/*
return multiple items from the top in a new array
*/
public int[] pop(int multiple) {
return null;
}
/*
push multiple items onto the stack
*/
public void push(int[] nums) {
for (int i = 0; i < nums.length; i++) {
push(nums[i]);
}
}
/*
how many [num]'s are n the stack?
*/
public int count(int num) {
return 0;
}
/*
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) {
}
}