Go Premium for a chance to win a PS4. Enter to Win


creation of a queue

Posted on 2000-04-26
Medium Priority
Last Modified: 2010-04-15
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?
Question by:beachbumm
LVL 16

Accepted Solution

imladris earned 40 total points
ID: 2752523
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.

Author Comment

ID: 2752591
that sums it all up very well!!! thank you

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question