c++ - 尽管我已经初始化了这个链表,但链表指向 NULL

标签 c++

我正在处理链表。但是当我使用我定义的方法 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;
}

当我调试以查找错误时,调试器说:“抛出异常:读取访问冲突。 this was 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。谁能给我解释一下?

最佳答案

这里

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

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

你想要这个

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

加类:

如果您想要一个带有 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;
  } 

关于c++ - 尽管我已经初始化了这个链表,但链表指向 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58046303/

相关文章:

c++ - 包括 Boost ASIO 与 Amazon Gamelift SDK 冲突

c++ - 我的 if 语句逻辑有什么问题?

c++ - 显示独立对话框

c++ - Qt 5.9 中的 Q_FLAG_NS 不提供按位运算符

c++ windows通过USB串行获取USB端口和集线器

C++: 'this' 关键字错误

c++ - C++ 中的 "Pimp my Library"

c++ - OSX 10.7.5 上的 node-gyp -- dyld : lazy symbol binding failed: Symbol not found

c++ - unique_ptr(std::nullptr_t) 和模板

c++ - 在 std::vector 中实例化对象时出现错误 C2280/复制构造函数问题?