Hi all,
I have attempted to implement a queue in C.
So far all is well, but I have a question to ask you experts in
regards to my implementation.
Say I have a queue of integers. My queue will be an array of integers
which could hold 100 elements for example.
I have two variables namely firstElement and lastElement. When I add
an integer to the queue, the lastElement variable will be incremented
and the integer stored at this index (lastElement) into the queue
array.
When I remove an element from the queue, the integer stored at
"firstElement" will be returned and the variable firstElement will
also be incremented.
If I keep doing this, soon the lastElement will reach the end of the
queue, and I will run out of space. Should I shift all the elements
back to the start of the queue array everytime I get a value from the
queue, or should this be how a queue is implemented? But if I shift
everytime i retrieve data from the queue, wouldn't this slow down the
program alot?
Thank you for all the help and support,
mike79 5 8374
mike79 wrote: Hi all,
I have attempted to implement a queue in C.
So far all is well, but I have a question to ask you experts in regards to my implementation.
Say I have a queue of integers. My queue will be an array of integers which could hold 100 elements for example.
I have two variables namely firstElement and lastElement. When I add an integer to the queue, the lastElement variable will be incremented and the integer stored at this index (lastElement) into the queue array.
When I remove an element from the queue, the integer stored at "firstElement" will be returned and the variable firstElement will also be incremented.
Yes, this is queue in action, first in last out.
If I keep doing this, soon the lastElement will reach the end of the queue, and I will run out of space. Should I shift all the elements back to the start of the queue array everytime I get a value from the queue, or should this be how a queue is implemented? But if I shift everytime i retrieve data from the queue, wouldn't this slow down the program alot?
Thank you for all the help and support, mike79
Perhaps you want a _circular_ queue or a.k.a. ring buffer.
In a circular queue, based on an array, when the pointers go past
the end of the array, they are moved to the beginning of the array.
One technique for implementing a circular queue is to have two
pointers, first and last; and a boolean variable to indicate full
or empty. When first == last, the queue is either full or empty
which the boolean variable is used for.
For more information about this and other data structures,
read news:comp.programming.
--
Thomas Matthews
C++ newsgroup welcome message: http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq: http://www.raos.demon.uk/acllc-c++/faq.html
Other sites: http://www.josuttis.com -- C++ STL Library book
"mike79" <mi****@iprimus.com.au> ha scritto nel messaggio
news:49*************************@posting.google.co m... Hi all,
I have attempted to implement a queue in C.
So far all is well, but I have a question to ask you experts in regards to my implementation.
Say I have a queue of integers. My queue will be an array of integers which could hold 100 elements for example.
I have two variables namely firstElement and lastElement. When I add an integer to the queue, the lastElement variable will be incremented and the integer stored at this index (lastElement) into the queue array.
When I remove an element from the queue, the integer stored at "firstElement" will be returned and the variable firstElement will also be incremented.
If I keep doing this, soon the lastElement will reach the end of the queue, and I will run out of space. Should I shift all the elements back to the start of the queue array everytime I get a value from the queue, or should this be how a queue is implemented? But if I shift everytime i retrieve data from the queue, wouldn't this slow down the program alot?
You can implement a circular buffer for the queue.
In practice, it means that you increment firstElement and
lastElement module (%) the lenght of the array.
You must also pay attention not to reach lastElement with firstElement,
that is "to dequeue elements that are not been enqueued".
Bye,
Ema
PS. Sorry for my English...
> If I keep doing this, soon the lastElement will reach the end of the queue, and I will run out of space. Should I shift all the elements back to the start of the queue array everytime I get a value from the queue, or should this be how a queue is implemented?
Why not just make the array circular. That is, when you get to the
index length-1 loop the index back around to zero. The only problem
with this is that if you keep adding elements then the firstElement and
lastElement indexes will cross... but such is life with static
structures.
-m
Thomas Matthews wrote: mike79 wrote:
Hi all,
I have attempted to implement a queue in C.
So far all is well, but I have a question to ask you experts in regards to my implementation.
Say I have a queue of integers. My queue will be an array of integers which could hold 100 elements for example.
I have two variables namely firstElement and lastElement. When I add an integer to the queue, the lastElement variable will be incremented and the integer stored at this index (lastElement) into the queue array.
When I remove an element from the queue, the integer stored at "firstElement" will be returned and the variable firstElement will also be incremented.
Yes, this is queue in action, first in last out.
*cough*
[...]
-Peter
Thomas Matthews wrote: Yes, this is queue in action, first in last out.
That would be a stack. Queues are FIFO.
Brian Rodenborn This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: AlanR |
last post by:
Hi,
I have to implement(or possibly acquire) a queue that manages
priorities. Low priority elements cannot get lost in the queue
indefinitely if there is a sustained, constant flow of high...
|
by: Anand Pillai |
last post by:
The standard Python Queue module, allows to generate queues that
have no size limit, by passing the size argument as <= 0.
q = Queue(0)
In a multithreaded application, these queues could be...
|
by: Aki Niimura |
last post by:
Hello everyone.
I'm trying to control a program from a Python program using IPC.
Although using a socket is a common choice for such applications, I
would like
to use SysV message queues...
|
by: badtemper |
last post by:
Is there any way to use perl or any other programming language to
perform a "qchk -A" from a web page and see the listing?
My problem is that I manage the network for a medical facility that is...
|
by: Aravind |
last post by:
Want to know how robust UNIX queues are? How many queues can be
created at a time in AIX platform ? What is the maxsize that we
allocate for a queue in AIX platform?
|
by: saleem |
last post by:
Dear friends,
I am working on the problem which deals with the data management
of requests and how should we match the responses for the requests to
compare the correct Response.
my problem...
|
by: Safalra |
last post by:
A long time ago when I used a browser that didn't support the shift or
unshift methods of the array object, I came up with an implementation of
queues that guaranteed amortised constant time...
|
by: brianbass |
last post by:
I am having a perplexing issue and am wondering if anyone can see the
flaw in my setup.
I have 3 windows services and 3 message queues. Each service resides
in the same directory and they all...
|
by: ericstein81 |
last post by:
I am trying to write a program that uses three queues for inputs of numbers. The problem i am having is I am not quite sure how to designate the seperate queues. can they be named as queue(a),...
|
by: DCC700 |
last post by:
Here are a couple of errors I have found in the event log
system.typeinitialization NIL
system.messaging.messagequeue NIL
These occur when trying to send a message through ms queues to a...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
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...
| |