Question: Please help! Why is this code not working at all? Is there an infinite loop? I need help making the html for this javascript queue
Please help! Why is this code not working at all? Is there an infinite loop? I need help making the html for this javascript queue that is a augemented list Not An Array You will fill the first list with numbers consecutively numbered from
to nwhere n is entered by the user
we will call this Q Create a second empty queue called Q Here is the algorithm;
Dequeue st element in Qwhich will be You will need to remember the value of this element we will call it X Enqueue this element into QQ is the list of primes Iterate and Dequeue each successive element of Q
If the value is divisible by Xgo to the next element if the value is not divisible by X enqueue back onto Qand go to the next element. Print the values of Q and Qafter each time through. When done go back to the beginning of the Q and repeat steps the first value will be the second time around
Sample output with input Iteration : QQIteration : QQIteration : QQ Iteration : Q Iteration : Q Q This is the code: Node class for the list
class Node
constructor value
this.value value;
this.next null;
Queue class using a list
class Queue
constructor
this.head null;
this.tail null;
Enqueue function using push
enqueue value
let node new Node value;
if thishead null
this.head node;
this.tail node;
else
this.tail.next node;
this.tail node;
Dequeue function using shift
dequeue
if thishead null
return null;
else
let node this.head;
this.head this.head.next;
if thishead null
this.tail null;
return node.value;
Peek function to get the front element
peek
if thishead null
return null;
else
return this.head.value;
IsEmpty function to check if the queue is empty
isEmpty
return this.head null;
Print function to display the queue
print
let node this.head;
let result ;
while node null
result node.value ;
node node.next;
return result;
Sieve of Eratosthenes algorithm using two queues
function sieveOfEratosthenesn
Create two queues
let Q new Queue ;
let Q new Queue ;
Fill Q with numbers from to n
for let i ; i n; i
Qenqueue i;
Initialize the iteration number
let iteration ;
Loop until Q is empty
while QisEmpty
Dequeue the first element of Q and call it X
let X Qdequeue ;
Enqueue X to Q
Qenqueue X;
Iterate over the remaining elements of Q
while QisEmpty
Dequeue the next element of Q and call it Y
let Y Qdequeue ;
Check if Y is divisible by X
if Y X
If not, enqueue Y back to Q
Qenqueue Y;
Increment the iteration number
iteration;
Print the values of Q and Q
const outputDiv document.getElementByIdoutput;
outputDiv.innerHTML Iteration $iteration: Q $Qprint Q $Qprint;
Test the code with n
sieveOfEratosthenes ;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
