Skip to content

Instantly share code, notes, and snippets.

@jason-s13r
Created November 1, 2011 11:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jason-s13r/1330337 to your computer and use it in GitHub Desktop.
Save jason-s13r/1330337 to your computer and use it in GitHub Desktop.
Linked List in JavaScript.
<!DOCTYPE HTML>
<html>
<head>
<title>Linked List</title>
<script type="text/javascript"><!--
function ListNode(data) {
this.data = data;
this.next = null;
this.previous = null;
}
ListNode.prototype = {
toString: function() { return "" + this.data; }
};
function LinkedList(type) {
this._type = typeof type;
this._length = 0;
this._head = null;
this._tail = null;
}
LinkedList.prototype = {
size: function() { return this._length; },
addLast: function(data) {
var node = new ListNode(data);
if (this._head === null) {
this._head = node;
this._tail = this._head;
} else {
this._tail.next = node;
node.previous = this._tail;
this._tail = node;
}
this._length++;
},
addFirst: function(data) {
var node = new ListNode(data);
if (this._head === null) {
this._head = node;
this._tail = this._head;
} else {
node.next = this._head;
this._head.previous = node;
this._head = node;
}
this._length++;
},
removeLast: function() {
var node = this._tail;
this._tail = this._tail.previous;
this._tail.next = null;
this._length--;
return node;
},
removeFirst: function() {
var node = this._head;
this._head = this._head.next;
this._head.previous = null;
this._length--;
return node;
},
// Some aliases of the above:
append: function(data) { this.addLast(data); },
prepend: function(data) { this.addFirst(data); },
add: function(data) { this.addLast(data); },
remove: function(data) { this.removeLast(); },
push: function(data) { this.addFirst(data); },
pop: function() { return this.removeFirst(); },
enqueue: function(data) { this.addLast(data); },
dequeue: function() { return this.removeFirst(); },
peek: function() { return this._head; },
get: function(index) {
if (index == 0) { return this._head; }
if (index == this._length-1) { return this._tail; }
var current = this._head;
i = 0;
while (current) {
if (i == index) { return current; }
current = current.next;
i++;
}
},
toArray: function() {
a = [];
i = 0;
var current = this._head;
while (current) {
a[i] = current;
current = current.next;
i++;
}
return a;
},
printList: function() {
var current = this._head;
document.write("HEAD: ");
while (current) {
document.write(current.data);
if (current.next !== null) { document.write(" -> "); }
current = current.next;
}
document.write(" :TAIL<br />");
},
printListBackwards: function() {
var current = this._tail;
document.write("TAIL: ");
while (current) {
document.write(current.data);
if (current.previous !== null) { document.write(" <- "); }
current = current.previous;
}
document.write(" :HEAD<br />");
},
};
//--></script>
</head>
<body>
<script type="text/javascript"><!--
stack = new LinkedList();
stack.push("Alpha");
stack.push("Bravo");
stack.push(5);
stack.push(parseInt(stack.peek()) + 10);
stack.push("Charlie");
stack.push("Delta");
document.write("" + stack.size() + "<br />");
stack.printList();
stack.printListBackwards();
document.write("" + stack.size() + "<br />");
document.write("" + stack.pop() + "<br />");
for (i = 0; i < 10; i++) {
if (i%2 == 0) {
stack.append(i);
} else {
stack.prepend(i);
}
}
stack.printList();
stack.printListBackwards();
document.write("" + stack.get(0) + "<br />");
document.write("[" + stack.toArray() + "]<br />");
//--></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment