链表
来源:互联网 发布:云计算行业标准 编辑:程序博客网 时间:2024/06/02 01:37
// ConsoleApplication8.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <stdlib.h>
enum NPCID{
NOMIN=1,
SHANREN,
GONGREN
};
typedef struct
{
char name[20];
int classID;
int x;
int y;
}Player;
typedef struct _Node
{
Player* data;
struct _Node*next;
}Node;
Node*head;
void addNode();
void printNode();
int _tmain(int argc, _TCHAR* argv[])
{
NPCID id;
while (1)
{
printf("NPC管理系统,1输入信息 2查询NPC 3打印 4t退出");
//printf("please input");
scanf_s("%d",&id);
switch (id)
{
case NOMIN:
printf(" 您选中了1添加功能");
addNode();
break;
case SHANREN:
printf("您选中了查询");
break;
case GONGREN:
printf("您选中了打印");
break;
default:
exit(0);
break;
}
}
return 0;
}
void addNode(){
Node*newNode=(Node*)malloc(sizeof(Node));
newNode->data=(Player*)malloc(sizeof(Player));
newNode->next=NULL;
printf_s("请输入内容");
scanf_s("%s %d %d %d",newNode->data->name,&newNode->data->classID,&newNode->data->x,&newNode->data->y);
if(head==nullptr){
//若头结点为空的话。则把nnewNode赋给head
head=newNode;
}else{
//从末尾插入
Node*node;
node=head;
while (node!=NULL)
{
//往下面传递下去
node=node->next;
}
node->next=newNode; //插入最后
}
}
void printNode(){
Node*node;
node=head;
while (node!=NULL)
{
printf_s("%s %d %d %d",node->data->name,node->data->classID,node->data->x,node->data->y);
node=node->next;
}
}
//
#include "stdafx.h"
#include <stdlib.h>
enum NPCID{
NOMIN=1,
SHANREN,
GONGREN
};
typedef struct
{
char name[20];
int classID;
int x;
int y;
}Player;
typedef struct _Node
{
Player* data;
struct _Node*next;
}Node;
Node*head;
void addNode();
void printNode();
int _tmain(int argc, _TCHAR* argv[])
{
NPCID id;
while (1)
{
printf("NPC管理系统,1输入信息 2查询NPC 3打印 4t退出");
//printf("please input");
scanf_s("%d",&id);
switch (id)
{
case NOMIN:
printf(" 您选中了1添加功能");
addNode();
break;
case SHANREN:
printf("您选中了查询");
break;
case GONGREN:
printf("您选中了打印");
break;
default:
exit(0);
break;
}
}
return 0;
}
void addNode(){
Node*newNode=(Node*)malloc(sizeof(Node));
newNode->data=(Player*)malloc(sizeof(Player));
newNode->next=NULL;
printf_s("请输入内容");
scanf_s("%s %d %d %d",newNode->data->name,&newNode->data->classID,&newNode->data->x,&newNode->data->y);
if(head==nullptr){
//若头结点为空的话。则把nnewNode赋给head
head=newNode;
}else{
//从末尾插入
Node*node;
node=head;
while (node!=NULL)
{
//往下面传递下去
node=node->next;
}
node->next=newNode; //插入最后
}
}
void printNode(){
Node*node;
node=head;
while (node!=NULL)
{
printf_s("%s %d %d %d",node->data->name,node->data->classID,node->data->x,node->data->y);
node=node->next;
}
}
0 0