Skip to content

Month 11/step 1 #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25,384 changes: 25,384 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

224 changes: 224 additions & 0 deletions src/class/list.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
import { ElementStates } from "../types/element-states";
import { Queue } from "./queue";

export class Node<T> {
value: T | null;
color: ElementStates;
index: number;
isHead: boolean;
isTail: boolean;
next: Node<T> | null;
prev: Node<T> | null;
constructor(value: T | null = null,
next?: Node<T>,
index = 0,
prev?: Node<T> | null,
color = ElementStates.Default,) {
this.value = value;
this.color = color;
this.index = index;
this.isHead = false;
this.isTail = false;
this.next = (next === undefined) ? null : next;
this.prev = (prev === undefined) ? null : prev;
}


}

export class List<T extends string> {
list: Node<string> | null;
head: Node<string> | null;
protected tail: Node<string> | null;
private index: number;
size: number;
constructor() {
this.list = null
this.head = null;
this.tail = null;
this.index = 0;
this.size = 0;
this.createList()
}

createList() {
const lengthArray = Math.floor(Math.random() * (4 - 3)) + 3;
let arr: string[] = [];
for (let i = 0; i <= lengthArray; i++) {
const elemArr = Math.floor(Math.random() * 100)
arr.push(`${elemArr}`)
}
for (let i = 0; i < arr.length; i++) {
const node = new Node<string>(arr[i]);
if (!this.head) {
this.list = node;
this.head = node;
this.head.isHead = true;
this.list.isTail = false;
} else {
let current = this.head;
while (current.next) {
current = current.next;
}
current.isTail = false;
current.next = node;
current.next.prev = current;
this.tail = node;
this.tail.isTail = true;
current.index = this.size;
this.size++;

}
}
}

createArr() {
let arr: { value: string, color: ElementStates, isHead: boolean, isTail: boolean }[] = [];
function recursion(node: Node<string> | null) {
if (node) {
const element = {
value: node.value || '',
color: node.color,
isHead: node.isHead,
isTail: node.isTail,
};
arr.push(element);
if (node.next) {
recursion(node.next);
}
}
}

if (this.list) {
recursion(this.list);
}

return arr;
}

push(element: T) {
if (this.tail && this.list) {
let current = this.tail;
current.isTail = false
current.next = new Node(element);
this.tail = current.next;
current.next.isTail = true;
current.next.prev = current;
this.index++;
}
}

pop() {
if (this.tail?.prev) {
let current = this.tail.prev;
current.next = null;
this.tail = current;
current.isTail = true;
}
}

unshift(element: T) {
if (this.head) {
let temp = this.head;
let current = new Node<string>(element);
current.next = temp;
current.next.prev = current;
current.isHead = true;
current.next.isHead = false;
this.head = current;
this.list = current;
}
}

shift() {
if (this.head?.next) {
let current = this.head.next;
current.prev = null;
current.isHead = true;
this.head = current;
this.list = current;
}
}

insert(id: string, element: T) {
const newElement = new Node<string>(element);
let current = this.head;
let index = 0;
if (current) {
while (current && index !== +id) {
current = current.next;
index++;
}
if (current && current.prev) {
const temp = current.prev;
if (temp) {
temp.next = newElement;
newElement.prev = temp;
newElement.next = current;
current.prev = newElement;
this.list = this.head;
}
} else if (current && !current.prev) {

this.unshift(element)
} else if (current && !current.next) {
this.push(element)
}
}
}

delElementId(id: number) {
let current = this.head;
let index = 0;
if (current) {
while (current && index !== +id) {
current = current.next;
index++;
}
if (current && current.prev && current.next) {
current.prev.next = current.next;
current.next.prev = current.prev;
}
if (current && !current.next) {
this.pop()
}
if (current && !current.prev) {
this.shift()
}
}
}


getTail() {
if (this.tail) {
return this.tail.value
}
return undefined;
}

getHead() {
if (this.head) {
return this.head.value
}
return undefined
}

getElement(id: number) {
let current = this.list;
let index = 0;
if (current) {
while (index !== id && current) {
current = current.next;
index++;
}
return current;
}
}






}

154 changes: 154 additions & 0 deletions src/class/queue.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import { TNode } from "../components/queue-page/queue-page";
import { ElementStates } from "../types/element-states";
import Stack from "./stack";

export class Node<T> {
value: T | null;
color: ElementStates;
isHead: boolean;
isTail: boolean;
next: Node<T> | null;
prev: Node<T> | null;
constructor(value: T | null = null, next?: Node<T>, prev?: Node<T> | null, color = ElementStates.Default,) {
this.value = value;
this.color = color;
this.isHead = false;
this.isTail = false;
this.next = (next === undefined) ? null : next;
this.prev = (prev === undefined) ? null : prev;
}


}
export type TQueue<T> = {
/* push: (element: T) => void; */
createList: () => void
}
export class Queue<T> implements TQueue<T> {
list: Node<T> | null;
head: Node<T> | null;
private tail: Node<T> | null;
private size: number;
length?: number | null

constructor(length: number) {
this.list = null
this.head = null;
this.tail = null;
this.size = 0;
this.length = length;
this.createList()
}
createList() {
if (this.length) // О как!!!
for (let i = 0; i < this.length; i++) {
const node = new Node<T>();
if (!this.head) {
this.list = node;
this.head = node;
this.tail = node;
} else {
let current = this.head;
while (current.next) {
current = current.next;
}
current.next = node;
current.next.prev = current;
this.tail = node;

}
}
}
push(element: T) {
if (!this.size && this.head && this.tail) {
this.head.isHead = true;
this.head.isTail = true;
this.head.value = element;
this.tail = this.head;
this.head.color = ElementStates.Default;
this.size++;
} else if (this.head && this.list) {
let current = this.head;
while (this.length !== this.size) {
if (current.value && current.next) {
current = current.next;
} else {
current.value = element;
this.tail = current;
this.head.isTail = false;
if (current.prev) current.prev.isTail = false;
current.isTail = true;
current.color = ElementStates.Default
this.size++;
break
}
}
}

}
pop() {
if (this.head && this.head.value && this.list) {
if (this.head && this.head.value) {
this.head.value = null;
this.head.isHead = false;
if (this.head.next && this.head !== this.tail) {
this.head = this.head.next;
this.head.isHead = true;
this.head.prev = null;
} else {
this.head.isTail = false;
this.head = null;
this.tail = null;

}
}
}
}

createArr() {
let arr: { value: string, color: ElementStates, isHead: boolean, isTail: boolean }[] = [];
function recusia(node: any) {
let current = node;
const elemetn = {
value: current.value,
color: current.color,
isHead: current.isHead,
isTail: current.isTail,
}
if (!current.next) {
arr.push(elemetn)
return arr
}
arr.push(elemetn)
current = current.next;
recusia(current)
}
if (this.list) {
recusia(this.list)
}
return arr
}

getTail() {
if (this.list) {
let current = this.list;
while (current.next) {
if (current.isTail) {
return current
}
current = current.next;
}
}
return null;
}

clear() {
this.list = null;
this.head = null;
this.tail = null;
this.size = 0;
this.createList()
}


}
Loading