-
Linked list problem....
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.
-
First of all, what is this line doing for you...
Code:
cin>>head->component;
I'd remove that first of all...
Also it looks like you are mixing C/C++. Why do you do this..
Code:
typedef NodeType* NodePtr;
You can ( in C++ ) just as easily say:
Code:
struct NodeType
{
ComponentType component;
NodeType *link;
};
-
I'm surprised it compiles at all. Anyway, take a look at this. Now it works.
Code:
#include <iostream.h>
#include <stdlib.h>
#include <fstream.h>
typedef int ComponentType;
//struct NodeType; // This doesn't look right... declaring the same struct twice
//typedef NodeType* NodePtr; //I don't think you can use typedef to give a pointer
// a name that has no *
struct NodeType
{
ComponentType component;
// NodePtr link;
NodeType* link; // pointer definition with *
};
void PrintList(NodeType*);
int main()
{
ifstream myFile;
NodeType* head; // replacing NodePtr with NodeType*
NodeType* newPtr; // same
NodeType* current; // same
ComponentType values;
head = new NodeType;
head -> link = NULL; // initialize link when node is created
// cin>>head->component; // what was this line for?
current=head;
myFile.open("link.txt");
if (myFile.fail())
{
cout<<"Cannot open file"<<endl;
exit(-1);
}
myFile>>values;
if(myFile.eof())
{
cout << "Input file is empty.";
return 0;
}
head ->component=values; // you weren't using the first node
// myFile>>values; // this line would prevent last value from being stored
while (!myFile.eof())
{
myFile>>values;
newPtr= new NodeType;
newPtr->link=NULL; // initialize link
newPtr->component = values;
current->link=newPtr;
current=newPtr;
// myFile>>values;
}
// current->link = NULL;
PrintList(head);
myFile.close();
return 0;
}
void PrintList(NodeType* head)
{
NodeType* current = head;
while(current)
{
cout<<current->component<<endl;
current= current->link;
}
}