Is the front and rear values in the circular queue promised to be initialized to zero?

Asked 1 years ago, Updated 1 years ago, 73 views

Hi, how are you?

To me, hash code masters like professors and teachers

As always, I'll ask shyly.

Currently, I am studying with a book called "Data Structure Written Easily in C Language" by Chun In-guk.

It's written on page 154.

In circular queues, the concepts of front and rear are slightly changed. First, the initial value is zero, not minus one.

Here's what I've got as a point.

(If there is anything I misunderstood, please reply! )

(I will attach an example photo similar to the book! )

This is what I'm curious about.

By leaving 0 digits empty, the position difference between the front and rear

Let's say you can tell the difference between the blank and the saturation.

If you want to leave a single digit empty, then specify the front and rear as -1 in the first place

Isn't it possible to insert the data into the starting number of the index, 0?

But why do we initialize the front and rear to zero in the circular queue?

It's hard to find even if you search Korean data or google English dataYo

Help me!

queue data-structure c

2022-09-20 17:03

2 Answers

It's a question about a circular queue using an array.

One of the many ways to create a circular queue using an array is to simplify the code by consuming one space. Please remember that this is a method that is introduced a lot, but it is not the only method.

Anyway, the key to this method is to prevent an indistinguishable state by deliberately defining a blank state as a full state, since the basic idea cannot be used to distinguish between a full state and an empty state. The downside is that an array of spaces (i.e., memory) is wasted, and the advantage is that it wastes memory, but while the amount is insignificant, it can write code intuitively as originally conceived.

One of the array indexes must be empty because this is how the array is used. Therefore, if the array size is 6 as shown in the figure, the index is 0 to 5, so one of them should be empty.

So the front and rear can't be -1 as in the question. It has to be either 0 or 5. It doesn't have to be zero, but if one of them is the initial value, I would normally choose zero.


2022-09-20 17:03

@cheolsu

Wow, thank you so much for your long reply.

It was an answer that became bone, blood, and flesh.

Thank you always! Haha

😁


2022-09-20 17:03

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.