From a3d841e1ffbd319bb9c31d3c5bf9318de23ea7c1 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: 2019年10月13日 11:44:36 +0530 Subject: [PATCH 01/13] update: added tail of LL --- src/_DataStructures_/LinkedList/index.js | 36 +++++++++++++----------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/src/_DataStructures_/LinkedList/index.js b/src/_DataStructures_/LinkedList/index.js index 1704c3c4..62a85b3c 100644 --- a/src/_DataStructures_/LinkedList/index.js +++ b/src/_DataStructures_/LinkedList/index.js @@ -2,36 +2,42 @@ class Node { constructor(data, next) { this.data = data; this.next = next; + this.length = 0; } } class LinkedList { constructor() { this.head = null; + this.tail = null; } addAtBeginning(element) { this.head = new Node(element, this.head); + if (!this.tail) { + this.tail = this.head; + } + return this.head; } addAtEnd(element) { - const node = new Node(element, null); - if (!this.head) { - this.head = node; - } else { - let address = this.head; - while (address.next) { - address = address.next; - } - address.next = node; + return this.addAtBeginning(element); } + const node = new Node(element, null); + this.tail.next = node; + this.tail = node; + return node; } removeFromBeginning() { if (!this.head) { + this.tail = null; return null; } + if (this.head.next === null) { + this.tail = this.head; + } const node = this.head; this.head = this.head.next; return node; @@ -47,8 +53,10 @@ class LinkedList { address = address.next; } - const node = address.next; - address.next = null; + this.tail = address; + + const node = this.tail.next; + this.tail.next = null; return node; } @@ -63,11 +71,7 @@ class LinkedList { if (!this.head) { return null; } - let address = this.head; - while (address.next) { - address = address.next; - } - return address; + return this.tail; } getAt(index) { From 63ac6320f1d201e009c32e87e0efd6b94fba93f5 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: 2019年10月13日 11:52:18 +0530 Subject: [PATCH 02/13] update: size property aded --- src/_DataStructures_/LinkedList/index.js | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/_DataStructures_/LinkedList/index.js b/src/_DataStructures_/LinkedList/index.js index 62a85b3c..fd1ed9c2 100644 --- a/src/_DataStructures_/LinkedList/index.js +++ b/src/_DataStructures_/LinkedList/index.js @@ -2,7 +2,6 @@ class Node { constructor(data, next) { this.data = data; this.next = next; - this.length = 0; } } @@ -10,6 +9,7 @@ class LinkedList { constructor() { this.head = null; this.tail = null; + this.size = 0; } addAtBeginning(element) { @@ -17,6 +17,7 @@ class LinkedList { if (!this.tail) { this.tail = this.head; } + this.size += 1; return this.head; } @@ -27,12 +28,12 @@ class LinkedList { const node = new Node(element, null); this.tail.next = node; this.tail = node; + this.size += 1; return node; } removeFromBeginning() { if (!this.head) { - this.tail = null; return null; } if (this.head.next === null) { @@ -40,6 +41,7 @@ class LinkedList { } const node = this.head; this.head = this.head.next; + this.size -= 1; return node; } @@ -57,6 +59,7 @@ class LinkedList { const node = this.tail.next; this.tail.next = null; + this.size -= 1; return node; } @@ -108,8 +111,10 @@ class LinkedList { count -= 1; } - previous.next = new Node(element, previous.next); - return null; + const node = new Node(element, previous.next); + previous.next = node; + this.size += 1; + return node; } removeAt(index) { @@ -133,21 +138,18 @@ class LinkedList { const node = address; previous.next = address.next.next; + this.size -= 1; return node; } length() { - let address = this.head; - let count = 0; - while (address) { - count += 1; - address = address.next; - } - return count; + return this.size; } delete() { this.head = null; + this.tail = this.head; + this.size = 0; } } From cbe4a1eb7e025c1c4797fbfa61a6bac4ba0c7bf9 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: 2019年10月13日 13:21:39 +0530 Subject: [PATCH 03/13] update: implemented Queue using SLL --- src/_DataStructures_/Queue/QueueUsingArray.js | 20 +++++++ src/_DataStructures_/Queue/index.js | 55 ++++++++++++++++--- 2 files changed, 66 insertions(+), 9 deletions(-) create mode 100644 src/_DataStructures_/Queue/QueueUsingArray.js diff --git a/src/_DataStructures_/Queue/QueueUsingArray.js b/src/_DataStructures_/Queue/QueueUsingArray.js new file mode 100644 index 00000000..99d1861c --- /dev/null +++ b/src/_DataStructures_/Queue/QueueUsingArray.js @@ -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; diff --git a/src/_DataStructures_/Queue/index.js b/src/_DataStructures_/Queue/index.js index d51bcab0..641724f1 100644 --- a/src/_DataStructures_/Queue/index.js +++ b/src/_DataStructures_/Queue/index.js @@ -1,20 +1,57 @@ -class Queue { +const { LinkedList: SinglyLinkedLists, Node } = require('../LinkedList'); + +class Queue extends SinglyLinkedLists { constructor() { - this.data = []; + super(); + this.NotAllowed = 'Not Allowed'; + } + + enqueue(data) { + const node = new Node(data, null); + return this.addAtEnd(node); } - add(element) { - // add element to the start of the data - return this.data.unshift(element); + dequeue() { + const node = this.removeFromBeginning(); + return node.data; } peek() { - return this.data[this.data.length - 1]; + return this.getLast(); + } + + size() { + return this.length(); + } + + 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); + } + + getFirst() { + 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; \ No newline at end of file +module.exports = Queue; From cc29c56d81137123e41ade2ceb49029874323331 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: 2019年10月13日 13:24:05 +0530 Subject: [PATCH 04/13] fix: peek() should show the front item of queue --- src/_DataStructures_/Queue/Queue.test.js | 21 +++++++++++---------- src/_DataStructures_/Queue/index.js | 4 ++-- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/_DataStructures_/Queue/Queue.test.js b/src/_DataStructures_/Queue/Queue.test.js index ebe7205c..bca8210f 100644 --- a/src/_DataStructures_/Queue/Queue.test.js +++ b/src/_DataStructures_/Queue/Queue.test.js @@ -12,23 +12,24 @@ describe('Data Structure : Queue', () => { queue = new Queue(); }); - it('Should add() element to a queue', () => { - queue.add(5); - expect(queue.data).toEqual([5]); + it('Should enqueue() element to a queue', () => { + queue.enqueue(5); + expect(queue.peek()).toEqual([5]); }); - it('Should remove() an element from the queue', () => { - queue.add(2); - queue.add(3); + it('Should dequeue() an element from the queue', () => { + queue.enqueue(2); + queue.enqueue(3); - expect(queue.remove()).toEqual(2); - expect(queue.data).toEqual([3]); + expect(queue.dequeue()).toEqual(2); + expect(queue.peek()).toEqual(3); + expect(queue.size()).toEqual(1); }); describe('peek()', () => { beforeEach(() => { - queue.add(2); - queue.add(5); + queue.enqueue(2); + queue.enqueue(5); }); it('Should return the elemet to be removed using peek()', () => { diff --git a/src/_DataStructures_/Queue/index.js b/src/_DataStructures_/Queue/index.js index 641724f1..01c04de8 100644 --- a/src/_DataStructures_/Queue/index.js +++ b/src/_DataStructures_/Queue/index.js @@ -17,7 +17,7 @@ class Queue extends SinglyLinkedLists { } peek() { - return this.getLast(); + return this.getFirst(); } size() { @@ -41,7 +41,7 @@ class Queue extends SinglyLinkedLists { throw new Error(this.NotAllowed); } - getFirst() { + getLast() { throw new Error(this.NotAllowed); } From 515f4234bf9435949cf427578561b3956dfd0500 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: 2019年10月13日 13:40:23 +0530 Subject: [PATCH 05/13] fix: change in Queue APIs --- src/_DataStructures_/LinkedList/index.js | 11 +++++++--- src/_DataStructures_/Queue/Queue.test.js | 26 ++++++++++++------------ src/_DataStructures_/Queue/index.js | 19 ++++++++++++----- 3 files changed, 35 insertions(+), 21 deletions(-) diff --git a/src/_DataStructures_/LinkedList/index.js b/src/_DataStructures_/LinkedList/index.js index fd1ed9c2..e7964c01 100644 --- a/src/_DataStructures_/LinkedList/index.js +++ b/src/_DataStructures_/LinkedList/index.js @@ -22,13 +22,16 @@ class LinkedList { } addAtEnd(element) { + const node = new Node(element, null); + this.size += 1; + if (!this.head) { - return this.addAtBeginning(element); + this.head = node; + this.tail = node; + return node; } - const node = new Node(element, null); this.tail.next = node; this.tail = node; - this.size += 1; return node; } @@ -42,6 +45,7 @@ class LinkedList { const node = this.head; this.head = this.head.next; this.size -= 1; + node.next = null; return node; } @@ -139,6 +143,7 @@ class LinkedList { const node = address; previous.next = address.next.next; this.size -= 1; + node.next = null; return node; } diff --git a/src/_DataStructures_/Queue/Queue.test.js b/src/_DataStructures_/Queue/Queue.test.js index bca8210f..bdbc6f26 100644 --- a/src/_DataStructures_/Queue/Queue.test.js +++ b/src/_DataStructures_/Queue/Queue.test.js @@ -12,9 +12,9 @@ describe('Data Structure : Queue', () => { queue = new Queue(); }); - it('Should enqueue() element to a queue', () => { + it('Should add element to a queue', () => { queue.enqueue(5); - expect(queue.peek()).toEqual([5]); + expect(queue.peek()).toEqual(5); }); it('Should dequeue() an element from the queue', () => { @@ -23,7 +23,7 @@ describe('Data Structure : Queue', () => { expect(queue.dequeue()).toEqual(2); expect(queue.peek()).toEqual(3); - expect(queue.size()).toEqual(1); + expect(queue.length()).toEqual(1); }); describe('peek()', () => { @@ -38,21 +38,21 @@ describe('Data Structure : Queue', () => { it('Should not remove the element', () => { expect(queue.peek()).toEqual(2); - expect(queue.remove()).toEqual(2); + expect(queue.dequeue()).toEqual(2); }); }); it('Should maintain the order of elements', () => { // first in first out - queue.add(2); - queue.add(1); - queue.add(4); - queue.add(3); - - expect(queue.remove()).toEqual(2); - expect(queue.remove()).toEqual(1); - expect(queue.remove()).toEqual(4); - expect(queue.remove()).toEqual(3); + queue.enqueue(2); + queue.enqueue(1); + queue.enqueue(4); + queue.enqueue(3); + + expect(queue.dequeue()).toEqual(2); + expect(queue.dequeue()).toEqual(1); + expect(queue.dequeue()).toEqual(4); + expect(queue.dequeue()).toEqual(3); }); }); }); diff --git a/src/_DataStructures_/Queue/index.js b/src/_DataStructures_/Queue/index.js index 01c04de8..4e097b16 100644 --- a/src/_DataStructures_/Queue/index.js +++ b/src/_DataStructures_/Queue/index.js @@ -7,8 +7,7 @@ class Queue extends SinglyLinkedLists { } enqueue(data) { - const node = new Node(data, null); - return this.addAtEnd(node); + return this.addAtEnd(data); } dequeue() { @@ -17,11 +16,12 @@ class Queue extends SinglyLinkedLists { } peek() { - return this.getFirst(); + const node = this.getFirst(); + return node.data; } - size() { - return this.length(); + length() { + return this.size; } destroy() { @@ -54,4 +54,13 @@ class Queue extends SinglyLinkedLists { } } +const q = new Queue(); + +q.enqueue(10); +q.enqueue(101); +q.enqueue(44); + +console.log(q.length()); +console.log(q.dequeue()); + module.exports = Queue; From aa9e66fb626e05ce9c6de764e4d7ee557d24a8b0 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: 2019年10月13日 13:47:37 +0530 Subject: [PATCH 06/13] fix: change in equality, fix in test case for new Queue API --- src/_DataStructures_/Queue/index.js | 4 ++-- src/_DataStructures_/Queue/weave/index.js | 8 ++++---- .../Queue/weave/weave.test.js | 20 +++++++++---------- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/_DataStructures_/Queue/index.js b/src/_DataStructures_/Queue/index.js index 4e097b16..acbdfb01 100644 --- a/src/_DataStructures_/Queue/index.js +++ b/src/_DataStructures_/Queue/index.js @@ -12,12 +12,12 @@ class Queue extends SinglyLinkedLists { dequeue() { const node = this.removeFromBeginning(); - return node.data; + return node ? node.data : node; } peek() { const node = this.getFirst(); - return node.data; + return node ? node.data : node; } length() { diff --git a/src/_DataStructures_/Queue/weave/index.js b/src/_DataStructures_/Queue/weave/index.js index 94d1d660..5a6da801 100644 --- a/src/_DataStructures_/Queue/weave/index.js +++ b/src/_DataStructures_/Queue/weave/index.js @@ -4,12 +4,12 @@ function weaveQueues(first, second) { const weaved = new Queue(); while (first.peek() || second.peek()) { - if (first.peek() !== undefined) { - weaved.add(first.remove()); + if (first.peek()) { + weaved.enqueue(first.dequeue()); } - if (second.peek() !== undefined) { - weaved.add(second.remove()); + if (second.peek()) { + weaved.enqueue(second.dequeue()); } } return weaved; diff --git a/src/_DataStructures_/Queue/weave/weave.test.js b/src/_DataStructures_/Queue/weave/weave.test.js index da307f36..05c43ec0 100644 --- a/src/_DataStructures_/Queue/weave/weave.test.js +++ b/src/_DataStructures_/Queue/weave/weave.test.js @@ -10,18 +10,18 @@ describe('Weave two queues using weaveQueues()', () => { const q1 = new Queue(); const q2 = new Queue(); - q1.add('Hello'); - q2.add(1); - q1.add('World'); - q2.add(2); - q2.add(3); + q1.enqueue('Hello'); + q2.enqueue(1); + q1.enqueue('World'); + q2.enqueue(2); + q2.enqueue(3); const q3 = weaveQueues(q1, q2); - expect(q3.remove()).toEqual('Hello'); - expect(q3.remove()).toEqual(1); - expect(q3.remove()).toEqual('World'); - expect(q3.remove()).toEqual(2); - expect(q3.remove()).toEqual(3); + expect(q3.dequeue()).toEqual('Hello'); + expect(q3.dequeue()).toEqual(1); + expect(q3.dequeue()).toEqual('World'); + expect(q3.dequeue()).toEqual(2); + expect(q3.dequeue()).toEqual(3); }); }); From d48713ce85cfc2f314371d0181b36239615116df Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: 2019年10月13日 13:48:04 +0530 Subject: [PATCH 07/13] cleanup --- src/_DataStructures_/Queue/index.js | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/src/_DataStructures_/Queue/index.js b/src/_DataStructures_/Queue/index.js index acbdfb01..9056c130 100644 --- a/src/_DataStructures_/Queue/index.js +++ b/src/_DataStructures_/Queue/index.js @@ -1,4 +1,4 @@ -const { LinkedList: SinglyLinkedLists, Node } = require('../LinkedList'); +const { LinkedList: SinglyLinkedLists } = require('../LinkedList'); class Queue extends SinglyLinkedLists { constructor() { @@ -54,13 +54,4 @@ class Queue extends SinglyLinkedLists { } } -const q = new Queue(); - -q.enqueue(10); -q.enqueue(101); -q.enqueue(44); - -console.log(q.length()); -console.log(q.dequeue()); - module.exports = Queue; From 61049dcfef6fa12ef8022a11198c05093c1d31b3 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: 2019年10月13日 14:44:04 +0530 Subject: [PATCH 08/13] update: reverse-first-k elements of a queue --- .../Queue/reverse-first-k/index.js | 48 +++++++++++++++++++ src/_DataStructures_/Stack/index.js | 6 ++- 2 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 src/_DataStructures_/Queue/reverse-first-k/index.js diff --git a/src/_DataStructures_/Queue/reverse-first-k/index.js b/src/_DataStructures_/Queue/reverse-first-k/index.js new file mode 100644 index 00000000..7d9ccf37 --- /dev/null +++ b/src/_DataStructures_/Queue/reverse-first-k/index.js @@ -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); diff --git a/src/_DataStructures_/Stack/index.js b/src/_DataStructures_/Stack/index.js index 4836ae65..9e6c7613 100644 --- a/src/_DataStructures_/Stack/index.js +++ b/src/_DataStructures_/Stack/index.js @@ -15,8 +15,10 @@ class Stack { peek() { return this.data[this.data.length - 1]; } - isEmpty(){ //check if stack is empty - return this.data.length==0; + + isEmpty() { + // check if stack is empty + return this.data.length === 0; } } From a8dac97dcef19e9acf9a7bbd272a7bf0db05e44d Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: 2019年10月13日 14:57:36 +0530 Subject: [PATCH 09/13] update: gen binary numbers using queue --- .../Queue/generate-binary-number/index.js | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 src/_DataStructures_/Queue/generate-binary-number/index.js diff --git a/src/_DataStructures_/Queue/generate-binary-number/index.js b/src/_DataStructures_/Queue/generate-binary-number/index.js new file mode 100644 index 00000000..feea197b --- /dev/null +++ b/src/_DataStructures_/Queue/generate-binary-number/index.js @@ -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; From 4119cf4a876bbfd373120e129d9ac922daf76156 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: 2019年10月13日 14:58:50 +0530 Subject: [PATCH 10/13] update: entry added in README --- README.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index fd888d40..7bc3dc05 100644 --- a/README.md +++ b/README.md @@ -30,14 +30,15 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct - [Remove Consecutive Repeated Digits](src/_DataStructures_/Stack/remove-consecutive-repeated-digits) - [Implement 2 Stacks using Single Array](src/_DataStructures_/Stack/2-stacks-using1-array) - -- [Queue](src/_DataStructures_/Queue) +* [Queue](src/_DataStructures_/Queue) - [Weave](src/_DataStructures_/Queue/weave) + - [Reverse First K Elements of a Queue](src/_DataStructures_/Queue/reverse-first-k) + - [Generate all Binary Numbers from 1 to N](src/_DataStructures_/Queue/generate-binary-number) -- [Doubly Linked List](src/_DataStructures_/DoublyLinkedList) +* [Doubly Linked List](src/_DataStructures_/DoublyLinkedList) -- [Trees](src/_DataStructures_/Trees) +* [Trees](src/_DataStructures_/Trees) - [Binary Search Tree](src/_DataStructures_/Trees/BinarySearchTree) - [Find kth maximum in a BinarySearchTree](src/_DataStructures_/Trees/BinarySearchTree/find-kth-max) - [Find kth minimum in a BinarySearchTree](src/_DataStructures_/Trees/BinarySearchTree/find-kth-min) From d9355b9fe501741c090210906c2ae5898a7d2161 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: 2019年10月13日 14:59:48 +0530 Subject: [PATCH 11/13] cleanup --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 7bc3dc05..a960682a 100644 --- a/README.md +++ b/README.md @@ -30,15 +30,15 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct - [Remove Consecutive Repeated Digits](src/_DataStructures_/Stack/remove-consecutive-repeated-digits) - [Implement 2 Stacks using Single Array](src/_DataStructures_/Stack/2-stacks-using1-array) -* [Queue](src/_DataStructures_/Queue) +- [Queue](src/_DataStructures_/Queue) - [Weave](src/_DataStructures_/Queue/weave) - [Reverse First K Elements of a Queue](src/_DataStructures_/Queue/reverse-first-k) - [Generate all Binary Numbers from 1 to N](src/_DataStructures_/Queue/generate-binary-number) -* [Doubly Linked List](src/_DataStructures_/DoublyLinkedList) +- [Doubly Linked List](src/_DataStructures_/DoublyLinkedList) -* [Trees](src/_DataStructures_/Trees) +- [Trees](src/_DataStructures_/Trees) - [Binary Search Tree](src/_DataStructures_/Trees/BinarySearchTree) - [Find kth maximum in a BinarySearchTree](src/_DataStructures_/Trees/BinarySearchTree/find-kth-max) - [Find kth minimum in a BinarySearchTree](src/_DataStructures_/Trees/BinarySearchTree/find-kth-min) From fa2a68a50490ec788cd129dd6a3aaa7538c5c17c Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: 2019年10月13日 15:42:56 +0530 Subject: [PATCH 12/13] update: queue using stack --- .../Queue/queue-using-stack/index.js | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/_DataStructures_/Queue/queue-using-stack/index.js diff --git a/src/_DataStructures_/Queue/queue-using-stack/index.js b/src/_DataStructures_/Queue/queue-using-stack/index.js new file mode 100644 index 00000000..dbaf6960 --- /dev/null +++ b/src/_DataStructures_/Queue/queue-using-stack/index.js @@ -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; From 75b69be58495b23f9fa6a9beb1b4767a36d4f89f Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: 2019年10月13日 15:43:30 +0530 Subject: [PATCH 13/13] update: entry in readme --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index a960682a..dc772666 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct - [Weave](src/_DataStructures_/Queue/weave) - [Reverse First K Elements of a Queue](src/_DataStructures_/Queue/reverse-first-k) - [Generate all Binary Numbers from 1 to N](src/_DataStructures_/Queue/generate-binary-number) + - [Queue using Stack](src/_DataStructures_/Queue/queue-using-stack) - [Doubly Linked List](src/_DataStructures_/DoublyLinkedList)

AltStyle によって変換されたページ (->オリジナル) /