queue

queue

by chris richarde

HTML

Choose quantity to sieve through for primes<br/>
<input type="textbox" id="value" />
<input type="button" id="stack" value="sieve" onClick="Math();" />
<p id="output"></p><br/>

JavaScript

var Node = function(content) {
    this.next = null;
    this.last = null;
    this.content = content;

  }

  var Queue = function() {
    this.bottom = null;
    this.top = null;
    this.length=0;
    
    this.enqueue = function(content) {
      if (this.bottom == null) {
        this.bottom = new Node(content);
        this.top = this.bottom;
        this.length++;
        return this;
      }
      
      var addedNode = new Node(content);
      addedNode.last = this.top;   
      this.top.next = addedNode;   
      this.top = addedNode;     
      this.length++;
      return this;
    }

    this.dequeue = function() {
      if (this.bottom == null) {
        alert("empty queue");
        return null;
      }
      if (this.bottom == this.top) {
      var a = this.bottom.content;
      this.bottom=null;
      this.next=null;
      this.last=null;
      this.length=0;
      return a;
      }
      
      // Now remove top Node
      var a = this.bottom.content;   // hold value for return
      this.bottom = this.bottom.next;
      this.bottom.last = null;
      this.length--;
      return a;
    }

    this.toString = function() {
      var str = "";
      var node = this.bottom;

      while (node != null) {
        str += node.content + ":";
        node = node.next;
      }
      return str;
    }
  }
	function ClearDisplay(){
  document.getElementById("output").innerHTML="";
  }
  function Print()
  {
  	document.getElementById("output").innerHTML=Q1.toString();
  }
  function Math()
{ 
	
  var i = 1;
  var Q1 = new Queue();
  var Q2 = new Queue();
  var Q3 = new Queue();
  var value = document.getElementById("value").value;
  
  for(var n=2;n<=value;n++){
  	Q1.enqueue(n);
  
  document.getElementById("output").innerHTML=
  "Q List = " + Q1.toString()+"<br/>";
  
  
  }
 Q2.enqueue(Q1.dequeue());
  
  document.getElementById("output").innerHTML+=
  "*round* : " + i + "<br/>"+
  "Q List = " + Q1.toString()+"<br/>"+
  "Q Prime = " +...