Sign in to follow this  
kingpinzs

Ring Queue view issue

Recommended Posts

I created a queue class and everything works except cout the queue starting from the logical front to the logical end. I can cout 0 to 15 of the array of the queue but I cant figure out how to start from the logical begining to the logical end.

[code]
void Queue::View()
{
int i = 0;

if(!Empty())//make sure queue had elements
{
while(i < curSize)//go through all elements tell all have been read
{
if(itr >=maxsize)
{
itr = 0;
i++;
}
else
{
itr++;
i++;
}
cout<<queueArray[itr];
}

}
cout<<endl;
itr = Front;
}
[/code]

This is what I have so far.
It works tell I try to add an elment to the queue then it crash's

Share this post


Link to post
Share on other sites
[quote name='Hodgman' timestamp='1297924854' post='4775282']
for( i = logicalBegin; i != logicalEnd; i=(i==physicalEnd)?physicalBegin:i+1 )
[/quote]

that did not work it just crashed becuase I have to manuely set the veribles to go from 0 to 14 and back to 0

this almost works only one small bug now

[code]
void Queue::View()
{
int i = 0;

if(!Empty())//make sure queue had elements
{
while(i <=curSize && i < 14)//go through all elements tell all have been read
{
if(itr < 14)
{
itr++;
i++;
}
else
{
itr = 0;
i++;
}
cout<<queueArray[itr];
}
}
cout<<endl;
itr = Front;
}
[/code]

I can remove all items from the queue and it displays correctly with no issues but when I add an elment to the rear of the queue it hides the front element I can still delete the front one but it wont display it
Once I remove it the second element is the first one and it works fine tell I add another element and it does the same thing.

So the starting position is wronf once I add an element to the queue.

Any ideas

Share this post


Link to post
Share on other sites
I finaly got it to work
I dont know why it worls but it does

I changed
itr = Front;
to
itr = Front -1;
even though that seams backwards and should be
itr = Front + 1;

Hope some one can inliten me

Share this post


Link to post
Share on other sites
You are making it very difficult for yourself. The value of "iter" should be set at the start of the function. Your problem here is that the iter variable is incremented before the first access, which is why you had to subtract one.

But even still, you should not be using a member variable "iter". This function should be stateless, doing it in the way you are will cause trouble. Second, don't have an explicit "empty" check. The loop test should handle that by itself. You've also duplicated code by writing "i++" in both sides of the conditional statement. Your loop would be more clearly written as a for() loop. You should also be careful with comparisons. When looping over N elements, if you start at index = 0 you should use index < N, not index <= N.

Something like this:
[code]

void Queue::View()
{
for(int i = 0 ; i < queueSize ; ++i)
{
int physicalIndex = (front + i) % queueSize;
cout << queueArray[physicalIndex];
}
cout<<endl;
}
[/code]
That said, this function does not belong in the queue class. Such a class should not be responsible for I/O. A better design is to make the interface so that this function can be written as a free function, something like this:
[code]
void print(Queue &queue, std::ostream &out = std::cout)
{
for(Queue::iterator it = queue.begin() ; it != queue.end() ; ++it)
{
out << *it;
}
}
[/code]
The nested "iterator" type will hide the detail of translating logical and physical indices for client code. You see that in this code the client has total control of the I/O, it can apply additional formatting and supports writing the queue to an arbitrary stream, including files.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this