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

Earth - Emily #1

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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
76 changes: 66 additions & 10 deletions lib/queue.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,89 @@
// had to look at geekforgeeks for reference..

class Queue {
constructor() {
// this.store = ...
throw new Error("This method has not been implemented!");
constructor(size = 20) {
this.store = new Array(size);
this.front = this.back = -1;
this.size = size;
}

enqueue(element) {
throw new Error("This method has not been implemented!");
if ((this.back + 1) % this.size === this.front) {
throw newError("Full Queue!");
} else if (this.front === -1) {
//if empty
this.front = 0;
this.back = 0;
this.store[this.back] = element;
} else if (this.back === this.size - 1 && this.front !== 0) {
this.back = 0;
this.store[this.back] = element;
} else {
console.log("HERE", this.store)
this.back = (this.back + 1) % this.size;
this.store[this.back] = element;
};
}

dequeue() {
throw new Error("This method has not been implemented!");
// let temp = null;

// let data = this.store[this.front];
// this.store[this.front] = null;

// if (this.front === -1) {
// throw newError("Empty Queue!");
// } else if (this.front === this.back) {
// // let temp = this.store[this.front];
// this.front = -1;
// this.back = -1;
// // return temp;
// } else {
// // temp = this.store[this.front];
// this.store[this.front] = null;
// this.front = (this.front + 1) % this.size;
// // return temp;
// }
// return data;
if (this.front === -1) throw newError("Empty Queue!");

let data = this.store[this.front];
this.store[this.front] = null;

//if the queue is empty
if (this.front === this.back) {
console.log("DEQUEUE")

this.front = -1;
this.back = -1;
} else if (this.front === this.size - 1) {
console.log("DEQUEUE")

this.front = 0;
} else {
console.log("DEQUEUE")
this.front = (this.front + 1) % this.size;
}

return data;
}

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

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

isEmpty() {
throw new Error("This method has not been implemented!");
return this.front === -1;
}

toString() {
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.back) {
arr = this.store.slice(this.front, this.size).concat(this.store.slice(0, this.back));
emirry marked this conversation as resolved.
Show resolved Hide resolved
} else {
arr = this.store
}
Expand Down
18 changes: 11 additions & 7 deletions lib/stack.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
const LinkedList = require("./linked-list");

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

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

pop() {
throw new Error("This method has not been implemented!");
let first = this.store.getFirst();

this.store.delete(first);
return first;
}

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

toString() {
JSON.stringify(this.store);
JSON.stringify(this.store);
}
}

Expand Down
Loading