# Differences

This shows you the differences between two versions of the page.

 algorithm:queue_list-based [2014/07/12 22:26]will algorithm:queue_list-based [2016/04/03 00:21] (current)will Updated tests. Both sides previous revision Previous revision 2016/04/03 00:21 will Updated tests.2015/02/02 08:28 external edit2015/01/29 21:20 will 2015/01/29 09:37 will 2014/07/26 23:42 will 2014/07/12 22:26 will 2014/07/12 22:25 will created Next revision Previous revision 2016/04/03 00:21 will Updated tests.2015/02/02 08:28 external edit2015/01/29 21:20 will 2015/01/29 09:37 will 2014/07/26 23:42 will 2014/07/12 22:26 will 2014/07/12 22:25 will created Line 1: Line 1: - [algorithm Queue_list-based] + ======= λ Queue list-based ​====== - ====== Algorithm ====== + <​nowiki>​ + ======= Algorithm ​======= function Queue() { function Queue() { Line 34: Line 35: } } } } - return popped; + return popped? popped.data : null; } } - } + }​ - ​ + - ====== Support ====== + ======= Support ​======= // Initial starting queue. // Initial starting queue. Line 59: Line 59: ​ - ====== Options ====== + ======= Tests ======= + + function testA() { + var q = new Queue(); + q.enqueue(1);​ + q.enqueue(2);​ + q.enqueue(3);​ + + assert(q.dequeue() === 1); + assert(q.dequeue() === 2); + assert(q.dequeue() === 3); + assert(q.dequeue() === null, "End of queue."​);​ + } + + function testB() { + var q = new Queue(); + q.enqueue(1);​ + q.enqueue(2);​ + + assert(q.dequeue() === 1); + + q.enqueue(3);​ + q.enqueue(4);​ + + assert(q.dequeue() === 2); + + q.enqueue(5);​ + + assert(q.dequeue() === 3); + assert(q.dequeue() === 4); + assert(q.dequeue() === 5); + assert(q.dequeue() === null, "End of queue."​);​ + }​ + + ======= Options ​======= { { + "​title":​ "Queue list-based",​ "​height":​ "​450px",​ "​height":​ "​450px",​ "​preRunSource":​ true, "​preRunSource":​ true, Line 68: Line 103: ​ - ====== Visualisation ====== + ======= Visualisation ​======= <​html>​ <​html>​ Line 137: Line 172: var nodes = newNodesToDisplay.concat(listNodesToDisplay);​ var nodes = newNodesToDisplay.concat(listNodesToDisplay);​ ​ ​ - // Ensure ​every node has an id. + // Position and ensure ​every node has an id. + var nX = 30; + var nNewX = 130; + var nY = 100; for (var i=0; i<​nodes.length;​ i++) { for (var i=0; i<​nodes.length;​ i++) { idNode(nodes[i]);​ idNode(nodes[i]);​ + if (nodes[i].isnew) { + nodes[i].x = nNewX; + nodes[i].y = 20; + nNewX += 60; + } + else { + nodes[i].x = nX; + nodes[i].y = nY; + nX += 60; + if (nX > 300) { + nX = 30; + nY += 60; + } + } } } ​ ​ listVis.nodesToDisplay = nodes; listVis.nodesToDisplay = nodes; - listVis.pointersToDisplay = [{value:​list.first,​ name:"​first",​ id:-1}, {value:​list.last,​ name:"​last",​ id:-2}]; + listVis.pointersToDisplay = [{value:​list.first,​ name:"​first"​, x:15, y:20, id:-1}, {value:​list.last,​ name:"​last"​, x:60, y:20, id:-2}]; if (clean) { if (clean) { listVis.clear(duration);​ listVis.clear(duration);​ Line 191: Line 243: ​ ​ var poppedValue = x.lookupInScope("​popped"​);​ var poppedValue = x.lookupInScope("​popped"​);​ + ​ + svg.selectAll("​.data"​).classed("​selected",​ function(d) {return d===poppedValue;​});​ ​ ​ returnText.text(function() {console.log(poppedValue);​ returnText.text(function() {console.log(poppedValue);​ Line 220: Line 274: ​ - ​ + - ​ +