From ae71cc67deff479701ef6c0d37509fd0f45b4990 Mon Sep 17 00:00:00 2001 From: Denis Travin Date: Wed, 4 Oct 2023 17:32:50 +0500 Subject: [PATCH] fix: fixed LinkedList methods errors + refactored dynamic objects model methods a bit --- .../dynamic-objects/dynamic-objects.model.ts | 37 +++--- src/chart/utils/linkedList.utils.ts | 119 +++++++++--------- 2 files changed, 75 insertions(+), 81 deletions(-) diff --git a/src/chart/components/dynamic-objects/dynamic-objects.model.ts b/src/chart/components/dynamic-objects/dynamic-objects.model.ts index d7addc47..877cf6fc 100644 --- a/src/chart/components/dynamic-objects/dynamic-objects.model.ts +++ b/src/chart/components/dynamic-objects/dynamic-objects.model.ts @@ -125,6 +125,7 @@ export class DynamicObjectsModel extends ChartBaseElement { paneList.removeAt(currentPos); paneList.insertAt(position, obj); } + this.setDynamicObjects(this.objects); } /** @@ -143,12 +144,10 @@ export class DynamicObjectsModel extends ChartBaseElement { const targetNode = new ListNode(obj); const targetPos = paneList.getNodePosition(targetNode); if (targetPos >= 0 && targetPos < paneList.size()) { - const nodeToReplace = paneList.removeAt(targetPos); - if (nodeToReplace) { - paneList.insertAtEnd(nodeToReplace.data); - } + paneList.removeAt(targetPos); + paneList.insertAtEnd(obj); + this.setDynamicObjects(this.objects); } - this.setDynamicObjects(this.objects); } /** @@ -167,12 +166,10 @@ export class DynamicObjectsModel extends ChartBaseElement { const targetNode = new ListNode(obj); const targetPos = paneList.getNodePosition(targetNode); if (targetPos > 0 && targetPos <= paneList.size()) { - const nodeToReplace = paneList.removeAt(targetPos); - if (nodeToReplace) { - paneList.insertAt(0, nodeToReplace?.data); - } + paneList.removeAt(targetPos); + paneList.insertAt(0, obj); + this.setDynamicObjects(this.objects); } - this.setDynamicObjects(this.objects); } /** @@ -190,13 +187,11 @@ export class DynamicObjectsModel extends ChartBaseElement { const [obj, paneList] = objInfo; const targetNode = new ListNode(obj); const targetPos = paneList.getNodePosition(targetNode); - if (targetPos >= 0 && targetPos < paneList.size()) { - const nodeToReplace = paneList.removeAt(targetPos); - if (nodeToReplace) { - paneList.insertAt(targetPos + 1, nodeToReplace.data); - } + if (targetPos >= 0 && targetPos + 1 < paneList.size()) { + paneList.removeAt(targetPos); + paneList.insertAt(targetPos + 1, obj); + this.setDynamicObjects(this.objects); } - this.setDynamicObjects(this.objects); } /** @@ -214,13 +209,11 @@ export class DynamicObjectsModel extends ChartBaseElement { const [obj, paneList] = objInfo; const targetNode = new ListNode(obj); const targetPos = paneList.getNodePosition(targetNode); - if (targetPos > 0 && targetPos <= paneList.size()) { - const nodeToReplace = paneList.removeAt(targetPos); - if (nodeToReplace) { - paneList.insertAt(targetPos - 1, nodeToReplace?.data); - } + if (targetPos > 0 && targetPos < paneList.size()) { + paneList.removeAt(targetPos); + paneList.insertAt(targetPos - 1, obj); + this.setDynamicObjects(this.objects); } - this.setDynamicObjects(this.objects); } /** diff --git a/src/chart/utils/linkedList.utils.ts b/src/chart/utils/linkedList.utils.ts index 6090c28a..672eb246 100644 --- a/src/chart/utils/linkedList.utils.ts +++ b/src/chart/utils/linkedList.utils.ts @@ -18,106 +18,107 @@ export class LinkedList { constructor(head?: ListNode) { this._head = head ?? null; - // init tail if (this.head !== null) { - let current: ListNode; - current = this.head; + // init tail + let current = this.head; while (current.next) { current = current.next; } this._tail = current; + this.length++; } } - public insertAtEnd(data: T): ListNode { + public insertAtEnd(data: T) { const node = new ListNode(data); - let current: ListNode; if (this.head === null) { this._head = node; } else { - current = this.head; + let current = this.head; + // iterate till the end of the list while (current.next) { current = current.next; } + // insert the node at the end current.next = node; } this._tail = node; this.length++; - return node; } public insertAt(position: number, data: T) { - if (position > -1 && position < this.length && this.head) { - let current = this.head; - let index = 0; - let previous = null; - const node = new ListNode(data); + // falsy cases + if (!this.head || position < 0 || position > this.length) { + return null; + } + + const node = new ListNode(data); + // if position === 0 it means that we need to insert the node in the head + if (position === 0) { + node.next = this.head; + this._head = node; + } else { + let current: ListNode | null = this.head; + let previous = current; + let index = 0; + // iterate till the index === position + while (current && index < position) { + index++; + previous = current; + current = current.next; + } + // insert an element + node.next = current; + previous.next = node; + // update tail if (position === this.length - 1) { this._tail = node; } - - if (position === 0) { - node.next = current; - this._head = node; - } else { - while (index < position && current.next) { - index++; - previous = current; - current = current.next; - } - node.next = current; - if (previous) { - previous.next = node; - } - } - this.length++; - return current; - } else { - this._head = new ListNode(data); - this.length++; - return this.head; } + this.length++; } - public removeAt(position: number): ListNode | null { - if (position > -1 && position < this.length && this.head) { - let current = this.head; - let previous = null; - let index = 0; + public removeAt(position: number) { + // falsy cases + if (!this.head || position < 0 || position >= this.length) { + return null; + } - if (position === 0) { - this._head = current.next; - } else { - while (index < position && current.next) { - index++; - previous = current; - current = current.next; - } - if (previous) { - previous.next = current.next; - } - if (position === this.length - 1) { - this._tail = current; - } - } - this.length--; - return current; + let current = this.head; + let previous = current; + let index = 0; + + // if position === 0 it means that we need to delete the first node + if (position === 0) { + this._head = current.next; } else { - return null; + // iterate till the index === position + while (current.next && index < position) { + index++; + previous = current; + current = current.next; + } + // remove the element + previous.next = current.next; + // update tail + if (position === this.length - 1) { + this._tail = previous; + } } + this.length--; } public getNodePosition(node: ListNode) { let index = 0; let current = this.head; - while (node) { - if (current?.data === node.data) { + while (current) { + if (current.data === node.data) { return index; } - current = current && current.next; + current = current.next; index++; }