Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

miku86
miku86

Posted on

     

JavaScript Data Structures: Singly Linked List: Setup

Intro

Last time, we talked about the theory behind a Singly Linked List.

Today, we start implementing it.

Recap fromlast time

  • real life example: a treasure hunt, where you have a starting point and have to seek places and solve riddles in a particular order; the current place knows about the next place, but the current place doesn't know about the previous place
  • consists of nodes
  • each node has a value and a pointer to the next node (or null at the end of the list)
  • has a head (=start), a tail (=end) and a length
  • "singly" because only one connection to another node (the next one)

Setup

So we need two basic entities:

  • a single place with a riddle (=> anode)
  • the complete treasure hunt (=> theSingly Linked List)

Node

  • create a file namedsingly-linked-list.js
  • add this code
// name of the classclassNode{// the constructor runs when using the class with `new` (see later)constructor(value){// set this nodes value property to the instantiation valuethis.value=value;// set this nodes next property to `null`this.next=null;}}
Enter fullscreen modeExit fullscreen mode

This is a JavaScript class. Under the hood it uses a function, but it doesn't matter, it's all about the concept. We use this object oriented approach, because it is simple to understand.

We have a class and this class acts as a blueprint for a node.

We can instantiate a new instance of this class and save it into a variable:

constnewNode=newNode("Empire State Building");
Enter fullscreen modeExit fullscreen mode

The string "Empire State Building" becomes thevalue in the constructor, sothis.value becomes"Empire State Building".this.next becomesnull.

We can see this by logging it:

console.log(newNode);// Node { value: 'Empire State Building', next: null }
Enter fullscreen modeExit fullscreen mode

We can now create as many nodes as we need by usingnew Node()


Singly Linked List

  • add this code tosingly-linked-list.js
// name of the classclassSinglyLinkedList{// the constructor runs when using the class with `new`constructor(){// set this lists length property to `0`this.length=0;// set this lists head property to `null`this.head=null;// set this lists tail property to `null`this.tail=null;}}
Enter fullscreen modeExit fullscreen mode

Similar to ourNode. Every instance of the Singly Linked List gets alength, ahead and atail.

We can instantiate a new instance of this class and save it into a variable:

constnewSinglyLinkedList=newSinglyLinkedList();
Enter fullscreen modeExit fullscreen mode

Because all of our three properties are set to default values in the constructor, we don't need arguments.

We can see this by logging it:

console.log(newSinglyLinkedList);// SinglyLinkedList { length: 0, head: null, tail: null }
Enter fullscreen modeExit fullscreen mode

We can now create our Singly Linked List by usingnew SinglyLinkedList().


Next Part

We will implement how to add a node at the end of the Singly Linked List. If you want to be notified,subscribe :)


Questions

  • Did you ever use a Singly Linked List in a project? Why?
  • Did you ever use classes in JavaScript?

Top comments(3)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
miku86 profile image
miku86
Mentor & Senior Web Developer - I help people to reach their (career) goals. => https://miku86.com
  • Location
    Germany
  • Joined
• Edited on• Edited

Great questions, Amir.

Use cases with benefits are:

  • when you often have to add or remove data:SLL:O(1) vs. Array: best caseO(1) (at the end) - worst caseO(N) (at the start)

But in the end, you're very unlikely to see a Singly Linked List.

CollapseExpand
 
cooky9 profile image
Landon
  • Location
    New York
  • Joined

Thanks buddy

Thread Thread
 
miku86 profile image
miku86
Mentor & Senior Web Developer - I help people to reach their (career) goals. => https://miku86.com
  • Location
    Germany
  • Joined

You're welcome.

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Mentor & Senior Web Developer - I help people to reach their (career) goals. => https://miku86.com
  • Location
    Germany
  • Joined

More frommiku86

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp