User Tools

Site Tools


Differences

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

Link to this comparison view

Next revision
Previous revision
algorithm:queue_list-based [2014/07/12 22:25]
will created
algorithm:queue_list-based [2016/04/03 00:21] (current)
will Updated tests.
Line 1: Line 1:
-[algorithm Queue_list-based]+======= λ Queue list-based ​======
  
-====== Algorithm ======+<​nowiki>​ 
 +======= Algorithm ​=======
 <syntax js> <syntax js>
 function Queue() { function Queue() {
Line 34: Line 35:
             }             }
         }         }
-        return popped;+        return popped? popped.data : null;
     }     }
-} +}</​syntax>​
-</​syntax>​+
  
-====== Support ======+======= Support ​=======
 <syntax js> <syntax js>
 // Initial starting queue. // Initial starting queue.
Line 59: Line 59:
 </​syntax>​ </​syntax>​
  
-====== Options ======+======= Tests ======= 
 +<syntax js> 
 +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."​);​ 
 +}</​syntax>​ 
 + 
 +======= Options ​=======
 <syntax js> <syntax js>
 { {
-    "​height":​ "480px",+    ​"​title":​ "Queue list-based",​ 
 +    ​"​height":​ "450px",
     "​preRunSource":​ true,     "​preRunSource":​ true,
     "​persistentGlobals":​ ["​queue"​]     "​persistentGlobals":​ ["​queue"​]
Line 68: Line 103:
 </​syntax>​ </​syntax>​
  
-====== Visualisation ======+======= Visualisation ​=======
 <syntax html> <syntax html>
 <​html>​ <​html>​
 <​head>​ <​head>​
 <script src="​http://​d3js.org/​d3.v3.js"></​script>​ <script src="​http://​d3js.org/​d3.v3.js"></​script>​
-<link rel="​stylesheet"​ type="​text/​css"​ href="file:///​Users/​will/​Dropbox/​list.css"/>​ +<link rel="​stylesheet"​ type="​text/​css"​ href="algorithms-lib/​list.css"/>​ 
-<script src="file:///​Users/​will/​Dropbox/​list.js"></​script>​+<script src="algorithms-lib/​list.js"></​script>​
 <​script>​ <​script>​
 function setup() { function setup() {
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:
 </​div>​ </​div>​
 </​body>​ </​body>​
-</​html>​ +</​html></​syntax
-</​syntax>​+</nowiki>
algorithm/queue_list-based.1405229119.txt.gz · Last modified: 2015/02/02 08:23 (external edit)