how to find number of elements in a Circular Queue

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-01 03:26:13

问题


how do i find the number of items in a circular queue?

|front - rear| doesnt always work.

is there one equation to know how many element is in a circular queue?


回答1:


actually the size would be,

size = front > rear ? (MAX - front + rear + 1) : (rear - front + 1);

or one can go for a generic formula:

size = abs(abs(MAX - front) - abs(MAX -rear));//this works in every situation



回答2:


 Pointer1 = head; // (your node)
 count = 0;


 if( Pointer1 != NULL )
 {
   count = 1;
   Pointer2 = Pointer1->Next;
   while ( Pointer2 != NULL && Pointer2 != Pointer1 )
   {
     count++;
     Pointer2 = Pointer2->Next;
   }
 }

 return count;



回答3:


Assuming you are using array of size N for queue implementation, then size of queue would be

size= (N-front+rear) mod N




回答4:


Assuming you implement it using an array with size N so there are pointers pointing to the front and rear. Use the following formula:

size = front > rear ? (front - rear) : (front+N -  rear);



回答5:


The standard answer is to take two iterators at the beginning, increment the first one once, and the second one twice. Check to see if they point to the same object. Then repeat until the one that is incrementing twice either hits the first one or reaches the end. inside this loop use the counter to get the length of the CQuueeue




回答6:


No of items in Circular queue is,

size = (N-f+r) mod N

where

  • N is the size of array used in circular fashion
  • f index of the front element
  • r index immediately past the rear element

This formula work for both liner and circular queues.




回答7:


None of the formulas take into account the empty (zero) case. This will give you the number of free bytes available in the queue:

FreeSpace = (printRdQue == printWrQue) ? PRINT_QUEUE_SIZE :
           (PRINT_QUEUE_SIZE - printWrQue + printRdQue) % PRINT_QUEUE_SIZE;



回答8:


can your queue contain the same element in more than one location? if it can then I don't think you can do this as there is no way to know the difference between:

a->b->c

and

a->b->c->a->b->c

if it can't contain the same element more than once, just look through the queue until you find an element you have already seen




回答9:


int lenghtQueue(queue* q){

     int len =0 ;
     if (isEmpty(q))
            return 0 ;

      queue* qe = q->next ;
      len ++ ;

       while(qe!=q){
             len ++ ;
             qe=qe->next ;           
       }
           return len ;
  }



回答10:


if (Cqueue_front>Cqueue_rear) cout<<" The number of queue items are : "<


回答11:


What do you need to implement a circular queue ?

Answer: you will need front and rear nodes + list of items + count_items.

Of course it is only implemented like this when the queue is finite, when talking about

dynamic allocation it will be different.

Take a look at an example in C Language,

typedef struct
{

TYPE items[MAXSIZE];

TYPE front;

TYPE rear;

int count_items;

} QUEUE;

This will assure you the exact amount of items are currently exist in the queue.

When you want to insert an item to the queue, you will simply increment rear and count_items, and when you want to remove an item from the queue, you will simply decrement rear and count_items.



来源:https://stackoverflow.com/questions/4459141/how-to-find-number-of-elements-in-a-circular-queue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!