User Tools

Site Tools


Differences

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

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
algorithm:queue_list-based [2014/07/12 22:26]
will
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>
 { {
 +    "​title":​ "Queue list-based",​
     "​height":​ "​450px",​     "​height":​ "​450px",​
     "​preRunSource":​ true,     "​preRunSource":​ true,
Line 68: Line 103:
 </​syntax>​ </​syntax>​
  
-====== Visualisation ======+======= Visualisation ​=======
 <syntax html> <syntax html>
 <​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:
 </​div>​ </​div>​
 </​body>​ </​body>​
-</​html>​ +</​html></​syntax
-</​syntax>​+</nowiki>
algorithm/queue_list-based.1405229160.txt.gz · Last modified: 2015/02/02 08:23 (external edit)