chore(JavaScript): add stack and queue implementation (#639)

pull/647/head
atulll 2021-12-03 00:09:23 +05:30 committed by GitHub
parent fb5ab3cf65
commit 62f1cfe0cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 175 additions and 0 deletions

View File

@ -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)

View File

@ -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');

View File

@ -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();

View File

@ -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();

View File

@ -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();