Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated Stack of plates logic in chapter 3 #111

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 48 additions & 83 deletions chapter03/3.3 - Stack of Plates/stackOfPlates.js
Original file line number Diff line number Diff line change
@@ -1,94 +1,59 @@
// implement as array of stacks
const Stack = require("../util/Stack");

class SetOfStacks {
constructor(capacity) {
this.capacity = capacity;
this.stackSet = [];
}

getLastStack() {
return this.stackSet[this.stackSet.length - 1];
class StackOfPlates {
constructor(size) {
this.size = size;
this.stacks = [[]];
}

push(value) {
let last = this.getLastStack();
if (this.stackSet.length === 0 || last.size() === this.capacity) {
var newStack = new Stack();
newStack.push(value);
this.stackSet.push(newStack);
} else {
last.push(value);
}
push(val) {
let lastStack = this.stacks.at(-1);
if (this.stacks.length === 0 || lastStack.length >= this.size) {
this.stacks.push([val]);
} else {
lastStack.push(val);
}
}

pop() {
if (this.stackSet.length === 0) {
return undefined;
}
let last = this.getLastStack();
let value = last.pop();
if (last.size() === 0) {
this.stackSet.pop();
}
return value;
let lastStack = this.stacks.at(-1);
let ele = lastStack.pop();
if (lastStack.length === 0) {
this.stacks.pop();
}
return ele;
}

peek() {
let last = this.getLastStack();
return last.peek();
}

isEmpty() {
return this.stackSet.length === 0;
popAt(index) {
let stack = this.stacks.at(index);
if (stack == null) return -1;
let ele = stack.pop();
if (stack?.length === 0) {
this.stacks.splice(index, 1);
}
return ele;
}

popAt(index) {
// out of range index
if (index < 0 || index >= this.stackSet.length) return false;
let value = this.stackSet[index].pop();
if (this.stackSet[index].size() == 0) {
// clear the stack from the set
this.stackSet.splice(index, 1);
}
return value;
peek() {
let lastStack = this.stacks.at(-1);
return lastStack?.at(-1);
}
}

/* TESTS */

var s = new SetOfStacks(3);
s.push(1);
s.push(2);
s.push(3);
s.push(4);
s.push(5);
s.push(6);
s.push(7);
s.push(8);
s.push(9);
s.push(10);
s.push(11);
s.push(12);
s.push(13);
s.push(14);

console.log(s.peek(), 14);

s.popAt(2);
s.popAt(2);
s.popAt(2);

console.log(s.peek(), 14);

s.pop();
s.pop();
s.pop();
s.pop();
s.pop();
s.pop();
s.pop();
s.pop();
s.pop();

console.log(s.peek(), 2);
let plates = new StackOfPlates(3);
plates.push(1);
plates.push(2);
plates.push(3);
plates.push(4);
plates.push(5);
plates.push(6);
plates.push(7);
plates.push(8);
plates.push(9);
plates.push(10);
plates.push(11);
plates.pop();
plates.pop();
plates.pop();
plates.popAt(0);
plates.popAt(0);
plates.popAt(0);
plates.popAt(0);
console.log(plates);