如何在C++中创建链表?

如何在C++中创建链表?,c++,data-structures,singly-linked-list,C++,Data Structures,Singly Linked List,我有这两个结构 struct CamelZombie{ int hp; int attack; CamelZombie *next; }; struct list_of_cz{ CamelZombie *head; }; 我创建了一个函数来创建具有给定值的链表: void createCamelZombie(list_of_cz *&pZ, int z_hp, int z_attack, int N){ pZ = new list_of_cz;

我有这两个结构

struct CamelZombie{
    int hp;
    int attack;
    CamelZombie *next;
};

struct list_of_cz{
    CamelZombie *head;
};
我创建了一个函数来创建具有给定值的链表:

void createCamelZombie(list_of_cz *&pZ, int z_hp, int z_attack, int N){
    pZ = new list_of_cz;
    pZ->head->hp = z_hp;
    pZ->head->attack = z_attack;
    CamelZombie *temp1 = pZ->head;
    CamelZombie *temp2 = NULL;
    for (int i = 0; i < N - 1  ; i++){
        temp2 = new CamelZombie;
        temp2->hp = z_hp;
        temp2->attack = z_attack;
        temp1->next = temp2;
        temp1 = temp2;
    }
}
然后我像这样把它放在函数main中,但随后程序崩溃了,不知道为什么

list_of_cz *pZ = NULL;
createCamelZombie(pZ, z_hp, z_attack, N);
    while (pList->head != NULL && pZ != NULL){
        atPlant(numPlant(pList) - 1, pList)->hp -= pZ->head->attack;
        if (atPlant(numPlant(pList) - 1, pList)->hp <= 0) deletePlant(numPlant(pList) - 1, pList);
        int count = 0;
        CamelZombie *z_temp;
        z_temp = pZ->head;
        while (z_temp){
            if (count == 0) z_temp->hp -= allPlantAttack(pList, numPlant(pList) - 1);
            else z_temp->hp -= allLaserAttack(pList); //trouble right here
            if (z_temp->hp <= 0) deleteCamelZombie(pZ, count);
            z_temp = z_temp->next;
            count++;
        }

似乎我在编写void createCamelZombie时遗漏了什么,因为编译器告诉我z_temp->hp没有值。请帮帮我

最好使用现有容器,如或


请逐字张贴错误信息+pZ->head 0xcdcdcd{hp=??attack=?next=?}CamelZombie*编译器告诉我这属于您的问题。和a一样,您目前没有提供。关于这个看似奇怪的值,。您正在访问一个未初始化的指针。注意:这是运行时错误消息而不是编译器问题。如何在C++中创建链接列表?使用
#include <iostream>
#include <string>
#include <list>

struct CamelZombie{
  std::string name; //added for demonstration purposes
  int hp;
  int attack;
  //pointer to next zombie not required
};

std::list<CamelZombie> createCamelZombie2(int z_hp, int z_attack, int N) {

  std::list<CamelZombie> result;

  for (int i = 0; i < N; i++){

    CamelZombie newZombie;
    newZombie.name = "Zombie"+std::to_string(i);
    newZombie.hp = z_hp;
    newZombie.attack = z_attack;
    newZombie.next = NULL;

    result.push_back(newZombie);
  }

  return result;
}
int main() {
  std::list<CamelZombie> listOfZombies2 = createCamelZombie2(10,20,10);

  for(std::list<CamelZombie>::iterator list_iter = listOfZombies2.begin(); 
      list_iter != listOfZombies2.end(); list_iter++)
  {
    std::cout<<list_iter->name<<std::endl;
  }

}
struct CamelZombie{
  std::string name; //added for demonstration purposes
  int hp;
  int attack;
  CamelZombie *next;
};

CamelZombie* createCamelZombie(int z_hp, int z_attack, int N){

  CamelZombie *result = NULL;
  CamelZombie *work = NULL; //keep track of the last node in the list

  for (int i = 0; i < N; i++){

    //create new zombie
    CamelZombie *newZombie = new CamelZombie();
    newZombie->name = "Zombie"+std::to_string(i);
    newZombie->hp = z_hp;
    newZombie->attack = z_attack;
    newZombie->next = NULL;

    if (result==NULL) {
      result = newZombie;
      work =result;
    } else {
      work->next = newZombie;
      work = newZombie;
    }
  }

  return result;
}
int main() {

  CamelZombie *listOfZombies = createCamelZombie(10,20,10);

  CamelZombie *work = listOfZombies;

  // print zombie names to screen ---------
  while (work!=NULL) {
    std::cout << work->name << std::endl;
    work = work->next;
  }
  work = listOfZombies;
  while (work!=NULL) {
    CamelZombie *temp =work->next;
    delete work;
    work = temp;
  }