队列列表输出不会像它应该的那样出现

时间:2016-06-16 20:22:40

标签: javascript queue

我应该得到的输出如下:

输入为10

迭代0:L1 = 2 3 4 5 6 7 8 9 10,Q1 =,

迭代1:L1 = 3 5 7 9,Q1 = 2

迭代2:L1 = 5 7,Q1 = 2 3

迭代3:L1 = 7,Q1 = 2 3 5

迭代4:L1 =,Q1 = 2 3 5 7

但是,我一直得到这个输出:

迭代0:L1 = 2,3,4,5,6,7,8,9,10,Q1 =

迭代4:L1 = Q1 = 2,3,5,7,

迭代1-3不会输出...

function Queue() {

var items = [];
var front=0;
var rear=0;
var size=20;

this.insert = function(element){
if(rear==size)
alert("Queue is full!");
else
{
items[rear]=element;
rear++;
}
};

this.delete = function(){
var deletedElement;
if(front==rear)
alert("Queue is empty!");
else
{
deletedElement=items[front];
front++;
}

return deletedElement;
};

this.deleteByElement = function(element) {
var index = items.indexOf(element);
if (index > -1) {
   items.splice(index, 1);
rear--;
}else {
alert("element not found ");
}
}

this.deleteByFactor = function(element) {
var isdeleted=false;
for(var i=front;i<rear;i++)
{
if(items[i]%element==0)
{
this.deleteByElement(items[i]);
        isdeleted=true;
}
}
        return isdeleted;
}

this.isEmpty = function(){
return rear == 0;
};

this.size = function(){
return items.length;
};

this.clear = function(){
items = [];
};

this.print = function(){
var queue='';
for(var i=front;i<rear;i++)
{
queue = queue + items[i]+",";
}
return queue;
};
}

function createNewQueue(queue)
{
var newqueue = new Queue();
var iter=0;
var start=2;
  
//Initial iteration
document.getElementById("output1").innerHTML = "Iteration "+ iter + ": L1 = " + queue.print() + " Q1 =" + newqueue.print();

while(!queue.isEmpty())
{
if(queue.deleteByFactor(start))
{
iter++;
newqueue.insert(start);

document.getElementById("output2").innerHTML = "Iteration " + iter + ": L1 = " + queue.print() + " Q1 =" + newqueue.print();
  
}
start++;
  
}   
  
}
var element = prompt("Enter Queue Element", "");
var queue = new Queue();
for(var i=2;i<=element;i++)
queue.insert(i);
//calling create new queue method
createNewQueue(queue);
<p>Queue Program</p>

<p id="output1"></p>
<p id="output2"></p>

1 个答案:

答案 0 :(得分:1)

你非常接近,看起来只有几件事。

首先,检查您的计数器变量。您希望在循环的每次迭代中更新iter,而不仅仅是在if语句中。

其次,每次迭代输出到div #output2都会覆盖现有输出。您想要附加新文本。

这样的事情会起作用:

更新代码

function createNewQueue(queue) {
  var newqueue = new Queue();
  var iter=0;
  var start=2;

  //Initial iteration
  var output = document.getElementById("output1");  //use a variable to store the output element
output.innerHTML = "<p>Iteration "+ iter + ": L1 = " + queue.print() + " Q1 =" + newqueue.print() + "</p>";

  while(!queue.isEmpty()) {
    iter++; //make sure to increment the counter with each loop

    if(queue.deleteByFactor(start)) {           
      newqueue.insert(start);

      //append the new output to the existing output  
      output.innerHTML = output.innerHTML + "<p>Iteration " + iter + ": L1 = " + queue.print() + " Q1 =" + newqueue.print() + "</p>";
    }
    start++;
  } //while    
} //createNewQueue()

<强>输出

  

迭代0:L1 = 2,3,4,5,6,7,8,9,10,Q1 =
  迭代1:L1 = 3,5,7,9,Q1 = 2,
  迭代2:L1 = 5,7,Q1 = 2,3,
  迭代4:L1 = 7,Q1 = 2,3,5,
  迭代6:L1 = Q1 = 2,3,5,7,

你可以看到它在这个JS小提琴中工作:https://jsfiddle.net/igor_9000/abnbpp01/

希望有所帮助!