Created
August 10, 2018 18:32
-
-
Save thedineshj/162cde0d6a612dc2b36188c52cdc8740 to your computer and use it in GitHub Desktop.
This file contains 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
class node { | |
constructor(data) { | |
this.data = data; | |
this.next = null; | |
} | |
} | |
class SingleLinkedList { | |
constructor() { | |
this.head = null; | |
this.length = 0; | |
} | |
addToHead(data) { | |
let newNode = new node(data); | |
if (this.head == null) { | |
this.head = newNode; | |
} else { | |
newNode.next = this.head; | |
this.head = newNode; | |
} | |
this.length++; | |
} | |
addToLast(data) { | |
let newNode = new node(data); | |
if (this.head == null) { | |
this.head = newNode; | |
} else { | |
let currentNode = this.head; | |
while (currentNode.next != null) { | |
currentNode = currentNode.next; | |
} | |
currentNode.next = newNode; | |
} | |
this.length++; | |
} | |
insertAfter(key, data) { | |
if (this.head == null) { | |
console.log("The list is empty"); | |
} else { | |
let currentNode = this.head; | |
let newNode = new node(data); | |
while (currentNode != null) { | |
if (currentNode.data == key) { | |
newNode.next = currentNode.next; | |
currentNode.next = newNode; | |
break; | |
} | |
currentNode = currentNode.next; | |
} | |
} | |
this.length++; | |
} | |
search(value) { | |
if (this.head == null) { | |
console.log("The list is empty"); | |
return false; | |
} else { | |
let currentNode = this.head; | |
while (currentNode != null) { | |
if (currentNode.data == value) { | |
console.log(`${value} is found in the list`); | |
return true; | |
} | |
currentNode = currentNode.next; | |
} | |
} | |
console.log(`${value} is not in the list`); | |
return false; | |
} | |
delete(value) { | |
if (this.head == null) { | |
console.log("The list is empty"); | |
} else { | |
let currentNode = this.head; | |
let previousNode = null; | |
while (currentNode != null) { | |
if (currentNode.data == value) { | |
break; | |
} | |
previousNode = currentNode; | |
currentNode = currentNode.next; | |
} | |
if (currentNode == null) { | |
console.log(`${value} is not in the list`); | |
} else { | |
if (currentNode.data == this.head.data) { | |
this.head = currentNode.next; | |
this.length--; | |
} else { | |
previousNode.next = currentNode.next; | |
this.length--; | |
} | |
console.log(`${value} is deleted from the list`); | |
} | |
} | |
} | |
getSize() { | |
console.log(this.length); | |
} | |
display() { | |
if (this.head == null) { | |
console.log('List is empty'); | |
} else { | |
let currentNode = this.head; | |
while (currentNode != null) { | |
console.log(currentNode.data); | |
currentNode = currentNode.next; | |
} | |
} | |
} | |
} | |
/* Example | |
var sl = new SingleLinkedList(); | |
sl.addToHead(1); | |
sl.addToHead(2); | |
sl.addToHead(3); | |
sl.addToHead(4); | |
sl.addToHead(5); | |
sl.addToLast(6) | |
sl.insertAfter(6,7) | |
sl.delete(7); | |
sl.search(6); | |
sl.search(7); | |
sl.display(); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment