473,805 Members | 2,017 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 14228
"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.goog legroups.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.goo glegroups.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.goog legroups.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.goog legroups.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
9704
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". Six spaces than the value of intQuantity. This is correct. But all the others end up being string objects of only 6 characters long (with the exception of strTotal). The left most positions of the string object are being padded with one...
4
7854
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 long int to store the value, and the precision (number of decimal points) is variable (it's a templated class): template <size_t _decimal_places = 4> class FixedFloat {
1
3530
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 not IE. Would someone please take a look at this and tell me if it can made to work in IE? If it can, a hint on the syntax/code would be much appreciated! PS, this would also get rid of my fixed background. Thanks in advance Jaz
9
17720
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 only at the moment.) Positioning is wrong, and it doesn't float at all. Here's a test page: http://www.bdiusa.com/mirrors/test.html I've tested the code using the CSS Validator on the W3 site - and it said it's okay.
6
5418
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 have some bits of such code but haven't yet made it work well. Question: Why have I never seen an example on the web? Not that I've seen everything, but I've seen numerous pages
4
5880
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 is causing problems as can be expected since I have used position:fixed. I don't want to use too many hacks and I don't care that the page looks a bit different in IE (their loss) but I want it to be usable.
9
4198
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 integer or decimal part? For example, if in 8.24, should the macro be: #define Int2Fixed(x) (((long)(short)x) << 24)?
4
11178
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 number in the click event I get row = 1 if I click on the FIXED row OR the actual row 1. How can I get the grid control to tell me when the user has clicked on the FIXED row and not on row 1 (since they both produce row = 1 and I cannot tell them apart...
4
4006
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 row in a table etc... But does fixed-with mean that every column has a fixed with: for example first column is 10 char wide and second column is 30 char wide?
15
2745
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 server with fixed IP address provided by our ISP, while the other sites (which I wish in the future hold the replicas databases) have only standard internet connections with Dynamic IP (which means that they change IP addresses, as given by the ISP...
0
9716
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9596
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10356
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10361
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9179
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7644
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6874
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5676
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.