Last active
July 27, 2022 15:18
-
-
Save deepiks/5d5724ca6d2498ee3abd to your computer and use it in GitHub Desktop.
Javascript Linked List - Object literal syntax
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var LinkedList = { | |
| _head: null, | |
| _size: null, | |
| createNode: function(d) { | |
| return { | |
| data: d, | |
| next: null | |
| }; | |
| }, | |
| addToTail: function(d) { | |
| var current, | |
| newNode = this.createNode(d); | |
| if(this._head == null) { | |
| this._head = newNode; | |
| } | |
| else { | |
| current = this._head; | |
| while(current.next != null) { | |
| current = current.next; | |
| } | |
| current.next = newNode; | |
| } | |
| this._size++; | |
| }, | |
| removeNode: function(d) { | |
| if(this._head.data == d) { | |
| this._head = this._head.next; | |
| return; | |
| } | |
| var prev = this._head, | |
| current = this._head.next; | |
| while(current.data !=null) { | |
| if(current.data == d) { | |
| prev.next =current.next; | |
| this._size--; | |
| return; | |
| } | |
| prev = current; | |
| current = current.next; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment