How to write a loop to count a queue

I need to write a loop which will count the elements in a queue which also needs to work with a wrap around.. This is what I have but it doesn't work.
1
2
int i;
  for (i = first_index; i != (last_index + 1) % queue_size; i = (i + 1) % queue_size)

I'm pretty sure that this is what's wrong with my program but if not then let me know.. Because it doesn't work it either prints no values or it prints 0
"needs to work with a wrap around" is incredibly vague. If you just want to loop through all elements, you can ignore this completely. If you want to start from the center and end just before it after wrapping around, your current code doesn't even attempt to start at the correct place.

(last_index + 1) % queue_size

Do you understand why the above expression will always be 0?
No I don't even understand the loop at all but this is how our professor told us to write it except the last part where it increments i that's the part that I wrote
I can only make assumptions about the rest of your code from the small amount you have posted. I can't help without seeing the rest of the queue class.
Okay I'll post the whole implementation file
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
// implementation file for the queue class

#include <iostream>
using namespace std;

const int queue_size = 6;

class queue
{
      private:
         char data [queue_size]; // elements in the queue
         int front, // index to the front of the queue, indexes one previous
                    // to actual front element (remove)
             rear;  // index of the rear element;
      public:
         queue ();  // create an empty queue
         void enqueue (char item);   // adds an element to the rear
         char dequeue ();            // removes an element from the front
         bool empty ();              // returns true for an empty queue
         bool full ();               // returns true for a full queue
         int elements ();            // returns the number of elements in the queue
         void print ();              // prints the queue from front to rear
};

// returns the number of elements in the queue. If queue is empty, it returns 0.
int queue::elements ()
{
	int i;
	if(empty())
	return 0;
	else
	for (i = front; i != (rear + 1) % queue_size; i = (i + 1)%queue_size)
	return i;
}

// prints the elements in the queue from first to last element. If the queue is empty, nothing
// is printed
void queue::print ()
{
	int i;
	if(!empty())
	for (i = front; i != (rear + 1) % queue_size; i = (i + 1)%queue_size)
	cout << data[i] << " ";
}

// constructor creates an empty queue
queue::queue ()
{
    front = 0;
    rear = 0;
}

// enqueue adds an element to the rear of the queue
void queue::enqueue (char item)
{
      // if full, can't add another element
      if (full ())
      {
          cout << "\n\nQueue Error: Can't add to a full queue";
          cout << "\nQueue will not be changed";
      }
      else // ok to add an item
      {
           rear = (rear + 1) % queue_size;
           data [rear] = item;
      }
}

// dequeue removes an element from the front of the queue
char queue::dequeue ()
{
     // if the queue is empty, we can't remove a value
     if (empty ())
     {
          cout << "\n\nQueue Error: Can't remove from an empty queue";
          cout << "\nReturning a blank";
          return ' ';
     }
     else // ok to remove a value
     {
          front = (front + 1) % queue_size;
          return data [front];
     }
}

// empty returns true if the queue is empty
bool queue::empty ()
{
     return front == rear;
}

// full returns true if the queue is full
bool queue::full ()
{
     return (rear + 1) % queue_size == front;
}






         
On line 12, your own comment says that front is one less than the actual first element. Hint: your loop should start with for(int i = front + 1;

Note that you don't actually have any variables named first_index or last_index - this is fine, those names were for example purposes only.
Last edited on
Okay now it works but I'm not sure if the results are correct.. After I enqueue 5 characters it says that the number of elements is one, then I dequeued 3 elements and enqueued 3 new ones and then it says that the number of elements is 4
Line 32 needs to be changed as well.
Yeah I changed both of them to front+1..I can post the application file too but I'm pretty sure that there's something wrong with the elements function
Copy and paste just the line of the for-loop that you currently have.
for (i = front+1; i != (rear + 1) % queue_size; i = (i + 1)%queue_size)
It is working fine for me:
http://ideone.com/1rMLTr
Edit: I just realized that you never called q.elements(); in your program to show how many elements you have in the queue
Last edited on
In your elements function, you are returning the last value that i takes, which has no relation to the number of elements. You need a separate variable that you increment in the loop.
Edit: Is this correct?
1
2
3
4
5
6
7
8
9
10
int queue::elements ()
{
	int i; int x = 0;
	if(empty())
	return 0;
	else
	for (i = front+1; i != (rear + 1) % queue_size; i = (i + 1)%queue_size)
	x++;
	return x;
}
Last edited on
It looks like it should be. Does it generate the correct output for you?
Topic archived. No new replies allowed.