Cocos2d如何实现一个指针的添加节点

前端之家收集整理的这篇文章主要介绍了Cocos2d如何实现一个指针的添加节点前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何定义一个指向NPC的指针,
struct NPC * pn;

用户在终端输入npc的数量,动态分配npc的空间

#include <stdio.h>

#include <stdlib.h>


typedef struct _NPC{

char name[20];

int hp;

int act;

}NPC;

typedef struct _NODE{

NPC data;

struct _NODE * pNext;

}NODE;//一个节点


NODE * head=NULL; //定义头结点

void addNODE(){

if (head==NULL) {

head=malloc(sizeof(NODE));

printf("请输入NPC信息名字 hp act");

scanf("%s %d %d",head->data.name,&head->data.hp,&head->data.act);

head->pNext=NULL;

}else{

NODE * p=head;//p指向头节点

while (p->pNext!=NULL) {

p=p->pNext;//p指向下一个节点

}

p->pNext=malloc(sizeof(NODE));

p=p->pNext;

->data.act);

p->pNext=NULL;

}

printf("添加节点成功\n");

}

void printNODE(){

NODE *p=head;

while (p!=NULL) {

printf("NPC名字 %s hp %d atc %d\n",p->data.name,p->data.hp,p->data.act);

p=p->pNext;

}

printf("输出所有节点成功\n");

}

void insertNODE(){

//创建一个新节点

NODE * newNODE=malloc(sizeof(NODE));

printf("请输入NPC的名子 hp act\n");

scanf("NPCdata.name,&newNODE->data.hp,&newNODE->data.act);

printf("请输入插入到第几个节点之后\n");

int index;

int count=0;

NODE * p=head;

scanf("%d",&index);

while (p!=NULL) {

count++;

if (count>=index) {

break;

}

p=p->pNext;

}

NODE *p2=p->pNext;

p->pNext=newNODE;

newNODE->pNext=p2;

printf("插入节点成功\n");

}

void deleteNODE(){

int index;

printf("请输入要删除第几个节点\n");

scanf("%d",&index);

int num=0;

NODE * p=head;

while (p!=NULL) {

num++;

p=p->pNext;

}

if (index==1) {

p=head;

head=head->pNext;

free(p);

}else if (index==num){

NODE * p=head;

while (p->pNext->pNext!=NULL) {

p=p->pNext;

}

free(p->pNext->pNext);

p->pNext=NULL;

}else{

int count=1;

NODE * p=head;

while (p!=NULL) {

count++;

if (count==index) {

break;

}

p=p->pNext;

NODE * p2=p->pNext;

p->pNext=p2->pNext;

free(p2);

}

printf("删除节点成功\n");

}

}

void modifyNODE(){

int index;

printf("请输入有修改的节点\n");

scanf("%d",&index);

NODE * p=head;

int count=0;

while (p!=NULL) {

count++;

if (count>=index) {

break;

}

p=p->pNext;

printf("请输入NPC的名字 hp act\n");

scanf("%s %d %d\n",&p->data.hp,&p->data.act);

}

printf("修改节点");

}

int main(int argc,const char * argv[]) {

printf("NPC管理系统\n");

int menuselect;

while (1) {

printf("1.添加NPC节点\n");

printf("2.输出所有NPC节点\n");

printf("3。插入NPC节点\n");

printf("4.删除NPC节点\n");

printf("5.修改NPC节点\n");

printf("6.退出\n");

scanf("%d",&menuselect);

switch (menuselect) {

case 1://添加节点

addNODE();

break;

case 2://输出节点

printNODE();

break;

case 3://插入节点

insertNODE();

break;

case 4://删除节点

deleteNODE();

break;

case 5://修改节点

modifyNODE();

break;

case 6://退出

return 0;

break;

default:

break;

}

}

return 0;

}

原文链接:https://www.f2er.com/cocos2dx/344248.html

猜你在找的Cocos2d-x相关文章