Question

You are going to create a Queue. (alternately you can create a list and simply implement...

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.

Create a second empty Queue Q2.

Once you have the first Queue filled we are going to use a technique called Sieve of Eratosthenes which uses first queue to fill the second queue. You will need to look at the algorithm for this https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes

Here is the algorithm;

1. Dequeue 1st element in Q1 (which will be 2). You will need to remember the value of this element – we will call it X.

2. Enqueue this element into Q2 (Q2 is the list of primes)

3. Iterate and Dequeue each successive element of Q1

If the value is divisible by X, go to the next element

if the value is not divisible by X enqueue back onto Q1, go to the next element.

4. Print the values of Q1 and Q2 after each time through.

5. When done go back to the beginning of the Q1 and repeat steps 1-3 (the first value will be 3 the second time around)

Sample output with input 10

Iteration 0: Q1 = 2 3 4 5 6 7 8 9 10, Q2 = ,

Iteration 1: Q1 = 3 5 7 9, Q2 = 2

Iteration 2: Q1 = 5 7, Q2 = 2 3

Iteration 3: Q1 = 7, Q2 = 2 3 5

Iteration 4: Q1 = , Q2 = 2 3 5 7

You must implement a working Queue with both enqueue and dequeue functions. You must then use your queues in the Erasthones algorithm for the remaining half credit.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

Program:

<!DOCTYPE html>

<html>
<head>
<script src="Queue.js"></script>
</head>
<body>

Enter the value of n: <input type="number" id="myNumber" value="2">

<button onclick="myFunction()">Okay</button>

<p id = "myDiv"></p>
</body>
</html>

Javascript code:

function myFunction() {
var x = document.getElementById("myNumber").value;
//document.getElementById("demo").innerHTML = x;
   var firstQueue = createQueue(x)
   var secondQueue = new Queue();
   displayQueue(firstQueue, 0, 1)
   displayQueue(secondQueue, 0, 2)
   sieveAlgorithm(firstQueue,secondQueue)
}

function createQueue(n)
{
   var queue = new Queue();
   for(i=2; i<=n; i++)
       queue.enqueue(i);
   return queue;
}

function sieveAlgorithm(firstQueue, secondQueue)
{
   var counter = 1;
   var lengthOfFirstQueue = firstQueue.getLength();
   while(firstQueue.hasElements())
   {
       var firstElem = firstQueue.dequeue(0)
       console.log("Element removed:" + firstElem)
       secondQueue.enqueue(firstElem);
       for(m=0;m<lengthOfFirstQueue; m++)
       {
           var value = firstQueue.currentElem(m);
           console.log("Element Value:" + value)
           if(value%firstElem == 0)
           {
              
               console.log("Element removed:" + firstQueue.dequeue(m));
           }
       }
       displayQueue(firstQueue, counter, 1)
       displayQueue(secondQueue, counter, 2)
       counter++;
   }  
  
}
function displayQueue(queue_list, itr, qnumber)
{
   var x = "";
  
   if(qnumber == 1)
       x = "Iteration "+itr+": Q1 = ";
   else
       x = ",Q2 = "
   for(j=0; j<queue_list.getLength(); j++ )
   {
       x = x+ queue_list.currentElem(j);
       x = x+" ";
   }
       var header = document.createElement("h3");
       var t;
       if(qnumber == 1)
       {
           var att = document.createAttribute("id");
           att.value = "itr"+itr;   
           header.setAttributeNode(att);
           t = document.createTextNode(x);
           header.appendChild(t);
           document.getElementById("myDiv").appendChild(header);
       }
       else
       {
           document.getElementById("itr"+itr).innerHTML = document.getElementById("itr"+itr).innerHTML + x;
       }
      
}

function Queue()
{
   var a=[],b=0;
   this.getLength=function()
   {
       return a.length-b
   };
   this.isEmpty=function()
   {
       return 0==a.length
   };
   this.enqueue=function(value)
   {
       a.push(value)
   };
   this.dequeue=function(index)
   {
       if(a.length != 0)
       {
           var c=a[index];
           a.splice(index,1)
           //2*++b>=a.length&&(a=a.slice(index),b=0);
           console.log("At Dequeue:" +c);
           return c
       }
   };
   this.peek=function()
   {
       return 0<a.length?a[b]:void 0
   }
   this.currentElem=function(index)
   {
       return a[index];
   }
   this.hasElements=function()
   {
       for(z=0;z<a.length;z++)
       {
           if(typeof a[z] != 'undefined')
           {
               return true;
           }
           else
               return false;
       }
   }
};

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
You are going to create a Queue. (alternately you can create a list and simply implement...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Suppose we want to implement a circular queue using an array that has an initial capacity...

    Suppose we want to implement a circular queue using an array that has an initial capacity (maximum number of elements) MAX. A circular queue is like a regular queue except that elements can be enqueued or dequeued by wrapping around it. Assume we enqueue on the tail and dequeue from the head. An example circular queue with sample operations is shown below: head head tail head tail tail head Enqueue(9) a) Write a program in C that implements this circular...

  • » Part A: Stack Create a Stack struct that is a wrapper for your linked list o You should impleme...

    Using C Please comment » Part A: Stack Create a Stack struct that is a wrapper for your linked list o You should implement the following functions that take a stack o void push(Stack * stack, int value) ● int pop(Stack * stack) Prompt the user to input 5 integers, and use the stack to reverse the integers Print the result to the screen. o o » Part B: Queue o Create a Queue struct that is a wrapper for...

  • solving using C. Use a singly linked list to implement a priority queue with two operations:...

    solving using C. Use a singly linked list to implement a priority queue with two operations: enqueue and dequeue. Each node contains an integer value, a priority, and a pointer to the next node. The priority is a value between 1- 10 (where 10 is the highest priority). When a value is added to the queue, it is added with a value and priority. When a value is removed from the priority queue, the first element with the highest priority...

  • You need to implement a queue based on the doubly linked list. (In C++) **PLEASE follow...

    You need to implement a queue based on the doubly linked list. (In C++) **PLEASE follow the format** **Don't worry about the readme.txt** THANK YOU SO SO MUCH! You have to implement the doubly linked list in DList.h and DList.cpp and the queue in the LinkedQueue.h and LinkedQueue.cpp; all as template classes. You have to provide the main.cpp file as we discussed in class to allow the user to interact with your queue using enqueue, dequeue, front, back, size, empty,...

  • C++ I need help to create a program that executes a simple queue data type. You...

    C++ I need help to create a program that executes a simple queue data type. You must define the queue data type and use the enqueue(), dequeue(), and displayQueue() functions. (Dont use C++ STL to execute queue logic) You may use a linked list , or just use a regular C array of integers. The input file (testfile.txt) will be the list of operations to carry out on a queue. Example input file: enqueue 6 enqueue 8 dequeue enqueue 4...

  • (Data Strcture) Tool(s)/Software Java programming language with NetBeans IDE. Description Implementing a Linear Queue using a...

    (Data Strcture) Tool(s)/Software Java programming language with NetBeans IDE. Description Implementing a Linear Queue using a Singly Linked-List and Implementing a Priority Queue Tasks/Assignments(s) ■ Write your own program to implement priority queue using the Linked-List and implement the Enqueue, Dequeue and Display methods. implement the Merge methods in your main program that receive Q1 and Q2 and merge the two queues into one queue. Public static PriorityQueue MergeQueue(PriorityQueue Q1,PriorityQueue Q2) Write main program to test priority queue class and...

  • Python use dequeue and enqueue methods, alter the dequeue method and the enqueue method. remove items...

    Python use dequeue and enqueue methods, alter the dequeue method and the enqueue method. remove items from the first element of the list (the front of the queue) and return this value and you need to add items to the last element of the list (the rear of the queue). 4. (20 points) Programming Exercise 5 from Chapter 3 of your textbook. Name the class Queuex, and use the other method names given in Chapter 3.11 in your digital textbook....

  • Write a C++ program to implement a queue using linked lists. You can use the queue...

    Write a C++ program to implement a queue using linked lists. You can use the queue data structure from the Standard Template Library (STL). The program should provide the following functionality: Enqueue data into queue Dequeue data from queue Print data at the front Print data at the back Print the entire queue Check if the queue is empty Print the number of elements in the queue Test your program using at least the following test cases (considering the queue...

  • Array-based Queue Lecture 6 Two Class Exercises | Class Exercise #1 - Create an array-based queue that holds value...

    Array-based Queue Lecture 6 Two Class Exercises | Class Exercise #1 - Create an array-based queue that holds values of double data type. 1.) Create a program that produces the following output OUTPUT: Q Quit Enter your choice: e Enter an item: 1.1 E Enqueue D Dequeue s-show queue ← showMenuO function called in main) OQuit // screen clears-.. continue enqueuing.screen clearing with each iteration Enter your choice: e Queue is full. E Enqueue D Dequeue s Show queue 0...

  • I am to implement a simple simulation that supports a stack and a queue of items being either enqueued and dequeued (ont...

    I am to implement a simple simulation that supports a stack and a queue of items being either enqueued and dequeued (onto the queue) or pushed and popped (onto the stack). I are required to use STL data structures to implement and create the stack and queue for my program. ----- testfile1.tst -------- enqueue 5 enqueue 7 push blooper push rookie dequeue push demerits pop enqueue 3 enqueue 8 push showplace enqueue 9 dequeue pop dequeue push palmetto push zebra...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT