universo-platform-3d

Форк
0
59 строк · 1.1 Кб
1
class QueueNode<T> {
2
  private _value: T
3
  public get value(): T {
4
    return this._value
5
  }
6
  public next: QueueNode<T> | null
7

8
  constructor(value: T) {
9
    this._value = value
10
    this.next = null
11
  }
12
}
13

14
class Queue<T> {
15
  private _first: QueueNode<T> | null = null
16
  public get first(): QueueNode<T> | null {
17
    return this._first
18
  }
19
  private _last: QueueNode<T> | null = null
20
  public get last(): QueueNode<T> | null {
21
    return this._last
22
  }
23
  private _size = 0
24
  public get size(): number {
25
    return this._size
26
  }
27

28
  public enqueue(value: T): number {
29
    const newNode = new QueueNode(value)
30
    if (!this._first) {
31
      this._first = newNode
32
      this._last = newNode
33
    } else {
34
      if (this._last) {
35
        this._last.next = newNode
36
      }
37
      this._last = newNode
38
    }
39

40
    return this._size++
41
  }
42

43
  public dequeue(): T | null {
44
    if (!this._first) {
45
      return null
46
    }
47

48
    const temp = this._first
49
    if (this._first === this._last) {
50
      this._last = null
51
    }
52

53
    this._first = this._first.next
54
    this._size--
55
    return temp.value
56
  }
57
}
58

59
export { Queue, QueueNode }
60

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.