I have to do an exercise with data structures, specifically with a doubly linked list and I don't understand why when I try to add two objects to the list I get this error:
"Argument of type '{ id_user: number; id_touristic_place: number; review_title: string; review_desc: string; review_points: number; }' is not assignable to parameter of type 'T'. 'T' could be instantiated with an arbitrary type which could be unrelated to '{ id_user: number; id_touristic_place: number; review_title: string; review_desc: string; review_points: number; }'.ts(2345)"
I Tried this from the documentation of TS
public addAtEnd<T>(data: T): void
but that give me another error related to the node
I would appreciate it if you could try to explain to me the reason for the error. (I'm new on TS)
Here is my code:
export class Node<T> {
public data: T;
public next: Node<T> | null;
public prev: Node<T> | null;
constructor(data: T) {
this.data = data;
this.next = null;
this.prev = null;
}
}
import { Node } from "./Node";
import { ILinkedList } from "./ILinkedList";
export class LinkedList<T> implements ILinkedList<T> {
private head: Node<T> | null;
constructor() {
this.head = null;
}
public get gethead(): Node<T> | null {
return this.head;
}
public set sethead(head: Node<T>) {
this.head = head;
}
public addAtEnd(data: T): void {
const newNode = new Node(data);
if (!this.head) {
this.head = newNode;
} else {
const getLast = (node: Node<T>): Node<T> => {
return node.next ? getLast(node.next) : node;
};
const lastNode = getLast(this.head);
newNode.prev = lastNode;
lastNode.next = newNode;
}
}
public addTwo(): void {
this.addAtEnd({
id_user: 1,
id_touristic_place: 11,
review_title: "Nice place",
review_desc: "C:",
review_points: 0,
});
this.addAtEnd({
id_user: 2,
id_touristic_place: 11,
review_title: "Good",
review_desc: "Not bad at all ",
review_points: 0,
});
}
}