Last active
May 15, 2016 14:31
-
-
Save Williammer/7f5a49b8de5778e4ce742c3ebbf494de to your computer and use it in GitHub Desktop.
LinkList implementations.
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(element) { | |
this.element = element; | |
this.next = null; | |
this.previous = null; | |
} | |
function LList() { | |
this.head = new Node("head"); | |
this.find = find; | |
this.insert = insert; | |
this.display = display; | |
this.remove = remove; | |
this.findLast = findLast; | |
this.dispReverse = dispReverse; | |
} | |
function dispReverse() { | |
var currNode = this.head; | |
currNode = this.findLast(); | |
while (!(currNode.previous == null)) { | |
console.log(currNode.element); | |
currNode = currNode.previous; | |
} | |
} | |
function findLast() { | |
var currNode = this.head; | |
while (!(currNode.next == null)) { | |
currNode = currNode.next; | |
} | |
return currNode; | |
} | |
function remove(item) { | |
var currNode = this.find(item); | |
if (!(currNode.next == null)) { | |
currNode.previous.next = currNode.next; | |
currNode.next.previous = currNode.previous; | |
currNode.next = null; | |
currNode.previous = null; | |
} | |
} | |
// findPrevious is no longer needed | |
/*function findPrevious(item) { | |
var currNode = this.head; | |
while (!(currNode.next == null) && | |
(currNode.next.element != item)) { | |
currNode = currNode.next; | |
} | |
return currNode; | |
}*/ | |
function display() { | |
var currNode = this.head; | |
while (!(currNode.next == null)) { | |
console.log(currNode.next.element); | |
currNode = currNode.next; | |
} | |
} | |
function find(item) { | |
var currNode = this.head; | |
while (currNode.element != item) { | |
currNode = currNode.next; | |
} | |
return currNode; | |
} | |
function insert(newElement, item) { | |
var newNode = new Node(newElement); | |
var current = this.find(item); | |
newNode.next = current.next; | |
newNode.previous = current; | |
current.next = newNode; | |
} | |
var cities = new LList(); | |
cities.insert("Conway", "head"); | |
cities.insert("Russellville", "Conway"); | |
cities.insert("Carlisle", "Russellville"); | |
cities.insert("Alma", "Carlisle"); | |
cities.display(); | |
console.log(); | |
cities.remove("Carlisle"); | |
cities.display(); | |
console.log(); | |
cities.dispReverse(); |
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(element) { | |
this.element = element; | |
this.next = null; | |
} | |
function LList() { | |
this.head = new Node("head"); | |
this.find = find; | |
// for circular llist | |
// this.head.next = this.head; | |
this.insert = insert; | |
this.display = display; | |
this.findPrevious = findPrevious; | |
this.remove = remove; | |
} | |
function advance(n) { | |
// the current node is moved n nodes forward in the list. | |
var currNode = this.head, | |
i = 0; | |
this.remove(this.head); | |
while (i < n) { | |
if (!(this.head.next == null)) { | |
this.head = this.head.next; | |
i++; | |
} | |
} | |
this.insert(currNode, this.head); | |
} | |
function remove(item) { | |
var prevNode = this.findPrevious(item); | |
if (!(prevNode.next == null)) { | |
prevNode.next = prevNode.next.next; | |
} | |
} | |
function findPrevious(item) { | |
var currNode = this.head; | |
while (!(currNode.next == null) && | |
(currNode.next.element != item)) { | |
currNode = currNode.next; | |
} | |
return currNode; | |
} | |
function display() { | |
var currNode = this.head; | |
while (!(currNode.next == null)) { | |
console.log(currNode.next.element); | |
currNode = currNode.next; | |
} | |
// for circular linked list | |
// while (!(currNode.next == null) && | |
// !(currNode.next.element == "head")) { | |
// print(currNode.next.element); | |
// currNode = currNode.next; | |
// } | |
} | |
function find(item) { | |
var currNode = this.head; | |
while (currNode.element != item) { | |
currNode = currNode.next; | |
} | |
return currNode; | |
} | |
function insert(newElement, item) { | |
var newNode = new Node(newElement); | |
var current = this.find(item); | |
newNode.next = current.next; | |
current.next = newNode; | |
} | |
var cities = new LList(); | |
cities.insert("Conway", "head"); | |
cities.insert("Russellville", "Conway"); | |
cities.insert("Carlisle", "Russellville"); | |
cities.insert("Alma", "Carlisle"); | |
cities.display(); | |
console.log(); | |
cities.remove("Carlisle"); | |
cities.display(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment