DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   Class Link-List help!? (http://www.daniweb.com/forums/thread149513.html)

DevC++4.9.9.2 Oct 6th, 2008 12:28 pm
Class Link-List help!?
 
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;
}

chococrack Oct 6th, 2008 12:47 pm
Re: Class Link-List help!?
 
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.

DevC++4.9.9.2 Oct 6th, 2008 1:04 pm
Re: Class Link-List help!?
 
i fixedthe line 62 to :
ptr->item = item;
but why would ptr->next never = NULL ?

stilllearning Oct 6th, 2008 1:25 pm
Re: Class Link-List help!?
 
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.

DevC++4.9.9.2 Oct 6th, 2008 1:56 pm
Re: Class Link-List help!?
 
wow, thank you, i feel very noob lol


All times are GMT -4. The time now is 4:25 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC