Posted by Drew37 on 18:57:00 10-11-2001
How do I input into this template to do a level order tranverse? I want to input a text file. What file do I put the template code in? My client file, header, or implementation?
void traverse(link h, void visit(link))
{ QUEUE q(max);
q.put(h);
while (!q.empty())
{
visit(h = q.get());
if (h--->l != 0) q.put(h--->l);
if (h--->r != 0) q.put(h--->r);
}
}
Posted by SilentStrike on 22:30:00 10-11-2001
Most of your code will not need to be changed... the template will only change depending on what kind of data you want to hold.
template <class dataType>
struct link<dataType> {
link* l, r;
dataType data;
}
template <class T>void traverse(link<T> h, void visit(link<T>)) // perhaps visit should take a T (the type of a data) rather than a link<T> (the type of link)
{ QUEUE q(max);
q.put(h);
while (!q.empty())
{
visit(h = q.get());
if (h->l != 0) q.put(h->l);
if (h->r != 0) q.put(h->r);
}
}
[ This Message was edited by: SilentStrike on 2001-10-11 22:36 ]
Posted by Drew37 on 23:25:00 10-11-2001
So Datatype would be a int or char, double, etc.. How do I input ito the template? So I put the template in main?
Posted by SilentStrike on 03:34:00 10-13-2001
#include "whateverfilethosearedeclaredin.h"
int main() {
link<double> linkContainingDoubleData;
// get input from file here
traverse(linkContainingDoubleData, visitFunc); // it knows what traverse do call, could have specified it like this if you wished, however
traverse<double>(linkContaingDoubleData, visitFunc);
}
I am not so sure about the syntax for passing functions like that, I've only used function pointers to do such a task.
[ This Message was edited by: SilentStrike on 2001-10-13 03:34 ]