473,394 Members | 1,709 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Fixed-size FIFO queue

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

Jun 9 '06 #1
8 14162
"Jack" <ju******@gmail.com> wrote:
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?


First of all "c-array" should be c_array.

Use memmove(c_array, c_array+1, N-1);
Jun 9 '06 #2
Jack <ju******@gmail.com> wrote:
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.

I guess you're looking to do that without shifting the characters each time.
You can implement the queue using circular arrays.

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)
Jun 9 '06 #3
"Jack" <ju******@gmail.com> wrote in message
news:11**********************@f6g2000cwb.googlegro ups.com...
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.
Jun 9 '06 #4
> I guess you're looking to do that without shifting the characters each time.

Yes.
You can implement the queue using circular arrays.


But circular array changes the FIFO queue to a LIFO queue, I think.

After the queue is full, i.e., there are 10 elements in the queue, the
11th element will be put at the first slot, c_array[0], other than
c_array[9].

Jun 9 '06 #5
"Jack" <ju******@gmail.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...
I guess you're looking to do that without shifting the characters each
time.
Yes.
You can implement the queue using circular arrays.


But circular array changes the FIFO queue to a LIFO queue, I think.


LIFO just means you pull out the last (newest) thing you put in instead of
the first (oldest) thing still in the queue.
If you keep track of where you put it, and you know the head and tail, then
pulling the right item is not difficult.
The issue with a ring buffer which uses an array is that you must handle the
queue full condition somehow. It might be as simple as to write over top
of an existing element or it may be very complex, depending on what the
queue is supposed to accomplish.
After the queue is full, i.e., there are 10 elements in the queue, the
11th element will be put at the first slot, c_array[0], other than
c_array[9].


You either have to write over top of something or pause the queue (in any
case) when the queue is full. If it is OK to stomp on items, then smash
'em. If not, then pause the queue until a slot becomes available.

What is the queue for?
Jun 9 '06 #6
Jack <ju******@gmail.com> wrote:
I guess you're looking to do that without shifting the characters each time.
Yes.
You can implement the queue using circular arrays.


But circular array changes the FIFO queue to a LIFO queue, I think.

No. LIFO is a stack and operations are done at one end. In a circular
queue (is ring buffer a synonim?) push and pop work at two different
ends that chase each other.
A circular queue uses modulo to find the position of the next element
so, instead of shifting everything you wrap around.

After the queue is full, i.e., there are 10 elements in the queue, the
11th element will be put at the first slot, c_array[0], other than
c_array[9].

If you are using a circular array you'll have to have a way of telling
when the queue is full (either by having an extra position in the array
that you do not use or by using a counter in a queue structure). What
you do when the queue is full depends entirely on the problem you are
trying to solve. You could just overwrite elements if you don't care
about it, or, you could wait for something to pull an element from the
queue to make space for another.

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)
Jun 9 '06 #7
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" <ju******@gmail.com> wrote in message
news:11**********************@f6g2000cwb.googlegro ups.com...
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.


Jun 10 '06 #8

"Jack" <ju******@gmail.com> wrote in message
news:11**********************@f6g2000cwb.googlegro ups.com...
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.

You need to firm up on your requirement.
It may be that you can use a "circular buffer", whereby you store a start
position.
It is hardly worth doing for a queue of only 10 characters, since the
memove() variety is easier to read and maintain. However if we were talking
of a million characters it would be much faster.
The problem is that there is a discontinuity because we are starting the
array from, say, postion five and then wrapping round. This may or may not
violate your requirements.

If the elements have to be contiguous and in order, you could consider
making the queue of an arbitrarily-sized capacity, say 100. Then you add
characters to the end of the queue, until you exhaust your memory buffer.
The call memove() to shift it up to the top. This cuts the number of calls
to memove() by a factor of nearly 100.

--
Buy my book 12 Common Atheist Arguments (refuted)
$1.25 download or $7.20 paper, available www.lulu.com/bgy1mm
Jun 10 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: Adrian Parker | last post by:
I'm using the code below in my project. When I print all of these fixed length string variables, one per line, they strings in questions do not properly pad with 0s. strQuantity prints as " ...
4
by: Roger Leigh | last post by:
Hello, I'm writing a fixed-precision floating point class, based on the ideas in the example fixed_pt class in the "Practical C++ Programming" book by Steve Oualline (O' Reilly). This uses a...
1
by: Jaz | last post by:
Trying to use a fixed layer for a couple of NAV buttons. I found this code, but the IE part is commented, and I don't understand the IF statement. It works great on Moz/Firebird and Opera BUT...
9
by: Paul Trautwein | last post by:
I'm trying to get an image to float in a window despite scrolling. I've gotten it to work on my Mac using IE 5.2, Netscape, and Safari, but it goes wonky when I test it on a PC. (testing with IE...
6
by: Mason A. Clark | last post by:
Masters: On two or three-column layouts, one column often has a list of links. Scrolling the page hides them. I'm aware there's supposed to be the ability to fix the column (frame-like). I...
4
by: Peter Fjelsten | last post by:
Guys at comp.infosystems.www.authoring.stylesheets, I have designed a page in (x)HTML transitional that I am happy with in (close to) standard compliant browsers (i.e. Firebird/Opera), but IE...
9
by: pout | last post by:
What are the purposes of fixed-point? When should it be used? I read: #define Int2Fixed(x) (((long)(short)x) << 16) and the fixed-point in 16.16 format. Does the 16 in the MACRO refer to...
4
by: Otie | last post by:
Hello, I am using the MSFlexGrd Control in VB5. I have 1 fixed row and one fixed column. I am trying to do a sort when the user clicks a column in the FIXED ROW. But when I capture the row...
4
by: Jeff | last post by:
Hey I'm wondering how the Fixed-Width Text Format is What I know is that the top line in this text format will contain column names. and each row beneath the top line represent for example a...
15
by: ingejg | last post by:
I am starting to study internet synchronization, and my head is still spinning since internet is not my forte, however my boss is breathing down my neck at the moment. Our company has only one...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.