Does Javascript have linked lists?
Linked List in JavascriptOsgood Gunawan Show
Aug 24, 2020·5 min read Last time I wrote an article about stack and queue implementation in an array. At the end of the article, I mention that I am going to talk about stack and queue in the Link List. Hold on, but before implementing stack and queue in Linked List, lets understand what linked list is. What Is a Linked List?A linked list is similar to an array. The differences are that the linked list doesnt have a particular memory location/index, and its structure is a chain of nodes. Each node contains two items: a value and a pointer to the next node of the chain. Check the illustration down below: Linked List vs. Array The head pointing(reference) to the first node (element) in the linked list also includes the entry of the linked list. The last node of the list points to null, which is also the tail. The tail is a particular node, where the next pointer is always pointing or reference to null, indicating the end of the list. If the list is empty, the head points to the null. Linked List visualization in javascript:Advantage
Disadvantage
Three types of linked list
Implementing a list node in JavascriptSo how do we create a linked list? First, we need to define a node class. As earlier stated, each node has value and pointer to the next node. And now, we are implementing a linked list in javascript with a constructor. Start with the head and tail are null, and the length of the list is zero. Lets create a linked list from the class LinkedList that was just created. Linked Lists MethodsUnlike arrays, there are no built-in functions in the linked list; everything needs to be created from scratch. Lets build helper methods for the singly linked list.
Once we understand more about how a linked list works, we can start implementing stack and queue in the next article. Thanks for reading, and I hope you can learn something. Resources:JavaScript (JS) Algorithms and Data Structures MasterclassHi! I'm Colt. I'm a developer with a serious love for teaching. I've spent the last few years teaching people towww.udemy.com |