Question: I need help making the html for this javascript queue that is a augemented list ( Not An Array ) . You will fill the
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 n where n is entered by the user we will call this 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 X go to the next element
if the value is not divisible by X enqueue back onto Q and go to the next element.
Print the values of Q and Q after 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 : Q QIteration : Q Q Iteration : Q Q Iteration : Q Q Iteration : Q Q This is the code so far: 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;
console.log result;
Sieve of Eratosthenes algorithm using two queues
function sieveOfEratosthenes n
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
console.log Iteration iteration : Q;
Qprint ;
console.log Q;
Qprint ;
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
