Skip to content

Instantly share code, notes, and snippets.

@btg5679
Created December 17, 2018 01:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save btg5679/c31ae73c72731d6284da8ec0cbb597c0 to your computer and use it in GitHub Desktop.
Save btg5679/c31ae73c72731d6284da8ec0cbb597c0 to your computer and use it in GitHub Desktop.
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