如何解决如何在Netlogo中将列表项添加到列表中?
我有一个称为“第一名单”的列表。 每当我生成其他列表,例如[2 3],[1 4 6],...我想将这些列表添加到该第一列表中: 第一列表:[[2 3],[1 4 6],...]。 如何在NetLogo中做到这一点? 我知道我可以添加项目,但不能将列表添加到列表中
解决方法
当然可以,例如
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFER_SIZE 128
const char *FILE_GAME_DATA_PATH = "./game.txt";
struct game_tag
{
char gname[20];
struct game_tag *next;
} * head;
//struct game_tag g;
typedef struct game_tag GAME;
//file functions
void read_file();
//util functions.
int menu();
void print_game(GAME *game);
void release(GAME *data);
//core
void display_game();
void quite();
//link
int count_elements(GAME *elements);
int count();
int main(void)
{
int selected;
read_file();
while (1)
{
selected = menu();
switch (selected)
{
case 1:
display_game();
break;
default:
printf("cannot find your option!");
break;
}
}
}
void display_game()
{
read_file();
GAME *game = head;
if(game == NULL)
{
printf("\nNo Game!\n");
return;
}
print_game(game);
}
void print_game(GAME *game)
{
int records_count = 0;
printf("\n========== GAME ==========\n");
while(game != NULL)
{
printf("\n");
printf("Game Name: %s\n ",game->gname);
game = game->next;
records_count++;
}
printf("\nRecords: %d has been loaded successfully!\n",records_count);
release(game);
}
int menu()
{
printf("\n(1) Display Game details\n");
int choosen;
printf("\nEnter your option: ");
scanf("%d",&choosen);
return choosen;
}
void add_game(char game_name[20])
{
GAME *temp,*iterator;
temp = (struct game_tag *)malloc(sizeof(struct game_tag));
GAME info;
memcpy(info.gname,game_name,20);
//temp = head;
iterator = head;
if (head == NULL)
{
head = temp;
head->next = NULL;
}
else
{
while (iterator->next != NULL)
{
iterator = iterator->next;
}
temp->next = NULL;
iterator->next = temp;
}
}
void read_file()
{
if(head != NULL)
{
GAME *temp;
while(head != NULL)
{
temp = head;
head = head->next;
free(temp);
}
}
FILE *file;
file = fopen(FILE_GAME_DATA_PATH,"r");
if(file == NULL)
{
printf("Cannot read file: %s",FILE_GAME_DATA_PATH);
exit(EXIT_FAILURE);
}
char game_name[20];
int i;
while(!feof(file))
{
char no[BUFFER_SIZE];
fgets(game_name,sizeof(game_name),file);
i=0;
while(game_name[i] != '\0')
{
i++;
}
game_name[i] = '\0';
add_game(game_name);
}
fclose(file);
}
void quite()
{
printf("\nGoodbye!");
exit(EXIT_SUCCESS);
}
void release(GAME *data)
{
if (data == NULL)
{
return;
}
// free the nodes
// because it can be use in memory
// we need to clear it first
// before we re-initailize the new data
GAME *temp;
while (data != NULL)
{
temp = data;
data = data->next;
free(temp);
}
}
如果您有变量,例如:
let mylist [1 2]
set mylist lput [3 4] mylist
print mylist ; prints [1 2 [3 4]]
要使用它们创建列表,您需要使用内置的原始“列表”,例如:
let x 42
let y "string"
要创建包含两个以上变量(或只有1个)的变量的列表,您需要用括号将其封装起来,例如:
let mylist2 list x y
我强烈建议您在链接https://ccl.northwestern.edu/netlogo/docs/dictionary.html#list
中引用netlogo词典。如果使用原始数字和字符串,例如[1 2“ three”],则只能使用“ [”和“]”创建列表,但是如果要使用变量创建列表,则需要使用“列表”原语,例如(list xy“ a string” 5)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。