DEV Community

Discussion on: Typescript Data Structures: Linked List

Collapse
 
glebirovich profile image
Gleb Irovich • Edited

Hey Cihat! Thank you for the feedback.
deleteNode as you can see from the type definition requires reference to the node stored in the list. Here is an example of searching and deleting a particular node:

interface Post {
  title: string;
}
const linkedList = new LinkedList<Post>();

linkedList.insertAtEnd({ title: 'Post A' });
linkedList.insertAtEnd({ title: 'Post B' });
linkedList.insertInBegin({ title: 'Post C' });
linkedList.insertInBegin({ title: 'Post D' });

const node = linkedList.search(({ title }) => title === 'Post A');
linkedList.deleteNode(node);
console.log(linkedList.traverse()); // [{ title : "Post D" }, { title : "Post C" }, { title : "Post B" }];
Enter fullscreen mode Exit fullscreen mode
Collapse
 
cihatsaman profile image
Cihat Saman

Thanks @glebirovich for quick answer :)