chore(JavaScript): add stack and queue implementation (#639)
parent
fb5ab3cf65
commit
62f1cfe0cd
|
@ -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)
|
||||
|
|
|
@ -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');
|
||||
|
|
|
@ -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();
|
|
@ -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();
|
|
@ -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();
|
Loading…
Reference in New Issue