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

Khandice Schuhmann #2

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
43 changes: 32 additions & 11 deletions lib/queue.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,54 @@
// last in in first out
class Queue {
constructor() {
// this.store = ...
throw new Error("This method has not been implemented!");
constructor(bufferSize = 20) {
this.bufferSize = bufferSize;
this.store = new Array(bufferSize);
this.front = 0;
this.rear = 0;
this.size = 0;
}

enqueue(element) {
throw new Error("This method has not been implemented!");
}
// adds element to the end of the queue

if (this.size === this.bufferSize){
return;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👀 We want to make sure we throw an Error her indicating the queue is full

}

this.store[this.rear] = element;
this.rear = (this.rear + 1) % this.bufferSize;
this.size += 1;
}
//removes and returns element form the front of queue and none if queue is empty
dequeue() {
throw new Error("This method has not been implemented!");

if (this.front === this.rear) {
return;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👀 Again, we want to throw an error here letting the user know the queue is full

}
const firstElement = this.store[this.front];
this.store[this.front] = null;
this.front = (this.front + 1) % this.bufferSize;
this.size -= 1;

return firstElement;
}

front() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

throw new Error("This method has not been implemented!");
return this.store[this.front]
}

size() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

throw new Error("This method has not been implemented!");
return this.size;
}

isEmpty() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

throw new Error("This method has not been implemented!");
return this.size === 0;
}

toString() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let arr;
if (this.head > this.tail) {
arr = this.store.slice(this.head, this.capacity).concat(this.store.slice(0, this.tail));
if (this.front > this.rear) {
arr = this.store.slice(this.front, this.capacity).concat(this.store.slice(0, this.rear));
} else {
arr = this.store
}
Expand Down
18 changes: 12 additions & 6 deletions lib/stack.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
//first in last out
const LinkedList = require("../lib/linked-list.js");


class Stack {
constructor() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// this.store = ...
throw new Error("This method has not been implemented!");
this.store = new LinkedList();
}

push() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

throw new Error("This method has not been implemented!");
push(element) {
return this.store.addLast(element);
}

pop() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

throw new Error("This method has not been implemented!");
const removeItem = this.store.getLast();
this.store.delete(removeItem);
return removeItem;

}

isEmpty() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

throw new Error("This method has not been implemented!");
return this.store.length() === 0;
}

toString() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✨ This works! LinkedList also has a __str__ method you could take advantage of!

Expand Down