Created
December 17, 2018 01:47
-
-
Save btg5679/c31ae73c72731d6284da8ec0cbb597c0 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
function Node(task) { // www.ja v a 2 s .com | |
this.name = task.name; | |
this.uri = task.uri | |
this.nextTask = undefined; | |
} | |
function LinkedList() { | |
this.head = new Node({name:"head"}); | |
this.find = find; | |
this.insert = insert; | |
this.remove = remove; | |
this.findPrevious = findPrevious; | |
this.remove = remove; | |
} | |
function find(item) { | |
var currNode = this.head; | |
while (currNode.name != item.name) { | |
currNode = currNode.nextTask; | |
} | |
return currNode; | |
} | |
function insert(newElement, item) { | |
var newNode = new Node(newElement); | |
var current = this.find(item); | |
newNode.nextTask = current.nextTask; | |
current.nextTask = newNode; | |
} | |
//Removing Nodes from a Linked List | |
function findPrevious(item) { | |
var currNode = this.head; | |
while (!(currNode.nextTask == null) && (currNode.nextTask.name != item)) { | |
currNode = currNode.nextTask; | |
} | |
return currNode; | |
} | |
function remove(item) { | |
var prevNode = this.findPrevious(item); | |
if (!(prevNode.nextTask == null)) { | |
prevNode.nextTask = prevNode.nextTask.nextTask; | |
} | |
} | |
var taskList = new LinkedList(); | |
taskList.insert({name:"C",uri:"http://456"}, {name:"head"}); | |
console.log(taskList) | |
const string = JSON.stringify(taskList.head) | |
console.log(JSON.parse(string)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment