We are just learning about circular queue in class, and I got a few questions. Since we define the tail as the empty space next to the last value, such as shown below:
|1| |3|4|5|6|
The head will be pointing to the number 3, and the tail will be pointing to the empty space between 1 and 3. I am confused on what happens if that space is filled up, so for example below:
|1|2|3|4|5|6|
Then the head will still be pointing to 3, but the tail needs to be pointing to the next box after the blank box before, thus it will be pointing to 3, or the header. What should I do about this?
When this situation occurs, your queue is full. You have a few options when to deal with new items that can be pushed:
discard the pushed event: only when some other item is popped can now items be pushed again. Neither
headnortailis updated.Example: Think of an event queue that has filled up, new requests are simple ignored.
discard (pop) the oldest event on the queue: in this case you update both the
headandtailpointer one place.Example: buffering incomming image frames from a webcam for processing. For a 'live' feed you may prefer to discard the older frames when the processing has a hickup.
create a bigger queue: that is, allocate more memory on the fly
Example: you use a circular queue since it an efficient implementation that doesn't require memory allocation most of the time when you push items. However, you do not want to loose items on the queue so you allow reallocating more memory once in a while
What the right action is depends on your application.
PS.: Your implementation is based on keeping an empty slot in your queue to distinguish full and empty buffer. Another option is to keep a counter on the number of elements in your queue to make this distinction. More info can be found on Circular buffer (Wikipedia).
As I thought of it, a circular queue is circular in part because it does not get "filled up". It will simply always hold a set number of elements, throwing out old ones as needed.
So you will never fill up the empty space; if you insert a new element, you'll remove an old element, so there will still be an empty space.
In other words, in your diagram, if you insert a new number, say 0, your a queue that looks like the following (assuming 1 is the last element, 3 the first):
|1| |3|4|5|6|
it will then look as follows:
| |0|3|4|5|6|
However, some implementations of a circular queue simply throw an exception/error when their full length is reached, if that's what you want. Check out for example this.
Here is the most succinct explanation about queues that I have ever found. You can expand on your queues based on this foundation. Source: "Algorithms," by Robert Sedgewick.
const max=100;
var queue: aray[0..max]of integer;
head,tail: integer;
procedure put(v:integer);
begin
queue[tail] := v;
tail := tail + 1;
if (tail > max) then tail := 0;
end;
function get: integer;
begin
get := queue[head];
head := head + 1;
if (head > max) then head := 0;
end;
procedure queueinitialize;
begin
head := 0;
tail := 0;
end;
function queueempty: boolean;
begin
queueempty := (head = tail);
end;
"It is necessary to maintain two indices, one to the beginning of the queue (head) and one to the end (tail). The contents of the queue are all the elements in the array between head and tail, taking into account the "wraparound" back to 0 when the end of the array is encountered. If head = tail then the queue is defined to be empty; if head = tail + 1, or tail = max and head = 0, it is defined to be full."
When the head and tail points to the same place then we say that the queue is full.No more elements can be added.To add any element ,you will have to remove an element from the queue.With this the head will get incremented and a space is again generated.
来源:https://stackoverflow.com/questions/11352415/full-circular-queue