Code:
/*
can yoou please tell me what is wrong with this linklist? i think it is overiding during my read.But how to fix it?
*/
// Created by: Pierre-Michel Jean-Louis
// Date: Febuary 29th 2003
// File name: exercise2.cc
// Purpose: Reads linklist to a file
// input: list.txt
// output: onscreen
# include <iostream.h>
# include <fstream.h>
struct nodetype;
typedef int itemtype;
typedef nodetype * ptrtype;
struct nodetype
{
itemtype item;
ptrtype link;
};
int main()
{
ptrtype head, newp, currentp;
itemtype temp;
ifstream file;
file.open("list.txt");
head = new nodetype;
file >> head->item;
if (file)
{
currentp = head;
file >> temp;
while (file)
{
newp = new nodetype;
newp->item = temp;
currentp->link = newp;
file >> temp;
}
currentp->link = NULL;
}
else
head = NULL;
currentp = head;
while(currentp != NULL)
{
cout << currentp->item <<endl;
currentp = currentp->item;
}
return 0;
}
May I ask why you need all those headers? As far as I can tell, all you need is iostream and fstream... perhaps even just fstream, as it includes iostream.