J
Jack
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.
Thanks.
Jack
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.
Thanks.
Jack