dumb implementations of each so far
This commit is contained in:
@@ -5,7 +5,7 @@ function bubble_sort(arr: number[]): void {
|
||||
const t = arr[j+1];
|
||||
arr[j+1] = arr[j];
|
||||
arr[j] = t;
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
0
ts/linked_list.ts
Normal file
0
ts/linked_list.ts
Normal file
55
ts/queue.test.ts
Normal file
55
ts/queue.test.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
type QNode<T> = {
|
||||
value: T,
|
||||
next?: QNode<T>
|
||||
}
|
||||
|
||||
class Queue<T> {
|
||||
public length: number;
|
||||
private head?: QNode<T>;
|
||||
private tail?: QNode<T>;
|
||||
|
||||
constructor() {
|
||||
this.head = this.tail = undefined;
|
||||
this.length = 0;
|
||||
}
|
||||
|
||||
enqueue(item: T): void {
|
||||
const node = { value: item } as QNode<T>;
|
||||
this.length++;
|
||||
if (!this.tail) {
|
||||
this.tail = this.head = node
|
||||
return
|
||||
}
|
||||
|
||||
this.tail.next = node;
|
||||
this.tail = node;
|
||||
|
||||
}
|
||||
|
||||
deque(): T | undefined {
|
||||
if (!this.head) {
|
||||
return undefined;
|
||||
}
|
||||
this.length--;
|
||||
const head = this.head;
|
||||
this.head = this.head.next;
|
||||
|
||||
return head.value;
|
||||
}
|
||||
|
||||
peek(): T | undefined {
|
||||
return this.head?.value
|
||||
}
|
||||
}
|
||||
|
||||
test("test that queue works", () => {
|
||||
const q = new Queue<number>;
|
||||
|
||||
q.enqueue(12);
|
||||
console.log(q);
|
||||
expect(q.length).toEqual(1);
|
||||
|
||||
q.enqueue(13);
|
||||
console.log(q);
|
||||
expect(q.length).toEqual(2);
|
||||
})
|
||||
Reference in New Issue
Block a user