M
mike79
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
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