diff --git a/Module_1_Common/JS/DobleLinkedList.js b/Module_1_Common/JS/DobleLinkedList.js new file mode 100644 index 0000000..1d677eb --- /dev/null +++ b/Module_1_Common/JS/DobleLinkedList.js @@ -0,0 +1,135 @@ +class Node { + constructor(data) { + this.data = data; + this.prev = null; + this.next = null; + } +} + +class DoublyLinkedList { + constructor() { + this.head = null; + this.tail = null; + this.length = 0; + } + + append(data) { + const newNode = new Node(data); + + if (!this.head) { + this.head = newNode; + this.tail = newNode; + } else { + newNode.prev = this.tail; + this.tail.next = newNode; + this.tail = newNode; + } + + this.length++; + } + + insertAt(index, data) { + if (index < 0 || index > this.length) { + console.error("Wrong index"); + return; + } + + const newNode = new Node(data); + + if (index === 0) { + newNode.next = this.head; + this.head.prev = newNode; + this.head = newNode; + } else if (index === this.length) { + newNode.prev = this.tail; + this.tail.next = newNode; + this.tail = newNode; + } else { + let current = this.head; + for (let i = 0; i < index - 1; i++) { + current = current.next; + } + + newNode.next = current.next; + newNode.prev = current; + current.next.prev = newNode; + current.next = newNode; + } + + this.length++; + } + + removeAt(index) { + if (index < 0 || index >= this.length) { + console.error("Wrong index"); + return; + } + + let current = this.head; + + if (index === 0) { + this.head = current.next; + if (this.head) { + this.head.prev = null; + } else { + this.tail = null; + } + } else if (index === this.length - 1) { + current = this.tail; + this.tail = current.prev; + this.tail.next = null; + } else { + for (let i = 0; i < index; i++) { + current = current.next; + } + + current.prev.next = current.next; + current.next.prev = current.prev; + } + + this.length--; + } + + // Изменение элемента по индексу + updateAt(index, newData) { + if (index < 0 || index >= this.length) { + console.error("Wrong index"); + return; + } + + let current = this.head; + for (let i = 0; i < index; i++) { + current = current.next; + } + + current.data = newData; + } + + // Поиск элемента по индексу + search(data) { + let current = this.head; + while (current) { + if (current.data === data) { + return current; + } + current = current.next; + } + + return null; + } + + // Получение длины списка + getLength() { + return this.length; + } + + // Элементы списка + display() { + let current = this.head; + while (current) { + console.log(current.data); + current = current.next; + } + } +} + diff --git a/Module_1_Common/JS/LinkedList.js b/Module_1_Common/JS/LinkedList.js new file mode 100644 index 0000000..fe49d2a --- /dev/null +++ b/Module_1_Common/JS/LinkedList.js @@ -0,0 +1,109 @@ +class Node { + constructor(data) { + this.data = data; + this.next = null; + } +} + +class LinkedList { + constructor() { + this.head = null; + this.length = 0; + } + + // Добавление элемента + append(data) { + 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, data) { + 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++) { + current = current.next; + } + newNode.next = current.next; + current.next = newNode; + } + + this.length++; + } + + // Удаление элемента + removeAt(index) { + if (index < 0 || index >= this.length) { + console.error("Неверный индекс"); + return; + } + + if (index === 0) { + this.head = this.head.next; + } else { + let current = this.head; + let prev = null; + for (let i = 0; i < index; i++) { + prev = current; + current = current.next; + } + prev.next = current.next; + } + + this.length--; + } + + // Изменение элемента + updateAt(index, newData) { + if (index < 0 || index >= this.length) { + console.error("Неверный индекс"); + return; + } + + let current = this.head; + for (let i = 0; i < index; i++) { + current = current.next; + } + + current.data = newData; + } + + // Поиск элемента + search(data) { + let current = this.head; + while (current) { + if (current.data === data) { + return current; + } + current = current.next; + } + + return null; + } + + // Получение длины списка + getLength() { + return this.length; + } +} diff --git a/Module_1_Common/JS/RedBlackThree.js b/Module_1_Common/JS/RedBlackThree.js new file mode 100644 index 0000000..17f490d --- /dev/null +++ b/Module_1_Common/JS/RedBlackThree.js @@ -0,0 +1,75 @@ +class RedBlackNode { + constructor(key, value, color = 'red') { + this.key = key; + this.value = value; + this.color = color; + this.left = null; + this.right = null; + } +} + +class RedBlackTree { + constructor() { + this.root = null; + } + + // Поиск элемента по ключу + search(key) { + return this._search(this.root, key); + } + + _search(node, key) { + 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, value) { + this.root = this._insert(this.root, key, value); + this.root.color = 'black'; // Корень всегда черный + } + + _insert(node, key, value) { + 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) { + this.root = this._remove(this.root, key); + } + + _remove(node, key) { + if (node === null) { + return null; + } + + return node; + } + + // Изменение элемента + update(key, value) { + const node = this.search(key); + if (node) { + node.value = value; + } + } +}