diff --git a/src/lib/LinkedList/ListNode.ts b/src/lib/LinkedList/ListNode.ts index 1cdd4db..87dbd98 100644 --- a/src/lib/LinkedList/ListNode.ts +++ b/src/lib/LinkedList/ListNode.ts @@ -10,11 +10,11 @@ export default class ListNode extends Node { this.prev = prev || null; } - setNextNode(node: ListNode) { + setNextNode(node: ListNode | null) { this.next = node; } - setPreviousNode(node: ListNode) { + setPreviousNode(node: ListNode | null) { this.prev = node; } diff --git a/src/lib/LinkedList/index.ts b/src/lib/LinkedList/index.ts index dcb5405..7a68f3f 100644 --- a/src/lib/LinkedList/index.ts +++ b/src/lib/LinkedList/index.ts @@ -54,6 +54,12 @@ export default class LinkedList { if (!this.head) this.head = newTail; } + addManyToTail(...items: T[]) { + for (let each of items) { + this.addToTail(each); + } + } + removeHead(): T | null { const toRemove = this.head; @@ -74,9 +80,13 @@ export default class LinkedList { const toRemove = this.tail; if (!toRemove) return null; - const newTail = toRemove.nextNode; + const newTail = toRemove.prevNode; this.tail = newTail; + if (this.tail) { + this.tail.setNextNode(null); + } + if (toRemove == this.head) { this.removeHead(); } @@ -177,4 +187,24 @@ export default class LinkedList { return targetNode; } + + print() { + let output = ""; + let current = this.head; + while (current !== null) { + if (current.data) { + if (typeof current.data == "string") { + output += " " + current.data; + } else { + output += " " + JSON.stringify(current.data); + } + } else { + output += " null"; + } + + current = current.nextNode; + } + + console.log(output); + } } \ No newline at end of file diff --git a/src/lib/helpers/sample.ts b/src/lib/helpers/sample.ts index 1b03c34..96d7fdf 100644 --- a/src/lib/helpers/sample.ts +++ b/src/lib/helpers/sample.ts @@ -1,5 +1,6 @@ import { ChordQuality, EventData } from "./index"; import LinkedList from "../LinkedList/index"; +import ListNode from "../LinkedList/ListNode"; const firstEvent: EventData = { root: "C", @@ -41,7 +42,16 @@ const fourthEvent: EventData = { ***************/ const eventlist = new LinkedList(); -eventlist.addManyToHead(firstEvent, secondEvent, thirdEvent, fourthEvent); +eventlist.addManyToTail(firstEvent, secondEvent, thirdEvent, fourthEvent); + +// eventlist.removeHead(); +// eventlist.removeTail(); + +// eventlist.removeAtPosition(3); +// eventlist.removeByData(fourthEvent); + +// const thirdnode = eventlist.findByData(thirdEvent) as ListNode; +// console.log(eventlist.findByNode(thirdnode)); console.log(eventlist); diff --git a/src/main.ts b/src/main.ts index 01b182a..07d7727 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,6 +1,8 @@ import data from "./lib/helpers/sample"; const { eventlist } = data; +eventlist.print(); + // export default content; // const parser = new DOMParser();