Skip to content

Commit 0370d4e

Browse files
committed
Queue
1 parent ceaa881 commit 0370d4e

File tree

4 files changed

+112
-7
lines changed

4 files changed

+112
-7
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { Queue } from './Queue'
3+
4+
describe('queue', () => {
5+
it('should create empty queue', () => {
6+
const queue = new Queue()
7+
8+
expect(queue).toBeDefined()
9+
expect(queue.toString()).toBe('')
10+
})
11+
12+
it('should enqueue data to queue', () => {
13+
const queue = new Queue()
14+
15+
queue.enqueue(1)
16+
queue.enqueue(2)
17+
18+
expect(queue.toString()).toBe('1,2')
19+
})
20+
21+
it('should peek data from queue', () => {
22+
const queue = new Queue()
23+
24+
expect(queue.peek()).toBeUndefined()
25+
26+
queue.enqueue(1)
27+
queue.enqueue(2)
28+
29+
expect(queue.peek()).toBe(1)
30+
expect(queue.peek()).toBe(1)
31+
})
32+
33+
it('should check queue size', () => {
34+
const queue = new Queue()
35+
36+
expect(queue.size()).toBe(0)
37+
38+
queue.enqueue(1)
39+
queue.enqueue(2)
40+
41+
expect(queue.size()).toBe(2)
42+
})
43+
44+
it('should check if queue is empty', () => {
45+
const queue = new Queue()
46+
47+
expect(queue.isEmpty()).toBe(true)
48+
49+
queue.enqueue(1)
50+
51+
expect(queue.isEmpty()).toBe(false)
52+
})
53+
54+
it('should dequeue data from queue', () => {
55+
const queue = new Queue()
56+
57+
queue.enqueue(1)
58+
queue.enqueue(2)
59+
60+
expect(queue.dequeue()).toBe(1)
61+
expect(queue.dequeue()).toBe(2)
62+
expect(queue.dequeue()).toBeUndefined()
63+
expect(queue.isEmpty()).toBe(true)
64+
})
65+
66+
it('should be possible to enqueue/dequeue objects', () => {
67+
const queue = new Queue<{ value: string }>()
68+
69+
queue.enqueue({ value: 'test1' })
70+
queue.enqueue({ value: 'test2' })
71+
72+
expect(queue.size()).toBe(2)
73+
expect(queue.dequeue()?.value).toBe('test1')
74+
expect(queue.dequeue()?.value).toBe('test2')
75+
})
76+
})

src/data-structures/queue/Queue.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
export class Queue<T> {
2+
private queue: T[] = []
3+
4+
public enqueue(value: T) {
5+
this.queue.push(value)
6+
}
7+
8+
public dequeue() {
9+
return this.queue.shift()
10+
}
11+
12+
public peek() {
13+
return this.queue[0]
14+
}
15+
16+
public size() {
17+
return this.queue.length
18+
}
19+
20+
public isEmpty() {
21+
return this.queue.length === 0
22+
}
23+
24+
toString(callback?: (item: T) => string) {
25+
if (callback)
26+
return this.queue.map(callback).toString()
27+
return this.queue.toString()
28+
}
29+
}

src/data-structures/stack/Stack.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,14 @@ describe('stack', () => {
7070
})
7171

7272
it('should be possible to push/pop objects', () => {
73-
const stack = new Stack()
73+
const stack = new Stack<{ value: string, key: string }>()
7474

7575
stack.push({ value: 'test1', key: 'key1' })
7676
stack.push({ value: 'test2', key: 'key2' })
7777

7878
const stringifier = (value: any) => `${value.key}:${value.value}`
7979
expect(stack.toString(stringifier)).toBe('key2:test2,key1:test1')
80-
expect(stack.pop().value).toBe('test2')
81-
expect(stack.pop().value).toBe('test1')
80+
expect(stack.pop()?.value).toBe('test2')
81+
expect(stack.pop()?.value).toBe('test1')
8282
})
8383
})

src/data-structures/stack/Stack.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
export class Stack {
2-
private items: any[] = []
1+
export class Stack<T> {
2+
private items: T[] = []
33

4-
push(item: any) {
4+
push(item: T) {
55
this.items.push(item)
66
}
77

@@ -25,7 +25,7 @@ export class Stack {
2525
this.items = []
2626
}
2727

28-
toString(callback?: (item: any) => string) {
28+
toString(callback?: (item: T) => string) {
2929
const list = this.items.toReversed()
3030
if (callback)
3131
return list.map(callback).toString()

0 commit comments

Comments
 (0)