Assignment 6

by austinmillett

HTML

Please enter a number:
<input type="textbox" id="number" number="" />
<input type="button" id="button1" value="Enter number" onClick="populateQueue();" />
<br/>
<br>
<div id="output1">
  <br/>
  <div id="output2">

JavaScript

/*
Define the Node object
*/

function Node(_content) {
  this.content = _content;
  this.next = null;
  this.last = null;
  return this;
}

/* 
Define the Queue
*/

function Queue() {
  this.bottom = null;
  this.top = null;
  this.length = 0;

  this.enqueue = function(_content) {
    // create head if head is null
    if (this.bottom == null) {
      this.bottom = new Node(_content);
      this.top = this.bottom;

      this.length++;

      return this;
    } else {
      //else to top node
      var addedNode = new Node(_content);
      addedNode.last = this.top;
      this.top.next = addedNode;
      this.top = addedNode;
      this.length++;

      return this;
    }
  }

  this.dequeue = function() {
      // return null if Stack is null
      if (this.bottom == null) {
        return null;
      }
      // only one Node
      else if (this.bottom == this.top) {
        var t = this.bottom.content;
        this.bottom = null;
        this.next = null;
        this.last = null;

        this.length = 0;

        return t;

      } else {

        var t = this.bottom.content;
        this.bottom = this.bottom.next;
        this.bottom.last = null;
        this.length--;

        return t;
      }
    }
    // Print

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

    while (node != null) {
      str += node.content + ", ";
      node = node.next;
    }
    return str;
  }
}

function populateQueue() {

  var Q1 = new Queue();
  var Q2 = new Queue();
  var n = parseInt(document.getElementById("number").value);
  for (var i = 2; i <= n; i++) {
    Q1.enqueue(i);
  }

  document.getElementById("output1").innerHTML += "Q1 = " + Q1.toString();

  Eratosthenes = function() {
    while (Q1.length > 0) {
      var node = Q1.bottom;
      var p = Q1.dequeue();
      Q2.enqueue(p);
      for (var j = Q1.top.content - 2; j > 0; j--) {
        var x = Q1.dequeue();
        if (x % p != 0) {
          Q1.enqueue(x);
          Q1.next;
     ...