Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

initial implementation

  • Loading branch information...
commit 4431b339250f4e1a59a87316794ad6895594fd99 0 parents
@andrewrjones authored
2  .gitignore
@@ -0,0 +1,2 @@
+.*redcar
+node_modules
3  .gitmodules
@@ -0,0 +1,3 @@
+[submodule "ext/qunit"]
+ path = ext/qunit
+ url = https://github.com/jquery/qunit.git
82 doubly-linked-list.js
@@ -0,0 +1,82 @@
+function DoublyLinkedList() {
+ // pointer to first item
+ this._head = null;
+ // pointer to the last item
+ this._tail = null;
+ // length of list
+ this._length = 0;
+}
+
+DoublyLinkedList.prototype._createNewNode = function(data){
+ var node = {
+ data: data,
+ next: null,
+ prev: null
+ };
+ return node;
+};
+
+DoublyLinkedList.prototype.append = function(data){
+ var node = this._createNewNode(data);
+
+ if (this._length === 0) {
+
+ // first node, so all pointers to this
+ this._head = node;
+ this._tail = node;
+ } else {
+
+ // put on the tail
+ this._tail.next = node;
+ node.prev = this._tail;
+ this._tail = node;
+ }
+
+ // update count
+ this._length++;
+};
+
+DoublyLinkedList.prototype.prepend = function(data){
+ var node = this._createNewNode(data);
+
+ if (this.first === null) {
+
+ // we are empty, so this is the first node
+ // use the same logic as append
+ this.append(node);
+ return;
+ } else {
+
+ // place before head
+ this._head.prev = node;
+ node.next = this._head;
+ this._head = node;
+ }
+
+ // update count
+ this._length++;
+};
+
+DoublyLinkedList.prototype.remove = function(index){
+ throw "Not implemented";
+};
+
+DoublyLinkedList.prototype.head = function(){
+ return this._head;
+};
+
+DoublyLinkedList.prototype.tail = function(){
+ return this._tail;
+};
+
+DoublyLinkedList.prototype.item = function(index){
+ if (index >= 0 && index < this._length){
+ var node = this._head;
+ while (index--) { node = node.next; }
+ return node;
+ }
+};
+
+DoublyLinkedList.prototype.size = function(){
+ return this._length;
+};
1  ext/qunit
@@ -0,0 +1 @@
+Subproject commit f08dc894391c856fc9cfe715ec2198fab9af60d9
37 grunt.js
@@ -0,0 +1,37 @@
+module.exports = function(grunt) {
+
+ // Project configuration.
+ grunt.initConfig({
+ qunit: {
+ files: ['test/**/*.html']
+ },
+ lint: {
+ files: ['grunt.js', 'doubly-linked-list.js', 'lib/**/*.js', 'test/**/*.js']
+ },
+ watch: {
+ files: '<config:lint.files>',
+ tasks: 'default'
+ },
+ jshint: {
+ options: {
+ curly: true,
+ eqeqeq: true,
+ immed: true,
+ latedef: true,
+ newcap: true,
+ noarg: true,
+ sub: true,
+ undef: true,
+ boss: true,
+ eqnull: true,
+ node: true,
+ es5: true
+ },
+ globals: {}
+ }
+ });
+
+ // tasks
+ grunt.registerTask('default', 'lint qunit');
+
+};
9 package.json
@@ -0,0 +1,9 @@
+{
+ "name": "DoublyLinkedList",
+ "version": "0.0.1",
+ "private": true,
+ "dependencies": {},
+ "devDependencies": {
+ "grunt": "~0.3.7"
+ }
+}
28 test/doubly-linked-list.html
@@ -0,0 +1,28 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <meta charset="utf-8">
+ <title>DoublyLinkedList Test Suite</title>
+ <!-- Load local jQuery, removing access to $ (use jQuery, not $). -->
+ <script src='http://code.jquery.com/jquery-1.7.2.js' type='text/javascript'></script>
+ <script>jQuery.noConflict()</script>
+ <!-- Load local QUnit (grunt requires v1.0.0 or newer). -->
+ <link rel="stylesheet" href="../ext/qunit/qunit/qunit.css" media="screen">
+ <script src="../ext/qunit/qunit/qunit.js"></script>
+ <!-- Load local lib and tests. -->
+ <script src="../doubly-linked-list.js"></script>
+ <script src="doubly-linked-list_test.js"></script>
+</head>
+<body>
+ <h1 id="qunit-header">DoublyLinkedList Test Suite</h1>
+ <h2 id="qunit-banner"></h2>
+ <div id="qunit-testrunner-toolbar"></div>
+ <h2 id="qunit-userAgent"></h2>
+ <ol id="qunit-tests"></ol>
+ <div id="qunit-fixture">
+ <span>lame test markup</span>
+ <span>normal test markup</span>
+ <span>awesome test markup</span>
+ </div>
+</body>
+</html>
68 test/doubly-linked-list_test.js
@@ -0,0 +1,68 @@
+/*global QUnit:false, module:false, test:false, asyncTest:false, expect:false*/
+/*global start:false, stop:false ok:false, equal:false, notEqual:false, deepEqual:false*/
+/*global notDeepEqual:false, strictEqual:false, notStrictEqual:false, raises:false*/
+/*global DoublyLinkedList:false, jQuery:false*/
+(function($) {
+
+ module('DoublyLinkedList#unit', {
+ setup: function() {
+ this.elems = $('#qunit-fixture').children();
+ }
+ });
+
+ test("DoublyLinkedList", 23, function() {
+ var list = new DoublyLinkedList(),
+ node = null;
+
+ ok(list);
+ strictEqual(list.size(), 0);
+
+ // append items to the list
+ list.append('data1');
+ strictEqual(list.size(), 1);
+ list.append('data2');
+ strictEqual(list.size(), 2);
+ list.append('data3');
+ strictEqual(list.size(), 3);
+
+ // get tail item
+ node = list.tail();
+ ok(node);
+ strictEqual(node.data, 'data3');
+ strictEqual(node.prev.data, 'data2');
+ strictEqual(node.next, null);
+
+ // get at 2
+ node = list.item(1);
+ ok(node);
+ strictEqual(node.data, 'data2');
+ // check pointers
+ strictEqual(node.prev.data, 'data1');
+ strictEqual(node.next.data, 'data3');
+
+ // prepend an item
+ list.prepend('data4');
+ strictEqual(list.size(), 4);
+
+ // get new item
+ node = list.head();
+ ok(node);
+ strictEqual(node.data, 'data4');
+ strictEqual(node.prev, null);
+ strictEqual(node.next.data, 'data1');
+
+ // get at 2
+ node = list.item(1);
+ ok(node);
+ strictEqual(node.data, 'data1');
+ // check pointers
+ strictEqual(node.prev.data, 'data4');
+ strictEqual(node.next.data, 'data2');
+
+ // not yet implemented
+ raises(function(){
+ list.remove(1);
+ });
+ });
+
+}(jQuery));
Please sign in to comment.
Something went wrong with that request. Please try again.