When implementing a queue with an array, what is more usual? To use array[0] as front or as rear of the queue? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

When implementing a queue with an array, what is more usual? To use array[0] as front or as rear of the queue?

1st Jun 2022, 11:29 AM
David Ortega
David Ortega - avatar
1 Answer
+ 1
Well, for Queues, since it is a FIFO data structure, "First in, First Out", the rear would not be considered the front unless it's the only element. But I also don't want to say array[0] because array[0] may be discarded/removed. It would be better to have a seperate variable that let's you know which element is "first" because it's not always going to be 0th. For example, if I already removed 3 elements, the first of an array is array[3], but if you hard-code array[0], it will access a garbage value. Here's a video that explains it really well. https://youtu.be/3TmUv1uS92s
1st Jun 2022, 11:42 AM
Justice
Justice - avatar