mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 00:01:37 +08:00
Data Structure : Convert live test to Jest test. Remove live code & console.log
This commit is contained in:
@ -12,53 +12,41 @@ class Queue {
|
||||
this.inputStack.push(item)
|
||||
}
|
||||
|
||||
dequeue (item) {
|
||||
dequeue () {
|
||||
// push all items to outputstack
|
||||
this.outputStack = []
|
||||
if (this.inputStack.length > 0) {
|
||||
while (this.inputStack.length > 0) {
|
||||
this.outputStack.push(this.inputStack.pop())
|
||||
}
|
||||
while (this.inputStack.length > 0) {
|
||||
this.outputStack.push(this.inputStack.pop())
|
||||
}
|
||||
// display the top element of the outputstack
|
||||
// return the top element of the outputstack if any
|
||||
if (this.outputStack.length > 0) {
|
||||
console.log(this.outputStack.pop())
|
||||
const top = this.outputStack.pop()
|
||||
// repush all the items to the inputstack
|
||||
this.inputStack = []
|
||||
while (this.outputStack.length > 0) {
|
||||
this.inputStack.push(this.outputStack.pop())
|
||||
}
|
||||
return top
|
||||
}
|
||||
}
|
||||
|
||||
// display elements of the inputstack
|
||||
listIn () {
|
||||
listIn (output = value => console.log(value)) {
|
||||
let i = 0
|
||||
while (i < this.inputStack.length) {
|
||||
console.log(this.inputStack[i])
|
||||
output(this.inputStack[i])
|
||||
i++
|
||||
}
|
||||
}
|
||||
|
||||
// display element of the outputstack
|
||||
listOut () {
|
||||
listOut (output = value => console.log(value)) {
|
||||
let i = 0
|
||||
while (i < this.outputStack.length) {
|
||||
console.log(this.outputStack[i])
|
||||
output(this.outputStack[i])
|
||||
i++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// testing
|
||||
|
||||
const queue = new Queue()
|
||||
queue.enqueue(1)
|
||||
queue.enqueue(2)
|
||||
queue.enqueue(8)
|
||||
queue.enqueue(9)
|
||||
|
||||
console.log(queue.dequeue())
|
||||
// ans = 1
|
||||
console.log(queue.dequeue())
|
||||
// ans = 2
|
||||
export { Queue }
|
||||
|
15
Data-Structures/Queue/test/QueueUsing2Stacks.test.js
Normal file
15
Data-Structures/Queue/test/QueueUsing2Stacks.test.js
Normal file
@ -0,0 +1,15 @@
|
||||
import { Queue } from '../QueueUsing2Stacks'
|
||||
|
||||
describe('QueueUsing2Stacks', () => {
|
||||
const queue = new Queue()
|
||||
|
||||
it('Check enqueue/dequeue', () => {
|
||||
queue.enqueue(1)
|
||||
queue.enqueue(2)
|
||||
queue.enqueue(8)
|
||||
queue.enqueue(9)
|
||||
|
||||
expect(queue.dequeue()).toBe(1)
|
||||
expect(queue.dequeue()).toBe(2)
|
||||
})
|
||||
})
|
Reference in New Issue
Block a user