Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need Help With My Code -- Linked Queues

Status
Not open for further replies.

DaniDak

Technical User
Mar 13, 2002
44
US
Hi, below I have some code segments from my program in which I need to implement a generic template class(myqueue). The elements of the queue
must be instantiated to any data type including strings. I got all that down the only problem I have is with the strings. To be able to instantiate a string data type like I did with int and double(see below) I need to write a string class but if I do that how am I supposed to instantiate a string data type from my myqueue class in order to use myqueue and node class. I really need some help.

Thank you

//genqueue.h file////

#include <iostream.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>

template<class DataT>
class myqueue;

///////////////////////
//template class node//
///////////////////////
template<class DataT>
class node
{
private:
DataT data;
node<DataT> * next;

public:
node(DataT d, node<DataT>* n=NULL):data(d), next(n){}

friend class myqueue<DataT>;
};


//////////////////////////
//template class myqueue//
//////////////////////////
template<class DataT>
class myqueue
{
private:
node<DataT> * head;
node<DataT> * tail;

public:
myqueue():head(NULL), tail(NULL){} //constructor
bool empty(){return !head}
void add(DataT v);
DataT remove();
void display(); //print queue contents
};
//member functions

//main
void main()
{
int nint;

myqueue <int> qint; //for int gueue
myqueue <double> qdouble; //double gueue
// can I do this if I write a string class to get a list of queues
myqueue <string> qstring; //for string queue

**
** code here

//to add to int queue
cout<<&quot; \t Add to int queue: &quot;; cin>>nint; qint.add(nint);

//I should be able to add to string queue like I did to int, right-

thank you again.....

 
Well... a first idea would be to pass some additional clues to the template class constructor, for instance, specify the type of the data to be used, but this is not such a good idea, dogmatically speaking.

Other method would be to allocate all the strings outside the class and have something like a purge method outside the class, in your code, to delete the strings but I don't know if that would satisfy your requirements.

A third idea would be to pass the bit size of the object to be used by the template and use memcpy to copy verbatim the bits into your node class. This too has disadvantages as bit-copy is not the best idea for complex objects.


I am not sure if a copy-construction would work on templates - that is, use the copy constructor for <DataT> instead of bit-copy; for that you have to supply a CC.

HTH... [red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top