Dmitry 9 months ago
parent aee605586e
commit 874e98dc27

@ -0,0 +1,146 @@
class Node<T> {
data: T;
prev: Node<T> | null;
next: Node<T> | null;
constructor(data: T) {
this.data = data;
this.prev = null;
this.next = null;
}
}
class DoublyLinkedList<T> {
head: Node<T> | null;
tail: Node<T> | 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<T> | 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;
}
}
}

@ -0,0 +1,115 @@
class Node<T> {
data: T;
next: Node<T> | null;
constructor(data: T) {
this.data = data;
this.next = null;
}
}
class LinkedList<T> {
head: Node<T> | 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<T> | 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<T> | null {
let current = this.head;
while (current) {
if (current.data === data) {
return current;
}
current = current.next;
}
return null;
}
// Получение длины списка
getLength(): number {
return this.length;
}
}

@ -0,0 +1,83 @@
class RedBlackNode<K, V> {
key: K;
value: V;
color: 'red' | 'black';
left: RedBlackNode<K, V> | null;
right: RedBlackNode<K, V> | 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<K, V> {
root: RedBlackNode<K, V> | null;
constructor() {
this.root = null;
}
// Поиск элемента по ключу
search(key: K): RedBlackNode<K, V> | null {
return this._search(this.root, key);
}
private _search(node: RedBlackNode<K, V> | null, key: K): RedBlackNode<K, V> | 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<K, V> | null, key: K, value: V): RedBlackNode<K, V> {
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<K, V> | null, key: K): RedBlackNode<K, V> | null {
if (node === null) {
return null;
}
return node;
}
// Изменение элемента
update(key: K, value: V): void {
const node = this.search(key);
if (node) {
node.value = value;
}
}
}

@ -0,0 +1,7 @@
// Собственный тип данных - Пользователь
type CustomUserType = {
name: string;
email: string;
};
type UserList = Record<string, CustomUserType>;
Loading…
Cancel
Save

Powered by TurnKey Linux.