I'm creating a linked list,adding the data I've read from file in each node but as I'm trying to print what I hold in nodes my output is some symbols.And even when I cout what I hold in my nodes in ADD function it's ok but as it goes to print function everything goes wrong and I'm pretty sure that my add and print functions work accurately.
class Gnode {
public:
const char *data;
Gnode *link;
};
class Glist {
public:
Gnode *first;
public:
Glist () {
first=NULL;
}
void Add (const char *d);
void print ();
};
void Glist::Add (const char *d){
Gnode *temp,*temp2;
temp=new Gnode;
temp->link=NULL;
temp->data=d;
if (first==NULL){
first=temp;
}
else{
temp2=first;
while (temp2->link!=NULL){
temp2=temp2->link;
}
temp2->link=temp;
}
}
void Glist::print( ){
Gnode *temp1;
temp1=first;
do
{
if(temp1==NULL)
cout<<"END"<<endl;
else
{
cout<<temp1->data<<" ";
temp1=temp1->link;
}
}
while (temp1!=NULL);
}
int main(){
ifstream f("A.txt");
string s;
Glist A;
getline (f,s,' ');
const char *p1=s.c_str();
A.Add(p1);
A.print();
getch();
}