-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
176362b
commit 770bfb1
Showing
4 changed files
with
96 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { describe, test, expect } from 'vitest'; | ||
import { Queue } from './datastructure'; | ||
|
||
describe('datastructures', () => { | ||
describe('queue', () => { | ||
test('queue sizes should be valid', () => { | ||
const queue = new Queue<number>() | ||
expect(queue.size).toBe(0) | ||
queue.add(1) | ||
expect(queue.size).toBe(1) | ||
queue.add(2) | ||
expect(queue.size).toBe(2) | ||
queue.remove() | ||
expect(queue.size).toBe(1) | ||
queue.remove() | ||
expect(queue.size).toBe(0) | ||
}) | ||
test('should be FIFO', () => { | ||
const queue = new Queue([1, 2, 3]); | ||
let res: number[] = []; | ||
let c = 0 | ||
while (queue.size > 0) { | ||
c++ | ||
if (c > 3) { | ||
throw new Error("Too many iterations.") | ||
} | ||
res.push(queue.remove()); | ||
} | ||
expect(res).toEqual([1, 2, 3]); | ||
}); | ||
test('should error on peek empty queue', () => { | ||
const queue = new Queue(); | ||
expect(() => queue.peek()).toThrowError("Can't peek, queue is empty."); | ||
}); | ||
test('should error on remove empty queue', () => { | ||
const queue = new Queue(); | ||
expect(() => queue.remove()).toThrowError("Can't remove an element, queue is empty."); | ||
}) | ||
test('peek should return first element without changing queue', () => { | ||
const queue = new Queue([1, 2, 3]); | ||
expect(queue.peek()).toBe(1); | ||
expect(queue.size).toBe(3); | ||
}); | ||
test('pop should remove first element and return it', () => { | ||
const queue = new Queue([1, 2, 3]); | ||
expect(queue.remove()).toBe(1); | ||
expect(queue.size).toBe(2); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
export class Queue<T> { | ||
#inStack: T[] = []; | ||
#outStack: T[] = []; | ||
|
||
constructor(iterable?: Iterable<T>) { | ||
if (iterable) this.#inStack.push(...iterable); | ||
} | ||
|
||
add(e: T) { | ||
this.#inStack.push(e); | ||
} | ||
|
||
peek(): T { | ||
this.handleEmptyOut("Can't peek, queue is empty.") | ||
return this.#outStack.at(-1)!; | ||
} | ||
|
||
remove(): T { | ||
this.handleEmptyOut("Can't remove an element, queue is empty."); | ||
return this.#outStack.pop()!; | ||
} | ||
|
||
private handleEmptyOut(errMsg: string) { | ||
if (this.#outStack.length === 0) { | ||
this.swapStacks(); | ||
} | ||
if (this.#outStack.length === 0) { | ||
throw new Error(errMsg); | ||
} | ||
} | ||
|
||
get size() { | ||
return this.#inStack.length + this.#outStack.length; | ||
} | ||
|
||
private swapStacks() { | ||
let mem = this.#outStack; | ||
this.#outStack = this.#inStack; | ||
this.#inStack = mem; | ||
this.#outStack.reverse(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters