-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChapter3.java
More file actions
280 lines (256 loc) · 5.3 KB
/
Copy pathChapter3.java
File metadata and controls
280 lines (256 loc) · 5.3 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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import java.util.*;
public class Chapter3 {
public static class Node<E>{
E value;
Node next;
public Node(E value){
this.value=value;
next=null;
}
public void insertNode(E v){
Node<E> n = new Node<E>(v);
Node<E> current = this;
while (current.next != null){
current = current.next;
}
current.next = n;
}
public Node<E> deleteNode(Node<E> head, E v){
Node<E> n = head;
if (n.value == v){
return head.next;
}
else {
while (n.next != null){
if (n.next.value == v){
n.next = n.next.next;
return head;
}
n = n.next;
}
}
return head;
}
}
public static class Stack<E> {
Node<E> top;
int size = 0;
void push(E item){
Node<E> t = new Node<E>(item);
t.next = top;
top = t;
size += 1;
}
E pop(){
if (top != null){
size -= 1;
Node<E> temp = top;
top = top.next;
return temp.value;
}
return null;
}
E peek(){
if (top != null){
return top.value;
}
return null;
}
}
public static class Queue<E> {
Node<E> first;
Node<E> last;
void enqueue(E input){
Node<E> n = new Node<E>(input);
if (first == null){
first = n;
}
if (last == null){
last = n;
}
else {
last.next = n;
last = last.next;
}
}
E dequeue(){
if (first == null){
return null;
}
E temp = first.value;
first = first.next;
return temp;
}
}
//Implements three stacks using one array
public static class ThreeStacks{
static final int stackSize = 300;
int[] buffer = new int[stackSize*3];
int[] stackPointers = {0,0,0};
void push(int stackNum, int input){
int stackP = stackSize*stackNum + stackPointers[stackNum] + 1;
stackPointers[stackNum] += 1;
buffer[stackP] = input;
}
int pop(int stackNum){
if (stackPointers[stackNum] == 0){
return 0;
}
int stackP = stackSize*stackNum + stackPointers[stackNum];
int val = buffer[stackP];
buffer[stackP] = 0;
stackPointers[stackNum] -= 1;
return val;
}
}
//Implements a stack that keeps track of the minimum element
public static class StackWithMin extends Stack<Integer>{
Stack<Integer> minStack;
int min(){
if (minStack.top == null){
return Integer.MAX_VALUE;
}
else return minStack.peek();
}
void push(int e){
if (minStack.top == null){
minStack.push(e);
}
else if (e<min()){
minStack.push(e);
}
super.push(e);
}
Integer pop(){
int value = super.pop();
if (value == min()){
minStack.pop();
}
return value;
}
}
public static class SetofStacks<E> {
ArrayList<Stack<E>> allStacks = new ArrayList<Stack<E>>();
int capacity;
int size = 0;
void push(E e){
Stack<E> currentStack = allStacks.get(size);
if (currentStack.size == capacity){
Stack<E> newStack = new Stack<E>();
newStack.push(e);
allStacks.add(newStack);
size += 1;
}
else {
currentStack.push(e);
}
}
E pop(){
E current = allStacks.get(size).pop();
if (current == null && size>0){
allStacks.remove(size);
size -= 1;
E e = allStacks.get(size).pop();
return e;
}
return current;
}
E popSpecific(int index){
if (index < 0 || index > size) return null;
if (index == size) pop();
Stack<E> specifiedStack = allStacks.get(index);
if (specifiedStack.size == 1){
E val = specifiedStack.pop();
for (int i = index; i<size; i++){
allStacks.set(i,allStacks.get(i+1));
}
allStacks.remove(size);
return val;
}
E val = specifiedStack.pop();
return val;
}
}
public void towersOfHanoi(Stack<Integer> first){
Stack<Integer> middle = new Stack<Integer>();
Stack<Integer> last = new Stack<Integer>();
solveHanoi(first,middle,last,first.size);
}
public void solveHanoi(Stack<Integer> first,Stack<Integer> middle, Stack<Integer> last, int index){
if (index == 1){
int finalElement = first.pop();
last.push(finalElement);
}
else if (index == 2){
int secondToLast = first.pop();
middle.push(secondToLast);
int firstLast = first.pop();
last.push(firstLast);
last.push(secondToLast);
}
else {
solveHanoi(first,last,middle,index-1);
int lastElement = first.pop();
last.push(lastElement);
solveHanoi(middle, first, last,index-1);
}
}
public static class QueueWithStacks<E> {
Stack<E> s1;
Stack<E> s2;
void push(E e){
if (s1.size == 0){
s2.push(e);
}
else s1.push(e);
}
E pop(){
if (s1.size == 0){
while (s2.size>0){
E element = s2.pop();
s1.push(element);
}
E result = s1.pop();
return result;
}
else {
while (s1.size>0){
E element = s1.pop();
s2.push(element);
}
E result = s2.pop();
return result;
}
}
E peek(){
if (s1.size == 0){
while (s2.size>0){
E element = s2.pop();
s1.push(element);
}
return s1.peek();
}
else {
while (s1.size>0){
E element = s1.pop();
s2.push(element);
}
return s2.peek();
}
}
}
public static Stack<Integer> sortStack(Stack<Integer> input){
Stack<Integer> buffer = new Stack<Integer>();
int n = input.size;
if (n == 0 || n == 1) return input;
buffer.push(input.pop());
while (buffer.size < n) {
int i = input.pop();
while (i<buffer.peek()){
input.push(buffer.pop());
}
buffer.push(i);
}
return buffer;
}
}