Re: Fixed-size FIFO queue



I hope Dann answered it.
The operation may not be look like FIFO.
But when trying to get o/p u will feel like a FIFO.

Dann Corbit wrote:
"Jack" <junw2000@xxxxxxxxx> wrote in message
news:1149883724.178628.289150@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
I want to implement a fixed-size FIFO queue for characters.

I only want to use array not linked list.

For example,

const int N = 10;
char c_array[N];

The question is when the queue is full, there are ten characters in the
queue. When the 11th character comes, c_array[0] will be popped out. I
have to shift all the rest elements in the queue to one slot left. That
is,
c-array[0] = c-array[1];
c-array[1] = c-array[2];
c-array[2] = c-array[3];

...............

c-array[7] = c-array[8];
c-array[8] = c-array[9];

Then I can put the 11th character into c-array[9].

Is there a better way to implement it if only array is allowed?

I know linked list can solve the problem. But all the elements in the
queue have to be consecutive in memory, so linked list can not meet the
requirement.

Classical answer is to use a ring buffer. You can use modular arithmetic to
store data in the slots.
That way you don't have to move the data when the array gets full. You will
have to track the head and tail and you will also have to decide how to
handle queue full situations.

.



Relevant Pages

  • Re: what is a stack
    ... There's FILO and FIFO, they're both implemented as a regular old array (at ... Consider a stack of plates in a cafeteria, ... aka a queue. ...
    (comp.lang.java.programmer)
  • Re: Fixed-size FIFO queue
    ... I only want to use array not linked list. ... The question is when the queue is full, there are ten characters in the ... When the 11th character comes, ... store data in the slots. ...
    (comp.lang.c)
  • Re: Fixed-size FIFO queue
    ... I only want to use array not linked list. ... The question is when the queue is full, there are ten characters in the ... When the 11th character comes, ...
    (comp.lang.c)
  • Re: writing ISR for UART
    ... so if the interrupt handler adds a character to the queue while ... and a little care to avoid buffer overflows) is safe on ... detect whether a character is available. ...
    (comp.arch.embedded)
  • Re: writing ISR for UART
    ... int putpoint; ... so if the interrupt handler adds a character to the queue while ... detect whether a character is available. ...
    (comp.arch.embedded)