Skip to content

Commit 06c4a7b

Browse files
author
Tony K Tan
committed
(attempt) 3.3 - 3.5
1 parent f9e7de1 commit 06c4a7b

File tree

3 files changed

+206
-0
lines changed

3 files changed

+206
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
var SetOfStacks = function(capacity) {
2+
// implement as an array of stacks
3+
this.capacity = capacity;
4+
this.stackSet = [];
5+
};
6+
7+
SetOfStacks.prototype.push = function(value) {
8+
if (this.stackSet.length === 0 || this.stackSet[this.stackSet.length - 1].length === this.capacity) {
9+
var newStack = [];
10+
newStack.push(value);
11+
this.stackSet.push(newStack);
12+
} else {
13+
this.stackSet[this.stackSet.length - 1].push(value);
14+
}
15+
};
16+
17+
SetOfStacks.prototype.pop = function() {
18+
if (this.numStack === 0) {
19+
return undefined;
20+
} else if (this.stackSet[this.stackSet.length - 1].length === 0) {
21+
this.stackSet.pop();
22+
}
23+
return this.stackSet[this.stackSet.length - 1].pop();
24+
};
25+
26+
SetOfStacks.prototype.peek = function() {
27+
var currStack = this.stackSet[this.stackSet.length - 1];
28+
return currStack[currStack.length - 1];
29+
};
30+
31+
SetOfStacks.prototype.isEmpty = function() {
32+
return this.stackSet.length === 0;
33+
};
34+
35+
SetOfStacks.prototype.popAt = function(index) {
36+
return this.stackSet[index].pop();
37+
};
38+
39+
/* TESTS */
40+
41+
var s = new SetOfStacks(3);
42+
s.push(1);
43+
s.push(2);
44+
s.push(3);
45+
s.push(4);
46+
s.push(5);
47+
s.push(6);
48+
s.push(7);
49+
s.push(8);
50+
s.push(9);
51+
s.push(10);
52+
s.push(11);
53+
s.push(12);
54+
s.push(13);
55+
s.push(14);
56+
57+
console.log(s.stackSet);
58+
59+
s.popAt(2);
60+
61+
console.log(s.stackSet);
62+
63+
s.pop();
64+
s.pop();
65+
s.pop();
66+
s.pop();
67+
s.pop();
68+
s.pop();
69+
s.pop();
70+
s.pop();
71+
s.pop();
72+
73+
console.log(s.stackSet);
74+
75+
76+
// Note: if stack not implemented as an array, would need to separately keep track of the depth
77+
// of each stack in an array
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
var Stack = require('./../util/Stack');
2+
3+
var myQueue = function() {
4+
this.front = new Stack();
5+
this.back = new Stack();
6+
this.backUp = true;
7+
};
8+
9+
myQueue.prototype.add = function(value) {
10+
if (!this.backUp) {
11+
while (!this.front.isEmpty()) {
12+
this.back.push(this.front.pop());
13+
}
14+
this.backUp = true;
15+
}
16+
this.back.push(value);
17+
};
18+
19+
myQueue.prototype.remove = function() {
20+
if (this.backUp) {
21+
while(!this.back.isEmpty()) {
22+
this.front.push(this.back.pop());
23+
}
24+
this.backUp = false;
25+
}
26+
return this.front.pop();
27+
};
28+
29+
myQueue.prototype.peek = function() {
30+
if (this.backUp) {
31+
while(!this.back.isEmpty()) {
32+
this.front.push(this.back.pop());
33+
}
34+
this.backUp = false;
35+
}
36+
return this.front.peek();
37+
};
38+
39+
myQueue.prototype.isEmpty = function() {
40+
return this.front.isEmpty() && this.back.isEmpty();
41+
};
42+
43+
/* TEST */
44+
var m = new myQueue();
45+
console.log(m.isEmpty(), true);
46+
47+
m.add('a');
48+
m.add('b');
49+
m.add('c');
50+
m.add('d');
51+
m.add('e');
52+
m.remove();
53+
console.log(m.peek(), 'b');
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
var Stack = require('./../util/Stack');
2+
3+
var sortStack = function(stack) {
4+
var tempStack = new Stack();
5+
var currMin = Infinity;
6+
var stackDepth = 0;
7+
8+
while (!stack.isEmpty()) {
9+
if (stack.peek() <= currMin) {
10+
if (currMin !== Infinity) {
11+
tempStack.push(currMin);
12+
}
13+
currMin = stack.pop();
14+
} else {
15+
tempStack.push(stack.pop());
16+
}
17+
stackDepth++;
18+
}
19+
20+
while (!tempStack.isEmpty()) {
21+
stack.push(tempStack.pop());
22+
}
23+
24+
tempStack.push(currMin);
25+
currMin = Infinity;
26+
stackDepth--;
27+
28+
while (stackDepth > 0) {
29+
30+
while (!stack.isEmpty()) {
31+
if (stack.peek() <= currMin) {
32+
if (currMin !== Infinity) {
33+
tempStack.push(currMin);
34+
}
35+
currMin = stack.pop();
36+
} else {
37+
tempStack.push(stack.pop());
38+
}
39+
}
40+
41+
for (var i = 0; i < stackDepth - 1; i++) {
42+
stack.push(tempStack.pop());
43+
}
44+
45+
tempStack.push(currMin);
46+
currMin = Infinity;
47+
stackDepth--;
48+
}
49+
50+
while (!tempStack.isEmpty()) {
51+
stack.push(tempStack.pop());
52+
}
53+
54+
return stack;
55+
};
56+
57+
/* TEST */
58+
var s = new Stack();
59+
s.push(99);
60+
s.push(4);
61+
s.push(1);
62+
s.push(6);
63+
s.push(8);
64+
s.push(10);
65+
s.push(22);
66+
s.push(3);
67+
s.push(72);
68+
69+
var sortS = sortStack(s);
70+
71+
while (!sortS.isEmpty()) {
72+
console.log(sortS.pop());
73+
}
74+
75+
76+

0 commit comments

Comments
 (0)