template %26lt;typename Type%26gt;
struct Node{
Type data;
Node* next;
Node* previous;
}
template%26lt;typename Type%26gt;
class Queue{
void push(const Type%26amp;);
private:
Node%26lt;Type%26gt;* queueFront;
Node%26lt;Type%26gt;* queueBack;
};
// add an element to the back of the queue
template %26lt;typename Type%26gt;
void Queue%26lt;Type%26gt;::push(const Type%26amp; aValue){
// create a new node on the heap
Node%26lt;Type%26gt;* p = new Node%26lt;Type%26gt;;
// store the value into the node
queueBack = p;
p-%26gt;data = aValue;
if(queueFront == 0){
queueFront = p;
// if it is the first elment in the stack
// there is no previous so it is null
queueFront-%26gt;previous = 0;
queueFront-%26gt;next = 0;
}else{
p-%26gt;next = queueBack;
queueBack-%26gt;previous = p;
p-%26gt;previous = 0;
};
// make stack top point to the new node
// incriment the size
++queueSize;
};
when ever i try to call front, and return the value of the front item in the queue,i get an error if i have more than one element. What is happening??? thanks.
C++ queue help please.?
please try another compiler and
mention what error you r facing
Reply:Not sure if this is the problem, but I noticed that the two lines:
queueBack-%26gt;previous = p;
p-%26gt;previous = 0;
are counterproductive. At this point, queueBack == p, so the first line is useless.
I suspect you don't want that original queueBack = p and instead of an else off the if (queueFront == 0) test, you want an independent test of queueBack to decide how to set it.
Reply:There are too many problems, may be you can contact a C++ expert. Check websites like http://oktutorial.com/
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment