Re: Fixed-size FIFO queue
- From: "deepak" <deepakpjose@xxxxxxxxx>
- Date: 10 Jun 2006 02:23:18 -0700
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.
.
- References:
- Fixed-size FIFO queue
- From: Jack
- Re: Fixed-size FIFO queue
- From: Dann Corbit
- Fixed-size FIFO queue
- Prev by Date: Re: casting
- Next by Date: Re: Example for an dynamically allocated associative array in C
- Previous by thread: Re: Fixed-size FIFO queue
- Next by thread: Re: Fixed-size FIFO queue
- Index(es):
Relevant Pages
|