Created
October 31, 2014 15:45
-
-
Save LittleHelicase/54cd9fd6cd809c50d574 to your computer and use it in GitHub Desktop.
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
function ListNode(){ // SingleLinkedListNode | |
this.element = ""; | |
this.nextNode = null; | |
} | |
function LinkedList () { | |
var start, end; | |
var n; | |
this.create = function(){ | |
start = null; | |
end = null; | |
n = 0; | |
}; | |
this.first = function(){ | |
return start; | |
}; | |
this.last = function(){ | |
return end; | |
} | |
this.length = function(){ | |
return n; | |
}; | |
this.next = function(p){ | |
return p.nextNode; | |
} | |
this.retrieve = function(p){ | |
return p.element; | |
} | |
// Schiebe das neue Element in einen Knoten hinter p | |
this.insert = function(p, x){ | |
node = new ListNode(); | |
node.element = x; | |
if(p == null){ // p = NIL, Einfügen an erster Position | |
node.nextNode = start; | |
start = node; | |
}else{ | |
node.nextNode = p.nextNode; | |
p.nextNode = node; | |
} if(p == this.last()){ | |
end = node; | |
} | |
n = n + 1; | |
return node; | |
}; | |
// Hilfsfunktion um den Vorgänger von p zu finden | |
// null falls p == start | |
this.predecessor = function(p) { | |
// finde Vorgänger von p | |
currentNode = start; | |
while(currentNode) { | |
if(currentNode.nextNode == p) | |
break; | |
currentNode = next(currentNode); | |
} | |
return currentNode; | |
} | |
this.delete = function(p) | |
{ | |
pred = p.prevNode; | |
if(pred) { | |
pred.nextNode = p.nextNode; | |
} else { | |
start = p.nextNode; | |
} | |
if(p.nextNode != null){ | |
p.nextNode.prevNode = pred; | |
} else { | |
end = pred; | |
} | |
n = n - 1; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment