Delete a particular item in a queue without using delete method

In reply to verific_engi:

If this is an interview question, the interviewer is trying to understand how you think and approach this question. Note that it;s highly unlikely that you would want to implement the above in a real practical use case. It’s just a tricky question for interviews.

Start your answer with : Queues are good at deleting/adding at the beginning or at the end. If you want to delete elements from the middle, Queues are in-efficient, and you may want to consider other data structures. This answer establishes that you have given the questions more high level thought.

However, if you still want to achieve the above :

As unpacked array methods can be used on queues, you can find the index at which 45 is present, and concatenate two queues skipping 45. Something like below :


module test();
  int q[$] = {12,3,45,23};
  int b[];
  initial begin
    b = q.find_first_index(x) with (x==45);
    q = {q[0:b[0]-1],q[b[0]+1:$]};
  end
endmodule

With the above idea, you will have to take care of corner cases like when the element is the first or the last in a queue.