Delete a particular item in a queue without using delete method

q[$] = {12,3,45,23};

How to delete 45 from the queue without using delete or pop_back methods to pop front or back items

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.

In reply to KillSteal:

why do you say queues are inefficient to delete elements in the middle of the queue ?

besides static or dynamic array, both queues and associative arrays allow us to delete an item based on the index using the delete(index) method.

please can you elaborate more on other efficient data structures besides queues ?

In reply to rlraj_2020:

I don’t agree with that explanation. Queues are the best kind of array for adding or deleting one element at a time. While it might be true that accessing (reading or writing) an element in the middle queue might be less efficient than other array kinds, having to reconstruct the entire queue because an element gets added or deleted is going to be less efficient. It would be much better to write this as

module test();
  int q[$] = {12,3,45,23};
  int b[$]; // all 'find' methods return a queue in case nothing is found.
  initial begin
    b = q.find_first_index(x) with (x==45);
    if (b.size() == 1) q.delete(b[0]);
  end
endmodule

In reply to dave_59:

Agree. But the dude at the top was asking for an interview answer without using the delete() method. So KillSteal’s solution was good but I was just curious about his statement that says

If you want to delete elements from the middle, Queues are in-efficient, and you may want to consider other data structures

In reply to rlraj_2020:

In reply to dave_59:
Agree. But the dude at the top was asking for an interview answer without using the delete() method. So KillSteal’s solution was good but I was just curious about his statement that says

The statement you quoted is what I disagree with. Queues are the most effecent kind of array for deleting a single element, regardless of whether they are in the middle or not.

In reply to dave_59:

In reply to rlraj_2020:
I don’t agree with that explanation. Queues are the best kind of array for adding or deleting one element at a time. While it might be true that accessing (reading or writing) an element in the middle queue might be less efficient than other array kinds, having to reconstruct the entire queue because an element gets added or deleted is going to be less efficient. It would be much better to write this as

module test();
int q[$] = {12,3,45,23};
int b[$]; // all 'find' methods return a queue in case nothing is found.
initial begin
b = q.find_first_index(x) with (x==45);
if (b.size() == 1) q.delete(b[0]);
end
endmodule

Thanks for the explanation, but here the delete method is used which is not expected.

In reply to Radha9:

This is an old and long conversation thread. Probably didn’t make this clear enough. I was commenting on the explanation given by KillSteal, not the concatenation example code. which is one way of answering the original question.

My code was showing the most efficient way to delete one element(45) from the queue.