how to find number of elements in a Circular Queue

后端 未结 13 1569
我寻月下人不归
我寻月下人不归 2021-01-13 03:10

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 cir

13条回答
  •  日久生厌
    2021-01-13 03:43

    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.

提交回复
热议问题