Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

miku86
miku86

Posted on • Edited on

     

JavaScript Data Structures: Singly Linked List: Unshift

Intro

Last time, we learned how to pop a new node from the end of our Singly Linked List.

Today, we learn how to unshift something to the list.Unshift meansadd something to the beginning.

Current Code

We start with the codefrom the setup, withoutpush andpop, because we want to keep the code as simple as possible to understand.

classNode{constructor(value){this.value=value;this.next=null;}}classSinglyLinkedList{constructor(){this.length=0;this.head=null;this.tail=null;}}
Enter fullscreen modeExit fullscreen mode

Thoughts

First, we should think about the constraints and possibilities:

If there is currently NO other node in the Singly Linked List (so it is currently empty):

  • create a new node
  • set the new node as the Singly Linked List'stail
  • set the new node as the Singly Linked List'shead
  • increase the Singly Linked List's length by 1
  • return the new node

If there is at least 1 node in the Singly Linked List:

  • create a new node
  • set the new node'snext to the Singly Linked List's currenthead
  • set the new node as the Singly Linked List'shead
  • increase the Singly Linked List's length by 1
  • return the new node

Examples:

  • 0 nodes: before: null (head & tail) => after: A (head & tail)
  • 1 node: before: A (head & tail) => after: A-1 (head) -> A (tail)
  • n nodes: before: A (head) -> ... -> n (tail) => after: A-1 (head) -> A -> ... -> n (tail)

Differences:

  • there is only one difference: the step after creating a new node

Implementation (Short version, DRY)

classNode{constructor(value){this.value=value;this.next=null;}}classSinglyLinkedList{constructor(){this.length=0;this.head=null;this.tail=null;}unshift(value){// create a new nodeconstnewNode=newNode(value);// check if Singly Linked List is emptyif(!this.length){// set the new node as the Singly Linked List's `tail`this.tail=newNode;}else{// set the new node's `next` to the Singly Linked List's current `head`newNode.next=this.head;}// set the new node as the Singly Linked List's `head`this.head=newNode;// increase the Singly Linked List's length by 1this.length+=1;// return the new nodereturnnewNode;}}
Enter fullscreen modeExit fullscreen mode

Result

Let's have a look how to use the Singly Linked Listunshift method and its results.

constnewSLL=newSinglyLinkedList();// should be emptyconsole.log(newSLL);// SinglyLinkedList { length: 0, head: null, tail: null }console.log(newSLL.unshift("1"));// Node { value: '1', next: null }// should be a list with the new node with value 1console.log(newSLL);/* *  SinglyLinkedList { *    length: 1, *    head: Node { value: '1', next: null }, *    tail: Node { value: '1', next: null } *  } */console.log(newSLL.unshift("2"));// Node { value: '2', next: Node { value: '1', next: null } }// should be a list with the new node with value 2 and 1 (from the last unshift)console.log(newSLL);/* *  SinglyLinkedList { *    length: 2, *    head: Node { value: '2', next: Node { value: '1', next: null } }, *    tail: Node { value: '1', next: null } *  } */
Enter fullscreen modeExit fullscreen mode

Next Part

We will implement how to remove a node from the beginning of the Singly Linked List. If you want to be notified,subscribe :)


Questions:

  • Any ideas how to improve the post or code?
  • Any specific questions?
  • Do you like the series or is it useless? Why?

Top comments(0)

Subscribe
pic
Create template

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

Dismiss

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