-
Notifications
You must be signed in to change notification settings - Fork 269
Problems involving Queues #81
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
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
a3d841e
update: added tail of LL
ashokdey 63ac632
update: size property aded
ashokdey cbe4a1e
update: implemented Queue using SLL
ashokdey cc29c56
fix: peek() should show the front item of queue
ashokdey 515f423
fix: change in Queue APIs
ashokdey aa9e66f
fix: change in equality, fix in test case for new Queue API
ashokdey d48713c
cleanup
ashokdey 61049dc
update: reverse-first-k elements of a queue
ashokdey a8dac97
update: gen binary numbers using queue
ashokdey 4119cf4
update: entry added in README
ashokdey d9355b9
cleanup
ashokdey fa2a68a
update: queue using stack
ashokdey 75b69be
update: entry in readme
ashokdey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
src/_DataStructures_/Queue/QueueUsingArray.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
class Queue { | ||
constructor() { | ||
this.data = []; | ||
} | ||
|
||
add(element) { | ||
// add element to the start of the data | ||
return this.data.unshift(element); | ||
} | ||
|
||
peek() { | ||
return this.data[this.data.length - 1]; | ||
} | ||
|
||
remove() { | ||
return this.data.pop(); | ||
} | ||
} | ||
|
||
module.exports = Queue; |
29 changes: 29 additions & 0 deletions
src/_DataStructures_/Queue/generate-binary-number/index.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
const Queue = require('../index'); | ||
|
||
function generateBinaryNumber(n) { | ||
const result = []; | ||
const q = new Queue(); | ||
|
||
// add `1` to the queue | ||
q.enqueue('1'); | ||
|
||
// iterate till the given number | ||
for (let i = 0; i < n; i += 1) { | ||
// push the item in the queue to the array | ||
result.push(q.dequeue()); | ||
|
||
// append `0` & `1` respectively | ||
const s1 = `${result[i]}0`; | ||
const s2 = `${result[i]}1`; | ||
|
||
// push the combinations in the queue | ||
q.enqueue(s1); | ||
q.enqueue(s2); | ||
} | ||
// return the result containing all the binary numbers | ||
return result; | ||
} | ||
|
||
// console.log(generateBinaryNumber(5)); | ||
|
||
module.exports = generateBinaryNumber; |
55 changes: 46 additions & 9 deletions
src/_DataStructures_/Queue/index.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,57 @@ | ||
class Queue { | ||
const { LinkedList: SinglyLinkedLists } = require('../LinkedList'); | ||
|
||
class Queue extends SinglyLinkedLists { | ||
constructor() { | ||
this.data = []; | ||
super(); | ||
this.NotAllowed = 'Not Allowed'; | ||
} | ||
|
||
enqueue(data) { | ||
return this.addAtEnd(data); | ||
} | ||
|
||
add(element) { | ||
// add element to the start of the data | ||
return this.data.unshift(element); | ||
dequeue() { | ||
const node = this.removeFromBeginning(); | ||
return node ? node.data : node; | ||
} | ||
|
||
peek() { | ||
return this.data[this.data.length - 1]; | ||
const node = this.getFirst(); | ||
return node ? node.data : node; | ||
} | ||
|
||
length() { | ||
return this.size; | ||
} | ||
|
||
destroy() { | ||
this.delete(); | ||
} | ||
|
||
/** Override and throw error for other LL methods */ | ||
addAtBeginning() { | ||
throw new Error(this.NotAllowed); | ||
} | ||
|
||
addAt() { | ||
throw new Error(this.NotAllowed); | ||
} | ||
|
||
removeFromEnd() { | ||
throw new Error(this.NotAllowed); | ||
} | ||
|
||
getLast() { | ||
throw new Error(this.NotAllowed); | ||
} | ||
|
||
getAt() { | ||
throw new Error(this.NotAllowed); | ||
} | ||
|
||
remove() { | ||
return this.data.pop(); | ||
removeAt() { | ||
throw new Error(this.NotAllowed); | ||
} | ||
} | ||
|
||
module.exports = Queue; | ||
module.exports = Queue; |
28 changes: 28 additions & 0 deletions
src/_DataStructures_/Queue/queue-using-stack/index.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
const Stack = require('../../Stack'); | ||
|
||
class Queue { | ||
constructor() { | ||
this.queue = new Stack(); | ||
this.temp = new Stack(); | ||
} | ||
|
||
enqueue(data) { | ||
this.queue.push(data); | ||
} | ||
|
||
dequeue() { | ||
if (!this.queue.peek()) { | ||
return null; | ||
} | ||
|
||
// pop all the element to the temp stack | ||
while (this.queue.peek()) this.temp.push(this.queue.pop()); | ||
const el = this.temp.pop(); | ||
|
||
// push all the temp items to the queue again | ||
while (this.temp.peek()) this.queue.push(this.temp.pop()); | ||
return el; | ||
} | ||
} | ||
|
||
module.exports = Queue; |
48 changes: 48 additions & 0 deletions
src/_DataStructures_/Queue/reverse-first-k/index.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// eslint-disable-next-line no-unused-vars | ||
const Queue = require('../index'); | ||
const Stack = require('../../Stack'); | ||
|
||
function reverseFirstKElelments(q, k) { | ||
const s = new Stack(); | ||
|
||
// push all the k elements ot the stack | ||
for (let i = 0; i < k; i += 1) { | ||
s.push(q.dequeue()); | ||
} | ||
|
||
// push the stack items to the queue | ||
for (let i = 0; i < k; i += 1) { | ||
q.enqueue(s.pop()); | ||
} | ||
|
||
// empty the queue and push the same queue | ||
const remaining = q.length() - k; | ||
for (let i = 0; i < remaining; i += 1) { | ||
q.enqueue(q.dequeue()); | ||
} | ||
|
||
// return the queue | ||
return q; | ||
} | ||
|
||
module.exports = reverseFirstKElelments; | ||
|
||
// let q = new Queue(); | ||
|
||
// q.enqueue(1); | ||
// q.enqueue(2); | ||
// q.enqueue(3); | ||
// q.enqueue(4); | ||
// q.enqueue(5); | ||
// q.enqueue(6); | ||
// q.enqueue(7); | ||
// q.enqueue(8); | ||
// q.enqueue(9); | ||
|
||
// q = reverseFirstKElelments(q, 4); | ||
|
||
// const arr = []; | ||
// while (q.length()) { | ||
// arr.push(q.dequeue()); | ||
// } | ||
// console.log(arr); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.