2016-09-10 73 views
-3

我得到了這兩個結構中如何在C++中創建鏈表?

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

然後我把它的功能主要是這樣,但隨後的propram墜毀,不知道爲什麼。

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沒有價值。請幫幫我!

+1

請逐字發佈錯誤消息! –

+0

+ \t \t pZ-> head \t 0xcdcdcdcd {hp = ???攻擊= ???接下來= ??? } \t CamelZombie * 編譯器告訴我這個 –

+1

*在你的問題***屬於**。 (就像你目前沒有提供的[minimal,complete,and verifiable example](https://stackoverflow.com/help/mcve)),關於這個看似奇怪的值,[你可能會發現** this ** (http://stackoverflow.com/questions/127386/in-visual-studio-c-what-are-the-memory-allocation-representations) – WhozCraig

回答

1

優選地使用現有的容器等std::vectorstd::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; 
} 

使用這樣的代碼。

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

} 

如果你真的想使用自己的鏈表,請嘗試下面的代碼。

  • 列表的單獨結構(list_of_cz)不是必需的。每個殭屍鏈接到下一個殭屍。所以只要保持一個指向第一個殭屍的指針。
  • createCamelZombie函數返回列表中的指針首個殭屍(無需使用功能參數(list_of_cz * & PZ)讓殭屍列表)
  • 太多下劃線和Z使代碼難以閱讀。
  • 如果你使用指針,你需要自己清理內存。

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