You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
742 B
42 lines
742 B
interface LinkedList { |
|
value: number | null; |
|
next: LinkedList | null; |
|
} |
|
|
|
class LinkedListQueue { |
|
private head: LinkedList | null = null; |
|
private tail: LinkedList | null = null; |
|
|
|
private len: number = 0; |
|
|
|
add(value: number) { |
|
if (this.len === 0) { |
|
this.head = { value, next: null }; |
|
this.tail = this.head; |
|
this.len++; |
|
return; |
|
} |
|
|
|
this.tail!.next = { value, next: null }; |
|
this.tail = this.tail!.next; |
|
this.len++; |
|
} |
|
|
|
pop(): number | null { |
|
if (!this.head || !this.tail) return null; |
|
|
|
const popValue = this.head.value; |
|
this.head = this.head.next; |
|
this.len--; |
|
|
|
return popValue; |
|
} |
|
|
|
get length() { |
|
return this.len; |
|
} |
|
|
|
get link() { |
|
return this.head; |
|
} |
|
} |