Skip to content

Commit

Permalink
Merge pull request #48 from devexperts/bugfix/review-the-linked-list-…
Browse files Browse the repository at this point in the history
…methods-logic

fix: fixed LinkedList methods errors + refactored dynamic objects mod…
  • Loading branch information
DeltaZN authored Oct 4, 2023
2 parents 94b406b + d644de9 commit f54288c
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 81 deletions.
37 changes: 15 additions & 22 deletions src/chart/components/dynamic-objects/dynamic-objects.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ export class DynamicObjectsModel extends ChartBaseElement {
paneList.removeAt(currentPos);
paneList.insertAt(position, obj);
}
this.setDynamicObjects(this.objects);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand Down
119 changes: 60 additions & 59 deletions src/chart/utils/linkedList.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,106 +18,107 @@ export class LinkedList<T> {

constructor(head?: ListNode<T>) {
this._head = head ?? null;
// init tail
if (this.head !== null) {
let current: ListNode<T>;
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<T> {
public insertAtEnd(data: T) {
const node = new ListNode(data);
let current: ListNode<T>;

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<T> | 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<T> | 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<T>) {
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++;
}

Expand Down

0 comments on commit f54288c

Please sign in to comment.