more testing on linkedlist methods

This commit is contained in:
Mikayla Dobson
2022-12-19 17:39:13 -06:00
parent 12af1f9af6
commit b16f19e500
4 changed files with 46 additions and 4 deletions

View File

@@ -10,11 +10,11 @@ export default class ListNode<T> extends Node<T> {
this.prev = prev || null; this.prev = prev || null;
} }
setNextNode(node: ListNode<T>) { setNextNode(node: ListNode<T> | null) {
this.next = node; this.next = node;
} }
setPreviousNode(node: ListNode<T>) { setPreviousNode(node: ListNode<T> | null) {
this.prev = node; this.prev = node;
} }

View File

@@ -54,6 +54,12 @@ export default class LinkedList<T> {
if (!this.head) this.head = newTail; if (!this.head) this.head = newTail;
} }
addManyToTail(...items: T[]) {
for (let each of items) {
this.addToTail(each);
}
}
removeHead(): T | null { removeHead(): T | null {
const toRemove = this.head; const toRemove = this.head;
@@ -74,9 +80,13 @@ export default class LinkedList<T> {
const toRemove = this.tail; const toRemove = this.tail;
if (!toRemove) return null; if (!toRemove) return null;
const newTail = toRemove.nextNode; const newTail = toRemove.prevNode;
this.tail = newTail; this.tail = newTail;
if (this.tail) {
this.tail.setNextNode(null);
}
if (toRemove == this.head) { if (toRemove == this.head) {
this.removeHead(); this.removeHead();
} }
@@ -177,4 +187,24 @@ export default class LinkedList<T> {
return targetNode; 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);
}
} }

View File

@@ -1,5 +1,6 @@
import { ChordQuality, EventData } from "./index"; import { ChordQuality, EventData } from "./index";
import LinkedList from "../LinkedList/index"; import LinkedList from "../LinkedList/index";
import ListNode from "../LinkedList/ListNode";
const firstEvent: EventData = { const firstEvent: EventData = {
root: "C", root: "C",
@@ -41,7 +42,16 @@ const fourthEvent: EventData = {
***************/ ***************/
const eventlist = new LinkedList<EventData>(); const eventlist = new LinkedList<EventData>();
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<EventData>;
// console.log(eventlist.findByNode(thirdnode));
console.log(eventlist); console.log(eventlist);

View File

@@ -1,6 +1,8 @@
import data from "./lib/helpers/sample"; import data from "./lib/helpers/sample";
const { eventlist } = data; const { eventlist } = data;
eventlist.print();
// export default content; // export default content;
// const parser = new DOMParser(); // const parser = new DOMParser();