-3

I got these two structs

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

struct list_of_cz{
    CamelZombie *head;
};

I've made a function to create linked list with given value:

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

Then i put it in function main like this, but then the propram crashed, don't know why.

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

Seem like i miss something when writing void createCamelZombie() 'cause the compiler tells me that z_temp->hp don't have a value. Please help me!

Sơn Phan
  • 1,120
  • 6
  • 12
  • 1
    Post the error message verbatim please! – πάντα ῥεῖ Sep 10 '16 at 14:48
  • + pZ->head 0xcdcdcdcd {hp=??? attack=??? next=??? } CamelZombie * Compiler tells me this – Sơn Phan Sep 10 '16 at 14:52
  • 1
    *That belongs **in your question***. (as does a [minimal, complete, and verifiable example](https://stackoverflow.com/help/mcve), which you're currently not providing. Regarding that seemingly odd value, [you may find **this** helpful](http://stackoverflow.com/questions/127386/in-visual-studio-c-what-are-the-memory-allocation-representations). – WhozCraig Sep 10 '16 at 14:53
  • You are accessing an uninitialized pointer. Note this is a runtime error message and not a compiler issue. – πάντα ῥεῖ Sep 10 '16 at 14:53
  • The right tool to solve such problems is your debugger. You should step through your code line-by-line *before* asking on Stack Overflow. For more help, please read [How to debug small programs (by Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). At a minimum, you should \[edit] your question to include a [Minimal, Complete, and Verifiable](http://stackoverflow.com/help/mcve) example that reproduces your problem, along with the observations you made in the debugger. – πάντα ῥεῖ Sep 10 '16 at 14:54
  • 4
    "How to create a linked list in C++?" - Use [std::list](http://en.cppreference.com/w/cpp/container/list) . – Jesper Juhl Sep 10 '16 at 14:59
  • Or [std::forward_list](http://www.en.cppreference.com/w/cpp/container/forward_list). –  Sep 10 '16 at 15:00
  • "How to create a linked list in C++?" -- Is there anything preventing you from searching the internet for "c++ linked list tutorial"? – Thomas Matthews Sep 10 '16 at 16:32

1 Answers1

2

Preferably use an existing container like std::vector or std::list

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

Use the code like this.

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

}

If you really want to use your own linked list try the code below.

  • A seperate struct (list_of_cz) for the list is not required. Each zombie links to the next zombie. So just keep a pointer to the first zombie.
  • createCamelZombie function returns a pointer to the first zombie in the list (no need to use the function parameter (list_of_cz *&pZ) to get the zombie list)
  • Too many underscores and Z makes the code hard to read.
  • If you use pointers you need to clean up memory yourself.

.

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

Example of how to use the code.

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

And free memory.

  work = listOfZombies;
  while (work!=NULL) {
    CamelZombie *temp =work->next;
    delete work;
    work = temp;
  }
robor
  • 2,969
  • 2
  • 31
  • 48