I am stuck, im trying to insert and print out this list of numbers from a file, however i just get an infinite loop every time... any suggestions?
// This is the implementation file for class List
#include <iostream>
#include <stddef.h> // to access NULL
#include "List.h"
#include<fstream.h>
using namespace std;
typedef NodeType* NodePtr;
struct NodeType
{
ItemType item;
NodePtr next;
};
List::List()
// Post: listPtr is set to NULL.
{
listPtr = NULL;
length = 0;
}
//***********************************************************
//List::List(const List& otherList)
// Copy-constructor for List.
//{
//}
//***********************************************************
bool List::IsThere(ItemType item) const
// Post: If item is in the list IsThere is
// True; False, otherwise.
{
NodeType *cur_pos;
cur_pos = listPtr;
if (cur_pos == NULL)
return false;
while((cur_pos -> next)!= NULL)
{
if (cur_pos -> item == item)
return true;
cur_pos = cur_pos -> next;
}
if (cur_pos -> item == item)
return true;
return false;
}
//***********************************************************
void List::Insert(ItemType item)
// Pre: item is not already in the list.
// Post: item is the first item in the list.
{
NodeType *ptr;
ptr = new NodeType;
ptr->item;
ptr->next = listPtr;
listPtr = ptr;
delete ptr;
length++;
}
//***********************************************************
void List::Delete(ItemType item)
// Pre: item is in the list.
// Post: item is no longer in the list.
{
// FILL IN Code.
}
//***********************************************************
void List::Print() const
// Post: Items on the list are printed on the screen.
{
if (listPtr == NULL)
cout << "List is empty\n";
else
{
NodeType *ptr;
ptr = listPtr;
while(ptr->next !=NULL)
{
cout << ptr->item << endl;
ptr = ptr->next;
}
cout << ptr->item << endl;
delete ptr;
}
}
//***********************************************************
int List::Length() const
// Post: Number of items have been counted; result returned.
{
return length;
}
//***********************************************************
List::~List()
// Post: All the components are deleted.
{
// FILL IN Code.
}
int main()
{
List mylist;
ifstream data;
data.open("int.dat");
if (!data)
cout << "Error opening file\n";
else
{
int item;
while (!data.eof())
{
data >> item;
cout << "Item: " << item << endl;
mylist.Insert(item);
}
mylist.Print();
}
system("PAUSE");
return 0;
}