• C

creation of a queue

if i create a queue and a sequence of M insertions and N deletions are executed (in any order), then the queue will contain which elements? I say it is the smaller of M-N and MaxQueue. Am i right or wrong on this?
Who is Participating?
imladrisConnect With a Mentor Commented:
Some constraints need to be indicated. I assume you are considering a FIFO queue (first in, first out) which is what happens at a cash register.
In this case one would normally also assume there will never be a deletion, unless there is something in the queue.

Now, if there is no maximum to the queue then M-N is the number of elements in the queue after M insertions and N deletions. And they are the M-N elements that were inserted LAST.

If there is a maximum size to the queue, an insertion can fail. For each failed insertion you will have one less element left in the queue at the end. However, the order now matters a lot. For instance, if insertions and deletions occur alternating, the queue will contain 0 or 1 elements until deletions run out, and then insertions will (potentially) fill up the queue. At that point you would indeed have the lesser of M-N or MaxQueue.

On the other hand, if all the insertions happen first, and then all the deletions, you would have the lesser of M-N or MaxQueue-N (depending on whether the insertions filled the queue or not).

Of course, if it isn't a FIFO queue, or deletions can happen on an empty queue, the results are different, or even less defined.
beachbummAuthor Commented:
that sums it all up very well!!! thank you
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.