如何在c++中创建链表
How to create a linked list in 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;
}
}
然后我像这样把它放在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
没有值。请帮帮我!
最好使用std::vector或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;
}
像这样使用代码。
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 (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;
}
和free memory.
work = listOfZombies;
while (work!=NULL) {
CamelZombie *temp =work->next;
delete work;
work = temp;
}
相关文章:
- 创建链表
- 使用 _getch 创建链表并反转列表
- 创建链表,但无法按 CPP 的预期工作
- 如何使用初始值设定项列表来指导创建链表?
- 尝试连接两种不同类型的结构来创建链表
- C++ 如何创建链表的副本作为类对象
- 我是C++新手。我在此代码中遇到错误,无法使用 SwapNodes 函数C++创建链表。该函数不交换节点
- 如何在C++中创建链表列表?
- C++简体版链表,当我加载数据以创建链表并输入更多数据时,似乎存在差距
- 尝试创建链表堆栈时出现链接器错误
- 如何在链表中创建链表
- 通过在循环C++中从文件中读取文本来创建链表
- C++ - 创建链表时出现段错误
- 创建链表,模板化堆栈
- 使用基类和派生类创建链表
- 第一次尝试创建链表
- 创建链表
- 如何使用结构体创建链表
- 在c++中创建链表
- 创建链表插入函数,但代码未运行