Queues

You are going to create a Queue. (alternately you can create a list and simply implement enqueue and dequeue functions in the List – that will technically make it a queue). You will fill the first list with numbers consecutively numbered from 2 to n where n is entered by the user (we will call this Q1). When creating your Queue object use the correct function names for enqueue and dequeue functions. Again – sorry, cannot use an Javascript array in your implementation – you need to implement enqueue and dequeue.

by Neil Daley

HTML

<H2>
Sieve of Eratosthenes
</H2>

The Sieve of Eratosthenes is a program that locates all prime numbers in a list containinig values geater than zero.<br/><br/>

Example:<br/>
To locate all primes from 1 to 10, enter 10 in the textbox then press "List Prime #'s".<br/><br/>

<b>Enter a number between 0 and &#x221e.</b><br/><br/>

<input type="textbox" id="inputValue" onkeypress="handle(event)" /><br/>

<input type="button" id="buttonList" value="List Prime #'s'" onclick="handle_que()" style="color:white; background-color:blue" />

<input type="button" id="buttonClear" value="Clear List" onclick="clearScr()" style="color:white; background-color:blue" /><br/><br/>

<div id="newList"></div>
<div id="output"></div>

JavaScript

// Global variables
var dq = 0; // Dequeued item
var cdq = 0; // Current dequeued item
var lit = 0; // Current list iteration

// Get list length from input box. This is global value to be used throughtout program
var iv = document.getElementById("inputValue").value;

/*
This function is added to aid user in computer calculation.  User can press enter and values will be added to stack then calculated */
function handle(event) {
		// Using 'Keycode' and 'Which' for browser compatibility
  var key=event.keyCode || event.which;
  if (key==13) {
    handle_que();
    document.getElementById("inputValue").value = "";
  }
}

	// Queue list populates
var Node = function(_content) {
  this.next = null;
  this.previous = null;
  this.content = _content;
}

	// Defining Queue as Stack defined in previous assignment
var Queue = function() {
    this.last = null;
    this.first = null;
    this.length = 0;

    	// Enqueue function like push function in previous assignment
    this.enqueue = function(_content) {
        var node = new Node(_content);
        if (this.last == null && this.head == null) {
          this.first = node;
          this.last = node;
          this.length++;
          return this;
        }

        this.last.previous = node;
        node.next = this.last;
        this.last = node;
        this.length++;
        return this;
      }
      
      // Dequeue function as pop in previous assignment
    this.dequeue = function() {
        if (this.last == null) {
          alert("Queue is Empty");
          return null;
        }

        if (this.last == this.first) {
          this.first = null;
          this.last = null;
          this.length = 0;
          return null;
        }

        var a = this.first;
        this.first = this.first.previous;
        this.first.next = null;
        this.length--;

        return a;

      }
      //needed to display the contents of the queue
    this.toString = function() {
      var str = "";
      var node =...