-
Notifications
You must be signed in to change notification settings - Fork 7
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
base: master
Are you sure you want to change the base?
Changes from all commits
317aeba
4725f3f
1db84f2
9b72280
b366549
cd6eace
e272c2d
7ffae0b
d253e65
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
} | ||
|
||
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} | ||
|
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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✨ This works! LinkedList also has a |
||
|
There was a problem hiding this comment.
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