473,412 Members | 2,277 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

Urgent help: print elements in queue

Thanks to those guys who helped me out yesterday. I have one more
problem; my print function for the queue program doesnt work and goes
into an endless loop. Also I am unable to calculate the length of my
queue. I started getting compilation errors when I included a length
function.
<code>

template<class ItemType>
void Queue<ItemType>::MakeEmpty()
{
rear = maxQue - 1;
front = maxQue - 1;
}

template<class ItemType>
bool Queue<ItemType>::IsEmpty()
{
return (rear==front);
}

template<class ItemType>
bool Queue<ItemType>::IsFull()
{
return ((rear+1) % maxQue == front);
}

template<class ItemType>
int Queue<ItemType>::length()
{
return 0; // I dont know what to put here
}

template<class ItemType>
void Queue<ItemType>::Print()
{
int current;
current = rear;
do{
current = (current + 1) % maxQue;
std::cout << items[current] << std::endl;
}while(current != front);
}

// Main function items

IntQueue.Enqueue(1);
IntQueue.Enqueue(2);
IntQueue.Enqueue(3);
IntQueue.Enqueue(4);
cout << "int length 3 = " << IntQueue.length() << endl;
IntQueue.Dequeue(x);
cout << "int length 4 = " << IntQueue.length() << endl;
IntQueue.Print();

</code>

my length function has no code and I dont know what to put in there.

Apr 16 '07 #1
4 4577
On Apr 16, 3:39 pm, j_depp...@yahoo.com wrote:
Thanks to those guys who helped me out yesterday. I have one more
problem; my print function for the queue program doesnt work and goes
into an endless loop. Also I am unable to calculate the length of my
queue. I started getting compilation errors when I included a length
function.
<code>

template<class ItemType>
void Queue<ItemType>::MakeEmpty()
{
rear = maxQue - 1;
front = maxQue - 1;

}

template<class ItemType>
bool Queue<ItemType>::IsEmpty()
{
return (rear==front);

}

template<class ItemType>
bool Queue<ItemType>::IsFull()
{
return ((rear+1) % maxQue == front);

}

template<class ItemType>
int Queue<ItemType>::length()
{
return 0; // I dont know what to put here

}

template<class ItemType>
void Queue<ItemType>::Print()
{
int current;
current = rear;
do{
current = (current + 1) % maxQue;
std::cout << items[current] << std::endl;
}while(current != front);

}

// Main function items

IntQueue.Enqueue(1);
IntQueue.Enqueue(2);
IntQueue.Enqueue(3);
IntQueue.Enqueue(4);
cout << "int length 3 = " << IntQueue.length() << endl;
IntQueue.Dequeue(x);
cout << "int length 4 = " << IntQueue.length() << endl;
IntQueue.Print();

</code>

my length function has no code and I dont know what to put in there.
You haven't posted sufficient code here to allow us to give a sensible
answer. Help us help you. Read and obey this FAQ:

http://www.parashift.com/c++-faq-lit...t.html#faq-5.8

Cheers! --M

Apr 16 '07 #2
Sorry about that.

<code>
#include <cstddef>
#include <new>

class FullQueue{};
class EmptyQueue{};


template<class ItemType>
class Queue
{
private:
int front; // front position
int rear; // rear position
int maxQue; // maximun number of
elements in the queue,
// including one
reserved element

ItemType* items; // it points to a
dynamically-allocated array
public:
Queue(int max); // parameterized class
constructor
Queue(); // default constructor;
queue created empty

Queue(const Queue<ItemType&x); // copy
constructor;implicitly called
// for pass by value
void MakeEmpty(); // queue is made empty
bool IsEmpty(); // test if the queue is
empty
bool IsFull(); // test if the queue is
full
int length(); // return the number of
elements in the queue
void Print(); // print the value of
all elements in the
// queue from the front
to rear
void Enqueue(ItemType x); // insert x to the rear
of the queue
// Pre: the queue is
not empty
void Dequeue(ItemType &x); // delete the element
from the front of the queue
// Pre: the queue is
not empty
~Queue(); // destructor: memory
for dynamic array needs to be deallocated

};

template<class ItemType>
Queue<ItemType>::Queue()
{

items = new ItemType[maxQue];
front = maxQue;
rear = maxQue;

}

template<class ItemType>
Queue<ItemType>::Queue(int max)
{
maxQue = max + 1;
front = maxQue - 1;
rear = maxQue -1;
items = new ItemType[maxQue];

}

template<class ItemType>
Queue<ItemType>::Queue(const Queue<ItemType&x)
{
front = x.front;
rear = x.rear;
int count = x.count;
for(int i=0;i<x.count;++i)
{
items[i] = x.items[i];
}
}

template<class ItemType>
void Queue<ItemType>::MakeEmpty()
{

rear = maxQue - 1;
front = maxQue - 1;
}

template<class ItemType>
bool Queue<ItemType>::IsEmpty()
{

return (rear==front);
}

template<class ItemType>
bool Queue<ItemType>::IsFull()
{
return ((rear+1) % maxQue == front);
}

template<class ItemType>
int Queue<ItemType>::length()
{

return 0;
}

template<class ItemType>
void Queue<ItemType>::Print()
{
int current;
current = rear;
do{
current = (current + 1) % maxQue;
std::cout << items[current] << std::endl;
}while(current != front);
}

template<class ItemType>
void Queue<ItemType>::Enqueue(ItemType x)
{
if(IsFull())
throw FullQueue();
else
{
rear = (rear+1)% maxQue;
items[rear]=x;

}
}

template<class ItemType>
void Queue<ItemType>::Dequeue(ItemType &x)
{
if(IsEmpty())
throw EmptyQueue();
else
{
front=(front +1)% maxQue;
x =items[front];

}
}

template<class ItemType>
Queue<ItemType>::~Queue()
{
delete[]items;
}

// main file
#include <cstdlib>
#include <cstddef>
#include <new>
#include <iostream>
#include "Queue.h"
using namespace std;

int main()
{

Queue<intIntQueue;

int x;
try{

IntQueue.Dequeue(x);
}
catch(FullQueue exceptionObject)
{
cerr << "FullQueue exception thrown.(int) " << endl;
}
catch(EmptyQueue exceptionObject)
{
cerr << "EmptyQueue exception thrown.(int) " << endl;
}
IntQueue.Enqueue(1);
IntQueue.Enqueue(2);
IntQueue.Enqueue(3);
IntQueue.Enqueue(4);
cout << "int length 3 = " << "4" << endl;//IntQueue.length() <<
endl;
IntQueue.Dequeue(x);
cout << "int length 4 = " << "4" << endl;;//IntQueue.length() <<
endl;
IntQueue.Print();


Queue<floatFloatQueue;
float y;
try{

FloatQueue.Dequeue(y);
}
catch(FullQueue exceptionObject)
{
cerr << "FullQueue exception thrown.(float)" << endl;
}
catch(EmptyQueue exceptionObject)
{
cerr << "EmptyQueue exception thrown.(float)" << endl;

}
cout << endl;
FloatQueue.Enqueue(7.1);
cout << "float length 3 = " << FloatQueue.length() << endl;
FloatQueue.Enqueue(2.3);
cout << "float length 4 = " << FloatQueue.length() << endl;
FloatQueue.Enqueue(3.1);
FloatQueue.Dequeue(y);
FloatQueue.Print();


system("pause");
return 0;
</code>
Apr 16 '07 #3

<j_*******@yahoo.comschrieb im Newsbeitrag
news:11*********************@d57g2000hsg.googlegro ups.com...
Sorry about that.

<code>
#include <cstddef>
#include <new>

class FullQueue{};
class EmptyQueue{};


template<class ItemType>
class Queue
{
private:
int front; // front position
int rear; // rear position
int maxQue; // maximun number of
elements in the queue,
// including one
reserved element

ItemType* items; // it points to a
dynamically-allocated array
public:
Queue(int max); // parameterized class
constructor
Queue(); // default constructor;
queue created empty

Queue(const Queue<ItemType&x); // copy
constructor;implicitly called
// for pass by value
void MakeEmpty(); // queue is made empty
bool IsEmpty(); // test if the queue is
empty
bool IsFull(); // test if the queue is
full
int length(); // return the number of
elements in the queue
void Print(); // print the value of
all elements in the
// queue from the front
to rear
void Enqueue(ItemType x); // insert x to the rear
of the queue
// Pre: the queue is
not empty
void Dequeue(ItemType &x); // delete the element
from the front of the queue
// Pre: the queue is
not empty
~Queue(); // destructor: memory
for dynamic array needs to be deallocated

};

template<class ItemType>
Queue<ItemType>::Queue()
{

items = new ItemType[maxQue];
front = maxQue;
rear = maxQue;

}

template<class ItemType>
Queue<ItemType>::Queue(int max)
{
maxQue = max + 1;
front = maxQue - 1;
rear = maxQue -1;
items = new ItemType[maxQue];

}

template<class ItemType>
Queue<ItemType>::Queue(const Queue<ItemType&x)
{
front = x.front;
rear = x.rear;
int count = x.count;
'count' is not a member of Queue<>. You probably meant maxQue instead.
for(int i=0;i<x.count;++i)
{
items[i] = x.items[i];
this->items is not valid. You hav to allocate some memory for your array of
pointers before you can assign pointers to it.
}
}
Remember the "Rule of Three". If you need a user-defined copy c-tor, you
probably need a user-defined assignment operator, too.
>
template<class ItemType>
void Queue<ItemType>::MakeEmpty()
{

rear = maxQue - 1;
front = maxQue - 1;
}

template<class ItemType>
bool Queue<ItemType>::IsEmpty()
{

return (rear==front);
}

template<class ItemType>
bool Queue<ItemType>::IsFull()
{
return ((rear+1) % maxQue == front);
}

template<class ItemType>
int Queue<ItemType>::length()
{

return 0;
If is rear greater than or equal to front, the length (number of elements in
queue) is rear - front. If it is less, the length is maxQue - front + rear
(or rear - front + maxQue). Also, if rear front than rear - front ==
(rear - front + maxQue) % maxQue. So you the length of the queue is (rear -
front + maxQue) % maxQue.
}

template<class ItemType>
void Queue<ItemType>::Print()
{
int current;
current = rear;
do{
current = (current + 1) % maxQue;
std::cout << items[current] << std::endl;
}while(current != front);
This code doesn't work it the queue is empty. Use a for or pre-emptive while
loop instead. You should also display those data between front and rear, not
those undefined items between rear and front. What about

for (int current = front; current != rear; current = (current + 1) %
maxQue)
{
std::cout << ...;
}

But you have to apply the following changes, too, to use for this way...
}

template<class ItemType>
void Queue<ItemType>::Enqueue(ItemType x)
{
if(IsFull())
throw FullQueue();
else
{
rear = (rear+1)% maxQue;
items[rear]=x;
In C++ (and C) it is more common to put something at the front position of a
container and make the rear position off by one, like it is with arrays,
where

int array[n];
array[0] = ...; /* legal */
array[n] = ...; /* error */

So using

items[rear] = x;
rear = (rear + 1) % maxQue;

is easier to understand for those accustomed to C++ or C.
}
}

template<class ItemType>
void Queue<ItemType>::Dequeue(ItemType &x)
{
if(IsEmpty())
throw EmptyQueue();
else
{
front=(front +1)% maxQue;
x =items[front];
Again, swap assignment and increment:

x = items[front];
front = (front + 1) % maxQue;
}
}

template<class ItemType>
Queue<ItemType>::~Queue()
{
delete[]items;
}

// main file
#include <cstdlib>
#include <cstddef>
#include <new>
#include <iostream>
#include "Queue.h"
using namespace std;

int main()
{

Queue<intIntQueue;

int x;
try{

IntQueue.Dequeue(x);
}
catch(FullQueue exceptionObject)
{
cerr << "FullQueue exception thrown.(int) " << endl;
}
catch(EmptyQueue exceptionObject)
{
cerr << "EmptyQueue exception thrown.(int) " << endl;
}
IntQueue.Enqueue(1);
IntQueue.Enqueue(2);
IntQueue.Enqueue(3);
IntQueue.Enqueue(4);
cout << "int length 3 = " << "4" << endl;//IntQueue.length() <<
endl;
IntQueue.Dequeue(x);
cout << "int length 4 = " << "4" << endl;;//IntQueue.length() <<
endl;
IntQueue.Print();


Queue<floatFloatQueue;
float y;
try{

FloatQueue.Dequeue(y);
}
catch(FullQueue exceptionObject)
{
cerr << "FullQueue exception thrown.(float)" << endl;
}
catch(EmptyQueue exceptionObject)
{
cerr << "EmptyQueue exception thrown.(float)" << endl;

}
cout << endl;
FloatQueue.Enqueue(7.1);
cout << "float length 3 = " << FloatQueue.length() << endl;
FloatQueue.Enqueue(2.3);
cout << "float length 4 = " << FloatQueue.length() << endl;
FloatQueue.Enqueue(3.1);
FloatQueue.Dequeue(y);
FloatQueue.Print();


system("pause");
return 0;
}
</code>

HTH
Heinz
Apr 16 '07 #4

<j_*******@yahoo.comwrote in message
news:11*********************@d57g2000hsg.googlegro ups.com...
Sorry about that.
Sorry about what? Please include what you're responding to.

template<class ItemType>
void Queue<ItemType>::Print()
{
int current;
current = rear;
do{
current = (current + 1) % maxQue;
std::cout << items[current] << std::endl;
}while(current != front);
}
That is the print function you're having problems with, right? So, what
happens? (Use your debugger and see!) If it's going into an infinite loop,
then that means that current never equals front, right? So, what's front
equal to?

Also, earlier you stated:
"Also I am unable to calculate the length of my queue. I started getting
compilation errors when I included a length function."
What does that mean? We can't tell what's wrong with code you don't show
us.

-Howard

Apr 16 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Bart Nessux | last post by:
Howdy, Below is a script that I'm using to try and count the number of HTTP servers within a company's private network. There are 65,536 possible hosts that may have HTTP servers on them. Any...
4
by: Sachin Jagtap | last post by:
Hi, I am getting exception while poping item from queue, I am writing messages coming from client in a queue in one thread and then in other thread i am reading from queue and writing in file....
3
by: Rob | last post by:
I have a form - when you click the submit button, it appends a variable to the URL (e.g. xyz.cgi?inputID=some_dynamic_variable) It also opens a new page. Now, that some_dynamic_variable is...
5
by: Sinan Nalkaya | last post by:
hello, i need a function like that, wait 5 seconds: (during wait) do the function but function waits for keyboard input so if you dont enter any it waits forever. i tried time.sleep() but when...
0
by: royal | last post by:
This problem is to simulate an airport landing and takeoff pattern. The airport has 3 runways, runway 1, runway 2 and runway 3. There are 4 landing holding patterns, two for each of the first two...
10
by: satan | last post by:
I need the definitions of the method copyQueue, the default constructor, and the copy constructor folr the class LinkedQueueClass. This is what i get so far public abstract class DataElement {...
4
by: jjh5030 | last post by:
This is a programming assignment. You are asked to work with pointers. Be aware that error messages are often not very helpful when your pointers point to bad locations. Therefore, reserve...
24
by: Joe, G.I. | last post by:
Can anyone help me w/ a priority_queue. I'm generating MyEvent classes and I put them on a priority_queue, but I don't know how to get them in priority. The priority is the event w/ the smallest...
2
by: hcaptech | last post by:
This is my Test.can you help me ? 1.Which of the following statement about C# varialble is incorrect ? A.A variable is a computer memory location identified by a unique name B.A variable's name...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.