-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueue.ts
47 lines (36 loc) · 1.23 KB
/
Queue.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { LinkedList, LinkNode } from "./SinglyLinkedList";
export class Queue<T>{
private list = new LinkedList<T>;
private capacity: number = 10;
constructor(capacity?: number) {
if (capacity)
this.capacity = capacity;
}
public getSize = () => this.list.getSize();
public isEmpty = (): boolean => this.list.isEmpty();
public isFull = (): boolean => this.list.getSize() >= this.capacity;
public peek = (): LinkNode<T> | undefined => this.list.peekHead();
public enqueue = (value: T) => {
if (this.isFull())
return false;
const node = new LinkNode<T>(value);
this.list.addTail(node);
return true;
}
public dequeue = (): T | undefined => {
if (this.isEmpty())
return;
const head = this.list.peekHead();
this.list.removeHead();
return head?.data
}
public print = (): T[] => this.list.traverse();
}
/**********************************************************************************************************************/
interface Enterance {
name: string;
}
const queue = new Queue<Enterance>(15);
queue.enqueue({name: 'Emre'});
queue.enqueue({name : 'Merve'});
console.log(queue.isEmpty());