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.
// implementation file for the queue class
#include <iostream>
usingnamespace std;
constint 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;
elsefor (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;
}
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
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
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.
int queue::elements ()
{
int i; int x = 0;
if(empty())
return 0;
elsefor (i = front+1; i != (rear + 1) % queue_size; i = (i + 1)%queue_size)
x++;
return x;
}