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

Kayla - 🌊 #5

Open
wants to merge 3 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
85 changes: 76 additions & 9 deletions lib/queue.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,61 @@
const maxCapacity = 20;
class Queue {
constructor() {
// this.store = ...
throw new Error("This method has not been implemented!");
this.store = new Array(maxCapacity)
this.head = 0
this.tail = 0
}

enqueue(element) {
throw new Error("This method has not been implemented!");
enqueue(data) {
//make sure queue is not full (size set at 20)
if(this.isFull()) {
console.log(this.size + this.isFull)
throw new Error("Sorry, Queue is full!")
} else {
this.store[this.tail] = data
console.log(this.store)
this.tail = (this.tail + 1) % maxCapacity;
}
}

dequeue() {
throw new Error("This method has not been implemented!");
if (this.isEmpty()) {
return
} else {
const data = this.store[this.head]; // data is equal to head
this.store[this.head] = null;
this.head = (this.head + 1) % maxCapacity
console.log(this.size + "SIZZZEEE")
return data;
}
}

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

}

isFull(){
return this.head === (this.tail + 1) % maxCapacity

}

size() {
throw new Error("This method has not been implemented!");
let count = 0;
let pointer = this.head;

while (this.store[pointer]) {
count++;
pointer = (pointer + 1) % maxCapacity;
}

return count;
}

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

toString() {
let arr;
Expand All @@ -35,4 +68,38 @@ class Queue {
}
}

module.exports = Queue;
module.exports = Queue;

// const q = new Queue();
// q.enqueue(10);
// q.enqueue(20);
// // console.log(q)
// q.dequeue();
// q.dequeue();
// // console.log(q)

// q.enqueue(10);
// q.enqueue(20);
// q.enqueue(30);
// q.dequeue;
// q.dequeue;
// q.enqueue(40);
// q.enqueue(50);
// q.enqueue(60);
// q.enqueue(70);
// q.enqueue(80);
// q.enqueue(90);
// q.enqueue(100);
// q.enqueue(110);
// q.enqueue(120);
// q.enqueue(130);
// q.enqueue(140);
// q.enqueue(150);
// q.enqueue(160);
// q.enqueue(170);
// q.enqueue(180);
// q.enqueue(190);
// q.enqueue(200);
// q.enqueue(210);
// q.dequeue();
// console.log(q.toString())
37 changes: 31 additions & 6 deletions lib/stack.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,49 @@
const LinkedList = require('./linked-list')

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

push() {
throw new Error("This method has not been implemented!");
push(item) {
// this.items[this.count] = element
// console.log(`${element} added to ${this.count}`)
// this.count++
// return this.count - 1
this.store.addFirst(item)
}

pop() {
throw new Error("This method has not been implemented!");
// if(this.count == 0) return undefined
// let deleteItem = this.items[this.count - 1]
// this.count--
// console.log(`${deleteItem} removed`)
// return deleteItem
const deleteFirst = this.store.getFirst();
this.store.delete(deleteFirst)
return deleteFirst
}

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

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

module.exports = Stack;
module.exports = Stack;


// const stack = new Stack()
// stack.push(100)
// stack.push(200)
// stack.push(300)

// stack.pop()
// stack.pop()
// stack.pop()
// console.log(stack.pop())