Thanks alot for your repley.
I finaly got it work. Of course the user must tell me if he wants to remove a string or a number.
But I think you are right. This is a very strange solution, because I first tried to compare numbers with strings and got no warnings.
About the templates, I started with a simple example, which don't look that simple to me.
Well if anyone interstated this is the solution I found.
Code:
#include <cstdlib>
#include <iostream>
using namespace std;
class list;
// N_TYPE is numbers.
// S_TYPE is strings.
enum data_type { N_TYPE, S_TYPE };
class block
{
block *pnext;
protected :
int type;
public:
block(void)
{
pnext = NULL;
}
virtual void show(void)=0;
virtual int get_type(void)=0;
friend class list;
};
class numbers: public block
{
int num;
public:
numbers(int Inum)
{
num = Inum;
type = N_TYPE;
}
void show(void)
{
cout << "num = " << num << "\n";
}
int get_type(void)
{
return(N_TYPE);
}
int get_value(void)
{
return(num);
}
};
class strings : public block
{
char str[256];
public:
strings(char *Istr)
{
strcpy(str, Istr);
type = S_TYPE;
}
void show(void)
{
cout << "str = " << str << "\n";
}
int get_type(void)
{
return(S_TYPE);
}
char* get_value(void)
{
return(str);
}
};
class list
{
block *phead;
data_type d_type;
int cur_num;
char cur_str[256];
int user_choice(void)
{
int ans;
cout << "Enter 1 for numbers data type. 2 for string data type";
cin >> ans;
if ( ans == 1 )
{
d_type = N_TYPE;
cout << "Enter number to delete : ";
cin >> cur_num;
return(1);
}
else if ( ans == 2 )
{
d_type = S_TYPE;
cout << "Enter string to delete : ";
cin >> cur_str;
return(1);
}
else
{
cout << "Invlid Option\n";
return(0);
}
}
public:
list(void)
{
phead = NULL;
}
void add(block *pnewb)
{
if ( ! phead )
phead = pnewb;
else
{
pnewb->pnext = phead;
phead = pnewb;
}
}
void show(void)
{
block *ptmp ;
for ( ptmp = phead; ptmp ; ptmp = ptmp->pnext)
ptmp->show();
}
void del_cell(void)
{
int ans;
block *pbf, *pbl, *pdel;
numbers *pnum;
strings *pstr;
if ( ! phead )
{
cout << "List is empty\n";
return;
}
if ( user_choice() == 0 )
return;
if ( phead->get_type() == d_type )
{
if ( d_type == N_TYPE )
{
pnum = (numbers*)phead;
if ( pnum->get_value() == cur_num )
{
phead = phead->pnext;
cout << "Deleteing " << pnum->get_value() << "\n\n";
delete pnum;
return;
}
}
else
{
pstr = (strings*)phead;
if ( strcmp(pstr->get_value(), cur_str) == 0 )
{
phead = phead->pnext;
cout << "Deleteing " << pstr->get_value() << "\n\n";
delete pstr;
return;
}
}
}
for ( pbl = phead, pbf = phead->pnext ;
pbf;
pbl = pbf , pbf = pbf->pnext )
if ( pbf->get_type() == d_type )
{
if ( d_type == N_TYPE )
{
pnum = (numbers*)pbf;
if ( pnum->get_value() == cur_num )
{
pbl->pnext = pbf->pnext;
cout << "Deleteing " << pnum->get_value() << "\n\n";
delete pnum;
return;
}
}
else
{
pstr = (strings*)pbf;
if ( strcmp(pstr->get_value(), cur_str) == 0 )
{
pbl->pnext = pbf->pnext;
cout << "Deleteing " << pstr->get_value() << "\n\n";
delete pstr;
return;
}
}
}
if ( d_type == N_TYPE )
cout << "Failed to find the number " << cur_num << "in list\n";
else
cout << "Failed to find the string " << cur_str << "in list\n";
}
void find_cell(void)
{
int ans;
block *ptmp;
ans = 1;
while ( ans = user_choice() )
{
for ( ptmp = phead ; ptmp ; ptmp = ptmp->pnext )
if ( ptmp->get_type() == d_type )
{
if ( d_type == N_TYPE )
{
if (((numbers*)ptmp)->get_value() == cur_num )
{
cout << "Found number : " << cur_num << "\n";
break;
}
}
else
{
if ( strcmp( ((strings*)ptmp)->get_value(), cur_str) == 0 )
{
cout << "Found string : " << cur_str << "\n";
break;
}
}
}
if ( ! ptmp )
{
if ( d_type == N_TYPE )
cout << "Failed to find number : " << cur_num ;
else
cout << "Failed to find string : " << cur_str ;
cout << "\n";
}
}
}
};
int main(int argc, char *argv[])
{
char strtbl[3][80] = { "one", "two", "three" };
numbers *pn;
strings *ps;
int i;
list l1;
for ( i = 0 ; i < 3 ; ++i )
{
pn = new numbers(i+1);
ps = new strings(strtbl[i]);
l1.add(pn);
l1.add(ps);
}
l1.show();
l1.find_cell();
l1.del_cell();
l1.show();
l1.del_cell();
l1.show();
l1.del_cell();
l1.show();
l1.del_cell();
l1.show();
l1.del_cell();
l1.show();
l1.del_cell();
l1.show();
l1.del_cell();
l1.show();
l1.del_cell();
l1.show();
system("PAUSE");
return EXIT_SUCCESS;
}