From 874e98dc278864c2004b9df41b4d7197230068d6 Mon Sep 17 00:00:00 2001 From: Dmitry <68408042+FonDerMark@users.noreply.github.com> Date: Tue, 27 Feb 2024 18:29:57 +0500 Subject: [PATCH] TS_HW --- Module_1_Common/TS/DoubleLinkedList.ts | 146 +++++++++++++++++++++++++ Module_1_Common/TS/LinkedList.ts | 115 +++++++++++++++++++ Module_1_Common/TS/RedBlackThree.ts | 83 ++++++++++++++ Module_1_Common/TS/UtilityTypes.ts | 7 ++ 4 files changed, 351 insertions(+) create mode 100644 Module_1_Common/TS/DoubleLinkedList.ts create mode 100644 Module_1_Common/TS/LinkedList.ts create mode 100644 Module_1_Common/TS/RedBlackThree.ts create mode 100644 Module_1_Common/TS/UtilityTypes.ts diff --git a/Module_1_Common/TS/DoubleLinkedList.ts b/Module_1_Common/TS/DoubleLinkedList.ts new file mode 100644 index 0000000..e4ca77a --- /dev/null +++ b/Module_1_Common/TS/DoubleLinkedList.ts @@ -0,0 +1,146 @@ +class Node { + data: T; + prev: Node | null; + next: Node | null; + + constructor(data: T) { + this.data = data; + this.prev = null; + this.next = null; + } +} + +class DoublyLinkedList { + head: Node | null; + tail: Node | null; + length: number; + + constructor() { + this.head = null; + this.tail = null; + this.length = 0; + } + + append(data: T): void { + const newNode = new Node(data); + + if (!this.head) { + this.head = newNode; + this.tail = newNode; + } else { + newNode.prev = this.tail; + if (this.tail) this.tail.next = newNode; + this.tail = newNode; + } + + this.length++; + } + + insertAt(index: number, data: T): void { + if (index < 0 || index > this.length) { + console.error("Wrong index"); + return; + } + + const newNode = new Node(data); + + if (index === 0) { + newNode.next = this.head; + if (this.head) this.head.prev = newNode; + this.head = newNode; + } else if (index === this.length) { + newNode.prev = this.tail; + if (this.tail) this.tail.next = newNode; + this.tail = newNode; + } else { + let current = this.head; + for (let i = 0; i < index - 1; i++) { + if (current) current = current.next; + } + + if (current) { + newNode.next = current.next; + newNode.prev = current; + if (current.next) current.next.prev = newNode; + current.next = newNode; + } + } + + this.length++; + } + + removeAt(index: number): void { + if (index < 0 || index >= this.length) { + console.error("Wrong index"); + return; + } + + let current = this.head; + + if (index === 0) { + this.head = current ? current.next : null; + if (this.head) { + this.head.prev = null; + } else { + this.tail = null; + } + } else if (index === this.length - 1) { + current = this.tail; + this.tail = current ? current.prev : null; + if (this.tail) this.tail.next = null; + } else { + for (let i = 0; i < index; i++) { + if (current) current = current.next; + } + + if (current && current.prev && current.next) { + current.prev.next = current.next; + current.next.prev = current.prev; + } + } + + this.length--; + } + + // Изменение элемента по индексу + updateAt(index: number, newData: T): void { + if (index < 0 || index >= this.length) { + console.error("Wrong index"); + return; + } + + let current = this.head; + for (let i = 0; i < index; i++) { + if (current) current = current.next; + } + + if (current) current.data = newData; + } + + // Поиск элемента по индексу + search(data: T): Node | null { + let current = this.head; + while (current) { + if (current.data === data) { + return current; + } + current = current.next; + } + + return null; + } + + // Получение длины списка + getLength(): number { + return this.length; + } + + // Элементы списка + display(): void { + let current = this.head; + while (current) { + console.log(current.data); + current = current.next; + } + } +} diff --git a/Module_1_Common/TS/LinkedList.ts b/Module_1_Common/TS/LinkedList.ts new file mode 100644 index 0000000..4493c90 --- /dev/null +++ b/Module_1_Common/TS/LinkedList.ts @@ -0,0 +1,115 @@ +class Node { + data: T; + next: Node | null; + + constructor(data: T) { + this.data = data; + this.next = null; + } +} + +class LinkedList { + head: Node | null; + length: number; + + constructor() { + this.head = null; + this.length = 0; + } + + // Добавление элемента + append(data: T): void { + const newNode = new Node(data); + + if (!this.head) { + this.head = newNode; + } else { + let current = this.head; + while (current.next) { + current = current.next; + } + current.next = newNode; + } + + this.length++; + } + + // Вставка элемента + insertAt(index: number, data: T): void { + if (index < 0 || index > this.length) { + console.error("Неверный индекс"); + return; + } + + const newNode = new Node(data); + + if (index === 0) { + newNode.next = this.head; + this.head = newNode; + } else { + let current = this.head; + for (let i = 0; i < index - 1; i++) { + if (current) current = current.next; + } + newNode.next = current ? current.next : null; + if (current) current.next = newNode; + } + + this.length++; + } + + // Удаление элемента + removeAt(index: number): void { + if (index < 0 || index >= this.length) { + console.error("Неверный индекс"); + return; + } + + if (index === 0) { + this.head = this.head ? this.head.next : null; + } else { + let current = this.head; + let prev: Node | null = null; + for (let i = 0; i < index; i++) { + prev = current; + if (current) current = current.next; + } + if (prev && current) prev.next = current.next; + } + + this.length--; + } + + // Изменение элемента + updateAt(index: number, newData: T): void { + if (index < 0 || index >= this.length) { + console.error("Неверный индекс"); + return; + } + + let current = this.head; + for (let i = 0; i < index; i++) { + if (current) current = current.next; + } + + if (current) current.data = newData; + } + + // Поиск элемента + search(data: T): Node | null { + let current = this.head; + while (current) { + if (current.data === data) { + return current; + } + current = current.next; + } + + return null; + } + + // Получение длины списка + getLength(): number { + return this.length; + } +} diff --git a/Module_1_Common/TS/RedBlackThree.ts b/Module_1_Common/TS/RedBlackThree.ts new file mode 100644 index 0000000..fe7366e --- /dev/null +++ b/Module_1_Common/TS/RedBlackThree.ts @@ -0,0 +1,83 @@ +class RedBlackNode { + key: K; + value: V; + color: 'red' | 'black'; + left: RedBlackNode | null; + right: RedBlackNode | null; + + constructor(key: K, value: V, color: 'red' | 'black' = 'red') { + this.key = key; + this.value = value; + this.color = color; + this.left = null; + this.right = null; + } +} + +class RedBlackTree { + root: RedBlackNode | null; + + constructor() { + this.root = null; + } + + // Поиск элемента по ключу + search(key: K): RedBlackNode | null { + return this._search(this.root, key); + } + + private _search(node: RedBlackNode | null, key: K): RedBlackNode | null { + if (node === null || key === node.key) { + return node; + } + + if (key < node.key) { + return this._search(node.left, key); + } else { + return this._search(node.right, key); + } + } + + // Вставка элемента + insert(key: K, value: V): void { + this.root = this._insert(this.root, key, value); + if (this.root) this.root.color = 'black'; // Корень всегда черный + } + + private _insert(node: RedBlackNode | null, key: K, value: V): RedBlackNode { + if (node === null) { + return new RedBlackNode(key, value); + } + + if (key < node.key) { + node.left = this._insert(node.left, key, value); + } else if (key > node.key) { + node.right = this._insert(node.right, key, value); + } else { + node.value = value; // Если ключ уже существует, обновляем значение + } + + return node; + } + + // Удаление элемента + remove(key: K): void { + this.root = this._remove(this.root, key); + } + + private _remove(node: RedBlackNode | null, key: K): RedBlackNode | null { + if (node === null) { + return null; + } + + return node; + } + + // Изменение элемента + update(key: K, value: V): void { + const node = this.search(key); + if (node) { + node.value = value; + } + } +} diff --git a/Module_1_Common/TS/UtilityTypes.ts b/Module_1_Common/TS/UtilityTypes.ts new file mode 100644 index 0000000..df050ee --- /dev/null +++ b/Module_1_Common/TS/UtilityTypes.ts @@ -0,0 +1,7 @@ +// Собственный тип данных - Пользователь +type CustomUserType = { + name: string; + email: string; +}; + +type UserList = Record; \ No newline at end of file