Circular queue size, help me please !!

Dear all,
I am implementing a circular queue, but I can't find the formula to obtain the size of the queue; this one doesn't work correctly and gives me wrong results:

int size = abs (m_tail - m_front) ;

where m_front and m_tail are the indexes of the queue's front and tail respectively.

Please help me solve this issue :(
can you show the code?

EDIT:

This worked for me. very similar:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

using namespace std;

int main()
{
	int absolute;
	int first,last;
	
	cout<<"Enter a big number: ";
	cin>>first;
	cout<<"Enter another one(smaller): ";
	cin>>last;
	absolute=abs(first-last);
	cout<<"Absolute: "<<absolute;
	cin.ignore();
	cin.get();
}
Last edited on
Thanks for the reply. Here the class Circular_Queue as I have implemented:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107

#include "circular_queue.h" 
#include <iostream> 
#include <cstdlib> 

#define MaxSize 150000   // in bytes, which means 100 packets maximum 

using namespace std ; 

typedef unsigned char Byte ; 

 CircularQueue::CircularQueue() 
{

   m_front = 0 ; 

   m_tail = 0 ; 


 }

  CircularQueue::~CircularQueue() 
{

}

void CircularQueue::insert(Byte byte) 

{ 

    if (m_front == (m_tail + 1) % MaxSize)

     { 
      // cout << "Circular Queue is full" << endl ; 

     }

    else 

     {
        m_tail = (m_tail + 1) % MaxSize ; 

        m_array[m_tail] = byte ; 

     }

}

Byte CircularQueue::dequeue(void) 

{ 
    Byte byte  ; 

   if (m_front == m_tail) 

    {  
       //cout << "the circular queue is empty" << endl ; 

    }

   else 

     { 
         byte = m_array[m_front] ; 

         m_front = (m_front + 1) % MaxSize ; 

       }
  
          return byte ; 

}

int CircularQueue::Size(void) 

{
   int size = abs(m_front – m_tail) ; 

   return  size  ; 
}

bool CircularQueue::isEmpty(void) 

{
   if (m_front == m_tail) 

      return true ; 

   else

      return false ; 
}

bool CircularQueue::isFull(void) 

{
    if ((m_front == m_tail + 1) % MaxSize) 

      return true ; 

    else 
    
      return false ; 

}



I have a problem with the function Size() which gives me a wrong result. Is there a general formula to get the circular queue size? Thanks in advance
Thank you for the code snippet but it doesn't seem to be the same case since what you are implementing is not a circular queue. By definition, in a circular queue, the front and tail indexes are set to zero when they reach the max_size , which is expressed here by a modulo. May be I don't explain my problem correctly but your code does simply calculate the difference between two numbers and shows the result, doesn' it??
Is circular queue a topic, hahaha lol! I'm still learning not a pro at all. but I do my best to help!
int size = abs (m_tail - m_front) ; ¿why do you use abs()? ¿when that value could be negative?

By the way
1
2
3
4
5
/*   if (m_front == m_tail) 
      return true ; 
   else
      return false;*/
   return m_front == m_tail;
Last edited on
Topic archived. No new replies allowed.