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;
}

62: ptr->item;
what about it?


89. while(ptr->next !=NULL)
figure out why in ptr->next never becomes null

I'm betting on the problem being in your insert function.

i fixedthe line 62 to :
ptr->item = item;
but why would ptr->next never = NULL ?

Why on earth are you deleting ptr in List::insert() delete ptr; Once you delete it your ListPtr points to garbage, and essentially at the end of the insert you will have no valid elements in the list.

You need to delete your list in the destructor.

wow, thank you, i feel very noob lol

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.