0
$\begingroup$

I'm learning data structures from "Fundamentals of Data structures in C" by Sahni . In the topic, Circular Queue using Dynamic Array, the author has mentioned below point,

Let capacity be the initial capacity of the circular queue,We must first increase the size of the array using realloc,this will copy maximum of capacity elements on to the new array. To get a proper circular queue configuration, we must slide elements in the right segment(i.e, elements A and B) to the right end of the array(refer diagram 3.7.d). The array doubling and the slide to the right together copy at most 2 * capacity -2 elements.

I understand array doubling copies at most capacity elements. But how does array doubling and slide to right copy at most 2 * capacity -2 elements??enter image description here

$\endgroup$
0

2 Answers 2

1
$\begingroup$

I think the author is just saying the circular list has capacity of $n-1$ where $n$ is the total cells. When the array is doubled there is copying of these $n-1$ elements to new array in addition with shifting of these $n-1$ elements in the worst case, thus making it total $2n-2$ doubling and sliding in the worst case.

$\endgroup$
0
$\begingroup$

In above example queue capacity is 8. A circular queue can have capacity - 1 elements. Here it is 7. While array doubling those 7 elements are copied. Then suppose front = 0 and rear = 7, then the elements from 1 to 7 has to be moved to the right. Here it copies 7 elements. Hence the total is 7 + 7 = 14. 2 * 8 - 2 = 14

$\endgroup$
1
  • $\begingroup$ (A [data structure instance] can have capacity - 1 elements sounds a fun definition of capacity.) "The -1" is an artefact of an implementation just keeping two indices besides "the array". More element copies than there are valid elements is sub-standard (in the eyes of this not-so-humble coder). $\endgroup$
    – greybeard
    Commented Sep 1, 2020 at 4:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.