首页 > 解决方案 > 尽管我已经初始化了这个链表,但链表指向 NULL

问题描述

我正在处理一个链接列表。但是当我使用我定义的方法 push_back 时,发生了一些事情,我不知道为什么。这是我的链接列表的项目代码。它包含数据和指向列表中下一项的指针。

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

这是链表代码

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

这是 push_back 方法代码

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

当我调试以查找错误时,调试器说:“抛出异常:读取访问冲突。这是 nullptr。” 在“L1Item* pH = this->_pHead;”行中。即使我删除了“this->”,它也没有用。

我在这种链表上是全新的。到目前为止,我所学到的还不足以解决这个问题。我什至不知道谷歌它的关键词。

编辑1:这是来电者

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

如果是错误的,请告诉我如何修复它。谢谢 !

编辑2:

这是 push_back 方法的调用者。在 LoadData 中只有一个,但其他 LoadFunction 具有相同的形式

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

我不知道为什么 this->_pHead 是 nullptr。有人可以向我解释吗?

标签: c++

解决方案


这里

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

您不会更改_pHead,而只会更改pH函数中的局部变量。

你想要这个

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

旧时:

如果你想要一个带有 at 方法的链表push_back,你应该在类中添加一个尾指针。喜欢

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

然后在push_back

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

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

推荐阅读