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

Added sort a stack problem and solution. #85

Merged
merged 17 commits into from
Oct 14, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct
- [Postfix Expression Evaluation](src/_DataStructures_/Stack/postfix-expression-evaluation)
- [Remove Consecutive Repeated Digits](src/_DataStructures_/Stack/remove-consecutive-repeated-digits)
- [Implement 2 Stacks using Single Array](src/_DataStructures_/Stack/2-stacks-using1-array)
- [Reverse First k Elements of Queue](src/_DataStructures_/Stack/reverse-first-k-elements-of-queue)


- [Queue](src/_DataStructures_/Queue)

Expand Down
11 changes: 10 additions & 1 deletion src/_DataStructures_/Queue/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ class Queue {
remove() {
return this.data.pop();
}
isEmpty() {
return this.data.length == 0;
}
enqueue(element) {
this.data.push(element);
}
length() {
return this.data.length;
}
}

module.exports = Queue;
module.exports = Queue;
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* Reverse first K elements of a Queue
* Given an integer K and a queue of integers,reverse the order of the first K elements of the queue, leaving the other elements in the same relative order.
* Input : Q = [20, 19, 18, 17, 16, 15, 14, 13, 12, 11] k = 5
* Output : Q = [ 20, 19, 18, 17, 16, 11, 12, 13, 14, 15]
*
*/
//const Queue = require.resolve('Queue');

const Queue = require('../../Queue/index');

const Stack = require('../index');

function reverseQueueElements(queue, k) {
let s = new Stack();

if (queue.isEmpty()) {
throw new Error("Empty Queue");
}
if(k<=0 || k>queue.length()){
throw new Error("Invalid K value");
}

for (let i = 0; i < k; i++) {
//push first k elements from queue and push in stack
s.push(queue.remove());
}

for (let i = 0; i < k; i++) {
//we reverse the k elements with the help of stack.
//pop all the k elements from stack and add to queue.
let no = s.pop();
queue.add(no);
}

for (let i = 0; i < queue.length() - k; i++) {
//bring the k elments again back to front to maintain the queue order.
queue.add(queue.peek());
queue.remove();

}
return queue;
}

/*
let queue = new Queue();
queue.add(11);
queue.add(12);
queue.add(13);
queue.add(14);
queue.add(15);
queue.add(16);
queue.add(17);
queue.add(18);
queue.add(19);
queue.add(20);
console.log("Queue before:", queue.data);
let k = 5;
let reversedQueue=reverseQueueElements(queue, k);
console.log("Queue after:", queue.data);
*/