Queue

In reply to Design Engineer:

  1. Pop operation reduces the queue size by 1 in each iteration. Foreach loop is similar to repeat loop. At start your queue size is 4 so there should be 4 iterations.
    1st iteration → q.size=3 iteration_completed=1
    2nd iteration → q.size=2 iteration_completed=2
    After this iteration you have q.size and iteration_completed equal value so foreach loop stops.

A better approach to pop all items would be-


module queue;
int q[$];
int item;  

  initial begin 
    q={1,2,3,4}; 
    $display("queue=%p",q); 
    
    //foreach(q[i])begin 
    while(q.size>0)begin // Better approach to pop all elements
      item = q.pop_back(); 
      //$display("pop_back values =%p",i,q); 
      $display("popped_item=%0d ,remaining q=%p",item,q); 
    end  
  end
endmodule


  1. For reversing you can use -

  int q[$],q1[$]
  q.reverse;
  q1=q;
// or
 q1 = {<<32{q}};

1 Like