I have encounter trouble while printing my link list.
it displays nothing on screen....i don't know why?
Here is my code:
Code:
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
typedef int ComponentType;
struct NodeType;
typedef NodeType* NodePtr;
struct NodeType
{
ComponentType component;
NodePtr link;
};
void PrintList(NodePtr);
int main()
{
ifstream myFile;
NodePtr head;
NodePtr newPtr;
NodePtr current;
ComponentType values;
head = new NodeType;
cin>>head->component;
current=head;
myFile.open("link.txt");
if (myFile.fail())
{
cout<<"Cannot open file"<<endl;
exit(-1);
}
myFile>>values;
while (!myFile.eof())
{
newPtr= new NodeType;
newPtr->component = values;
current->link=newPtr;
current=newPtr;
myFile>>values;
}
current->link = NULL;
PrintList(head);
myFile.close();
return 0;
}
void PrintList(NodePtr head)
{
NodePtr current = head;
while(current)
{
cout<<current->component<<endl;
current= current->link;
}
}
This little program read from a text file call "link.txt", inside this text file is only four number:
Unfortunately, my program print nothing on the screen.
(Since i can compile it, i think it would be a logical error)
Pleas help.