Phuong Nguyen
Phuong Nguyen

Reputation: 13

The linked list points to NULL though I had initialized this linked list

I'm working on a linked list. But when I worked with method push_back that I defined, something happended and I don't know why. Here's my item code for the linked list. It contains the data and pointer to the next item within the list.

template <class T>
struct L1Item {
  T data;
  L1Item<T>* pNext;
  L1Item() : pNext(NULL) {}
  L1Item(T& a) : data(a), pNext(NULL) {}
};

Here's linked list code

template <class T>
class L1List {
  L1Item<T>* _pHead;// The head pointer of linked list
  size_t      _size;// number of elements in this list
public:
  L1List() : _pHead(NULL), _size(0) {}
  ~L1List();

  bool    isEmpty() {
    return _pHead == NULL;
  }
  size_t  getSize() {
    return _size;
  }

  int     push_back(T& a);// insert to the end of the list
  int     insertHead(T& a);// insert to the beginning of the list

  int     removeHead();// remove the beginning element of the list
  int     removeLast();// remove the last element of the list
};

Here's push_back method code

template <class T>
int L1List<T>::push_back(T& a) {
  // TODO: Your code goes here
  L1Item<T> *pNew, *pPre;
  pNew = new L1Item<T>();
  L1Item<T>* pH = this->_pHead;
  if (pNew == NULL) {
    return -1;
  }
  pNew->data = a;
  if (isEmpty()) {
    pH = pNew;
    pNew->pNext = NULL;
  } 
  else {
    pPre = pH;
    // travel to the end of the linked list
    for (size_t i = 0; i < this->_size - 1; i++)
        pPre = pPre->pNext;
    pNew->pNext = pPre->pNext;
    pPre->pNext = pNew;
  } 
  this->_size++;
  return 0;
}

When I debug to find bugs, debugger said that : "Exception thrown: read access violation. this was nullptr." in the line "L1Item* pH = this->_pHead;". Even though I delete "this->" it didn't work.

I am brand new on this kind of linked list. What I learnt so far is not enough to solve this problem. I even don't know the key-word to Google it.

Edit 1: Here's the caller

void LoadData(void*& pData)
{
  pData = new TDataset();
  TDataset* pNewData = new TDataset();
  LoadCity(pNewData->pCity);
  LoadLine(pNewData->pLine);
  LoadStationLine(pNewData->pStationLine);
  LoadStation(pNewData->pStation);
  LoadTrack(pNewData->pTrack);
  LoadTrackLine(pNewData->pTrackLine);
  pData = pNewData;
}

If it was wrong, please tell me how to fix it. Thank you !

Edit 2:

This is the caller for push_back method. Just ont of them in LoadData, but other LoadFunction has the same form

void LoadCity(L1List<TCity>* pCity ) {
  fstream fout;
  fout.open("cities.csv");
  string strIn;
  getline(fout, strIn);
  stringstream ssCancer(strIn);
  while (!fout.eof()) {
    string str;
    getline(fout, str);
    stringstream ss(str);
    TCity city;
    // id,name,coords,start_year,url_name,country,country_state
    string _id;
    string _start_year;

    getline(ss, _id, ',');
    getline(ss, city.name, ',');
    getline(ss, city.coords, ',');
    getline(ss, _start_year, ',');
    getline(ss, city.url_name, ',');
    getline(ss, city.country, ',');
    getline(ss, city.country_state, ',');
    city.id = stoi(_id);
    city.start_year = stoi(_start_year);
    pCity->push_back(city);
  }
}

I get no idea why this->_pHead is nullptr. Can someone explain to me ?

Upvotes: 1

Views: 87

Answers (2)

4386427
4386427

Reputation: 44329

Here

  if (isEmpty()) {
    pH = pNew;
    pNew->pNext = NULL;
  } 

you don't change _pHead but only pH which is a local variable in the function.

You want this instead

  if (isEmpty()) {
    _pHead = pNew;    // <------
    pNew->pNext = NULL;
  } 

OT:

If you want a linked list with at push_back method, you should add a tail-pointer to the class. Like

template <class T>
class L1List {
  L1Item<T>* _pHead;// The head pointer of linked list
  L1Item<T>* _pTail;

and then do this in push_back:

  pNew = new L1Item<T>();
  pNew->data = a;
  pNew->pNext = NULL;

  if (isEmpty()) {
    _pHead = pNew;
    _pTail = pNew;
  } 
  else {
    _pTail->pNext = pNew;
    _pTail = pNew;
  } 

Upvotes: 1

n. m. could be an AI
n. m. could be an AI

Reputation: 120051

L1Item<T>* pH = this->_pHead; 
...
pH = pNew;

This doesn't change _pHead.

Upvotes: 0

Related Questions