-
Notifications
You must be signed in to change notification settings - Fork 4
Time - Kate P #2
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
base: master
Are you sure you want to change the base?
Changes from all commits
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,35 +1,60 @@ | ||
| class Queue { | ||
| constructor() { | ||
| // this.store = ... | ||
| throw new Error("This method has not been implemented!"); | ||
| constructor(capacity = 20) { | ||
| this.capacity = capacity; | ||
| this.array = []; | ||
| this.front = -1; | ||
| this.rear = -1; | ||
| } | ||
|
|
||
| enqueue(element) { | ||
|
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!"); | ||
| if (this.front === -1) { | ||
| this.front = 0; | ||
| this.rear = 1; | ||
| this.array[this.front] = element; | ||
| } else if (this.front === this.rear) { | ||
| throw new Error("The queue is full"); | ||
| } else { | ||
| this.array[this.rear] = element; | ||
| this.rear = (this.rear + 1) % this.capacity; | ||
| } | ||
| } | ||
|
|
||
| dequeue() { | ||
|
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!"); | ||
| if (this.front === -1) { | ||
| throw new Error("The queue is empty"); | ||
| } | ||
|
|
||
| let data = this.array[this.front]; | ||
| this.array[this.front] = null; | ||
|
|
||
| this.front = (this.front + 1) % this.capacity; | ||
| if (this.front === this.rear) { | ||
| this.front = -1; | ||
| this.rear = -1; | ||
| } | ||
|
|
||
| return data; | ||
| } | ||
|
|
||
| front() { | ||
| throw new Error("This method has not been implemented!"); | ||
| return this.array[front]; | ||
| } | ||
|
|
||
| size() { | ||
| throw new Error("This method has not been implemented!"); | ||
| return this.array.length; | ||
| } | ||
|
Comment on lines
43
to
45
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 really should be the number of elements used in the Queue. |
||
|
|
||
| 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.rear) { | ||
| arr = this.array.slice(this.front, this.capacity).concat(this.array.slice(0, this.rear)); | ||
| } else { | ||
| arr = this.store | ||
| arr = this.array | ||
| } | ||
| return JSON.stringify(arr.filter((v) => v !== null)); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,32 @@ | ||
| const LinkedList = require("./linked-list"); | ||
|
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. 👍 |
||
|
|
||
| class Stack { | ||
| constructor() { | ||
| // this.store = ... | ||
| throw new Error("This method has not been implemented!"); | ||
| this.list = new LinkedList(); | ||
| } | ||
|
|
||
| push() { | ||
| throw new Error("This method has not been implemented!"); | ||
| push(item) { | ||
| this.list.addFirst(item); | ||
| } | ||
|
|
||
| pop() { | ||
| throw new Error("This method has not been implemented!"); | ||
| let first = this.list.getFirst(); | ||
| this.list.delete(first); | ||
|
|
||
| return first; | ||
| } | ||
|
|
||
| isEmpty() { | ||
| throw new Error("This method has not been implemented!"); | ||
| return this.list.isEmpty(); | ||
| } | ||
|
|
||
| toString() { | ||
| JSON.stringify(this.store); | ||
| } | ||
|
|
||
| peek() { | ||
| return this.list.getFirst(); | ||
| } | ||
| } | ||
|
|
||
| module.exports = Stack; | ||
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.
👍