From 62f1cfe0cd6afbd950045c58ed5e88e243df3173 Mon Sep 17 00:00:00 2001 From: atulll <39869656+underager@users.noreply.github.com> Date: Fri, 3 Dec 2021 00:09:23 +0530 Subject: [PATCH] chore(JavaScript): add stack and queue implementation (#639) --- algorithms/JavaScript/README.md | 7 ++ algorithms/JavaScript/src/index.js | 7 ++ algorithms/JavaScript/src/queues/queue.js | 54 +++++++++++++ algorithms/JavaScript/src/stacks/stack.js | 29 +++++++ algorithms/JavaScript/src/stacks/two-stack.js | 78 +++++++++++++++++++ 5 files changed, 175 insertions(+) create mode 100644 algorithms/JavaScript/src/queues/queue.js create mode 100644 algorithms/JavaScript/src/stacks/stack.js create mode 100644 algorithms/JavaScript/src/stacks/two-stack.js diff --git a/algorithms/JavaScript/README.md b/algorithms/JavaScript/README.md index e51c9313..3034ad0d 100644 --- a/algorithms/JavaScript/README.md +++ b/algorithms/JavaScript/README.md @@ -22,3 +22,10 @@ ## Strings - [Palindrome](src/strings/palindrome.js) - [Sequence](src/strings/sequence.js) + +## Stacks +- [Stacks](src/stacks/stack.js) +- [Two Stack](src/stacks/two-stack.js) + +## Queues +- [Queue](src/queues/queue.js) diff --git a/algorithms/JavaScript/src/index.js b/algorithms/JavaScript/src/index.js index 214b8f7f..f85a9fe7 100644 --- a/algorithms/JavaScript/src/index.js +++ b/algorithms/JavaScript/src/index.js @@ -19,3 +19,10 @@ require('./sorting/selection-sort'); // Strings require('./strings/palindrome'); require('./strings/sequence'); + +// Stack +require('./stacks/stack'); +require('./stacks/two-stack') + +// Queue +require('./queues/queue'); diff --git a/algorithms/JavaScript/src/queues/queue.js b/algorithms/JavaScript/src/queues/queue.js new file mode 100644 index 00000000..6e48139b --- /dev/null +++ b/algorithms/JavaScript/src/queues/queue.js @@ -0,0 +1,54 @@ +class Node { + constructor(value) { + this.value = value; + this.next = null; + } +} + +class Queue { + constructor() { + this.first = null; + this.last = null; + this.size = 0; + } + + enqueue(value) { + const newNode = new Node(value); + if (!this.first) { + this.first = newNode; + this.last = newNode; + } else { + this.last.next = newNode; + this.last = newNode; + } + this.size++; + } + + dequeue() { + if (!this.first) return null; + const temp = this.first; + this.first = this.first.next; + this.size--; + return temp.value; + } + + printQueue() { + let current = this.first; + while (current) { + console.log(current.value); + current = current.next; + } + } +} + +const queue = new Queue(); +queue.enqueue(1); +queue.enqueue(2); +queue.enqueue(3); +queue.enqueue(4); + +queue.printQueue(); + +queue.dequeue(); + +queue.printQueue(); diff --git a/algorithms/JavaScript/src/stacks/stack.js b/algorithms/JavaScript/src/stacks/stack.js new file mode 100644 index 00000000..8799e49f --- /dev/null +++ b/algorithms/JavaScript/src/stacks/stack.js @@ -0,0 +1,29 @@ +class Stack { + constructor() { + this.items = []; + } + + push(element) { + this.items.push(element); + } + + pop() { + return this.items.pop(); + } + + printStack() { + console.log(this.items); + } +} + +const stack = new Stack(); +stack.push(1); +stack.push(2); +stack.push(3); +stack.push(4); + +stack.printStack(); + +console.log(stack.pop()); + +stack.printStack(); diff --git a/algorithms/JavaScript/src/stacks/two-stack.js b/algorithms/JavaScript/src/stacks/two-stack.js new file mode 100644 index 00000000..84a65b0c --- /dev/null +++ b/algorithms/JavaScript/src/stacks/two-stack.js @@ -0,0 +1,78 @@ +// Implement Stack using two queues +class Stack { + constructor() { + this.queue1 = new Queue(); + this.queue2 = new Queue(); + } + + push(value) { + this.queue1.enqueue(value); + } + + pop() { + if (!this.queue1.first) return null; + while (this.queue1.size > 1) { + this.queue2.enqueue(this.queue1.dequeue()); + } + const temp = this.queue1.dequeue(); + this.queue1 = this.queue2; + this.queue2 = new Queue(); + return temp; + } + + printStack() { + if (this.queue1.size === 0) return null; + let current = this.queue1.first; + while (current) { + console.log(current.value); + current = current.next; + } + } +} + +class Node { + constructor(value) { + this.value = value; + this.next = null; + } +} + +class Queue { + constructor() { + this.first = null; + this.last = null; + this.size = 0; + } + + enqueue(value) { + const newNode = new Node(value); + if (!this.first) { + this.first = newNode; + this.last = newNode; + } else { + this.last.next = newNode; + this.last = newNode; + } + this.size++; + } + + dequeue() { + if (!this.first) return null; + const temp = this.first; + this.first = this.first.next; + this.size--; + return temp.value; + } +} + +const stack = new Stack(); +stack.push(1); +stack.push(2); +stack.push(3); +stack.push(4); + +stack.printStack(); + +console.log(stack.pop()); + +stack.printStack();