Queue delete help

I have a queue memory {7,3,1,0,8,1,2,1};
I want to delete all index with 1 , so final output should be {7,3,0,8,2}

I tried but not getting the expected output .

Any help is appreciated .

Thanks,

In reply to MP:

When you delete an element of a queue, all the other elements shift positions so the index array is no longer valid.

There are several approaches you can take, but since this is a homework assignment, you should figure it out yourself. Perhaps if you delete only the first index from the queue and then recalculate index? Or maybe use the index array in reverse order somehow?

Can we achieve the same result through array locator methods ?

Below works ( Edit code - EDA Playground)
module queues;
//declaration
int queue[]; int index[];
int temp_var;
int que_1[]; int que_f[];

initial begin
//Queue Initialization:
queue = {7,3,1,0,8,1,2,1};

$display("Queue entries are %p",queue);
$display("Before Queue size is %0d",queue.size());

foreach (queue[i]) begin 
   if(queue[i] ==1)  
    begin 
      que_1.push_back(queue[i]); 
  end else begin 
    que_f.push_back(queue[i]); 
  end 
end 
     
$display("delete %0p , final %p",que_1, que_f); 

end
endmodule

In reply to MP:

why not simply using a foreach with delete? without storing it temporarily:


// Code your testbench here
// or browse Examples
module queues;
  //declaration
  int    queue[$];  
  initial begin
    //Queue Initialization:
    queue = {7,3,1,0,8,1,2,1};
    $display("Queue entries are %p",queue);
    $display("Before Queue size is %0d",queue.size());
    foreach (queue[i]) begin 
       if(queue[i] ==1)
         queue.delete(i);
    end 
    $display("After Queue size is %0d",queue.size());
    $display("Queue after delete, entries are %p",queue);
  end
endmodule

//EXPECTED output is {7,3,0,8,2 }

Other approaches could leverage find item


// Code your testbench here
// or browse Examples
module queues;
  //declaration
  int    queue[$], index[$], qf[$], tmp[$];  
  initial begin
    //Queue Initialization:
    queue = {7,3,1,0,8,1,2,1};
    $display("Queue entries are %p",queue);
    $display("Before Queue size is %0d",queue.size());   
    index = queue.find_index() with (item == 1);
    foreach (queue[ll]) begin
      tmp = index.find() with (item == ll);
      if(!(tmp.size() > 0)) qf.push_back(queue[ll]);
    end
    $display("Indexes are %p",index);
    $display("After Queue size is %0d",queue.size());
    $display("Queue after delete, entries are %p",qf);
  end
endmodule

//EXPECTED output is {7,3,0,8,2 }

//EXPECTED output is {7,3,0,8,2 }

In reply to Rsignori92:

Your first approach won’t work. Try setting ‘queue = {7,3,1,1,1,0,8,1,2,1};’ and see what happens.