C 如何在不创建变量的情况下将项添加到结构中
我使用结构来存储字符串和整数,如下所示:C 如何在不创建变量的情况下将项添加到结构中,c,struct,C,Struct,我使用结构来存储字符串和整数,如下所示: struct movement { char *direction; int steps; }; 我可以通过这样做将项添加到结构中 struct movement m1= { "right",20 }; struct movement m2= { "left" ,10 }; 我试图实现的最终结果是收集用户输入(例如“right 20”),并将其存储在struct中。如何在不使用变量(m1、m2等)的情况下将未知数量的用户输入存储到结构
struct movement {
char *direction;
int steps;
};
我可以通过这样做将项添加到结构中
struct movement m1= { "right",20 };
struct movement m2= { "left" ,10 };
我试图实现的最终结果是收集用户输入(例如“right 20”),并将其存储在struct中。如何在不使用变量(m1、m2等)的情况下将未知数量的用户输入存储到结构中,因为我不知道最后会有多少项。使用链接列表存储无限数量的移动。 对于每个移动,在链表中创建一个节点并更新下一个指针
struct node {
struct movement m;
node* next;
}
使用LinkedList存储无限数量的移动。 对于每个移动,在链表中创建一个节点并更新下一个指针
struct node {
struct movement m;
node* next;
}
听起来你并不是真的想“将值存储到结构中”,而是想存储一系列独立的结构实例;每个用户输入一个 三种最基本的方法是:
- 一个数组,在编译时选择其大小。要使其“足够大”以满足合理的投入,应该不会太难
- 在运行时设置(然后增加)其大小的数组
- 结构实例的链接列表
struct movement movements[10000];
在全局级别,在64位系统上,这可能只需要120 KB。请注意,这不包括方向字符串的内存;如果总是从“右”和“左”(也可能是“上”/“下”)中选择它们,则可以将其表示为枚举:
enum direction { DIRECTION_LEFT = 0, DIRECTION_RIGHT, DIRECTION_UP, DIRECTION_DOWN };
这将使结构“自包含”并且(在64位系统上)更小,因为枚举将小于指针
使用realloc()
动态增长数组并不难,您可以像经常使用的那样轻松查找。听起来您并不是真的想“将值存储到结构中”,而是想存储一系列独立的结构实例;每个用户输入一个
三种最基本的方法是:
- 一个数组,在编译时选择其大小。要使其“足够大”以满足合理的投入,应该不会太难
- 在运行时设置(然后增加)其大小的数组
- 结构实例的链接列表
选择哪一个取决于你认为哪一个最简单。如果可能的话,静态外观总是最简单的。你可以很容易地得到像这样的东西
struct movement movements[10000];
在全局级别,在64位系统上,这可能只需要120 KB。请注意,这不包括方向字符串的内存;如果总是从“右”和“左”(也可能是“上”/“下”)中选择它们,则可以将其表示为枚举:
enum direction { DIRECTION_LEFT = 0, DIRECTION_RIGHT, DIRECTION_UP, DIRECTION_DOWN };
这将使结构“自包含”并且(在64位系统上)更小,因为枚举将小于指针
使用realloc()
动态增长数组并不难,您可以像经常使用的那样轻松查找该数组。使用链接列表。这是一种递归数据结构,非常适合您的需要
下面是我不久前编写的一些示例代码,可能会有所帮助:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* basic linked list structure */
typedef struct node node_t;
struct node {
char *direction;
int steps;
node_t *next;
};
/* pointers to the head and tail of the list */
typedef struct {
node_t *head;
node_t *foot;
} list_t;
list_t *initialize_list(void);
list_t *insert_nodes(list_t *list, char *direction, int steps);
void free_list(list_t *list);
node_t *generate_node(void);
void print_list(list_t *list);
void exit_if_null(void *ptr, const char *msg);
int
main(int argc, char const *argv[]) {
list_t *list;
/* empty list created */
list = initialize_list();
/* inserting information one a time */
list = insert_nodes(list, "right", 20);
list = insert_nodes(list, "left", 10);
print_list(list);
/* freeing list at the end */
free_list(list);
list = NULL;
return 0;
}
/* function to insert information into a node */
list_t
*insert_nodes(list_t *list, char *direction, int steps) {
/* called generate_node() to create a new node */
node_t *new;
new = generate_node();
/* puts steps information into node */
new->steps = steps;
/* allocates space for direction string */
/* this is needed because *direction is a pointer */
new->direction = malloc(strlen(direction)+1);
/* copies direction info into node */
strcpy(new->direction, direction);
/* inserting information at the tail of the list */
new->next = NULL;
if (list->foot == NULL) {
/* first insertion into list */
list->head = list->foot = new;
} else {
list->foot->next = new;
list->foot = new;
}
/* returns modified list */
return list;
}
.* function which generates new nodes */
node_t
*generate_node(void) {
node_t *newnode;
/* create space for new node */
newnode = malloc(sizeof(*newnode));
exit_if_null(newnode, "Allocation");
/* initialize node info to nothing */
newnode->direction = NULL;
newnode->steps = 0;
return newnode;
}
/* creates the empty linked list */
list_t
*initialize_list(void) {
list_t *list;
create space for list */
list = malloc(sizeof(*list));
exit_if_null(list, "Allocation");
/* set pointers to NULL */
/* We don't want them pointing at anything yet */
list->head = list->foot = NULL;
return list;
}
/* function which prints entire list */
void
print_list(list_t *list) {
/* start at the head of the list */
node_t *curr = list->head;
while (curr) {
printf("%s %d\n", curr->direction, curr->steps);
/* steps through the list */
curr = curr->next;
}
}
/* function which frees nodes */
void
free_list(list_t *list) {
node_t *curr, *prev;
/* start at beginning of list */
curr = list->head;
/* frees nodes one at a time */
while(curr) {
prev = curr;
curr = curr->next;
free(prev);
}
/* frees entire list */
free(list);
}
/* function which checks malloc(), and whether enough space was allocated */
void
exit_if_null(void *ptr, const char *msg) {
if (!ptr) {
printf("Unexpected null pointer: %s\n", msg);
exit(EXIT_FAILURE);
}
}
#包括
#包括
#包括
/*基本链表结构*/
类型定义结构节点;
结构节点{
字符*方向;
int步;
节点_t*下一步;
};
/*指向列表开头和结尾的指针*/
类型定义结构{
节点头;
足节;
}名单;;
列表*初始化列表(无效);
列表*插入节点(列表*列表,字符*方向,整数步);
无效自由列表(列表*列表);
节点\u t*生成\u节点(void);
作废打印列表(列表*列表);
如果无效,则无效退出(无效*ptr,常量字符*msg);
int
main(int argc,char const*argv[]){
列表_t*列表;
/*已创建空列表*/
list=初始化_list();
/*一次插入一个信息*/
列表=插入节点(列表,“右”,20);
列表=插入节点(列表,“左”,10);
打印列表(列表);
/*最后释放列表*/
自由列表(列表);
列表=空;
返回0;
}
/*函数将信息插入到节点中*/
列表
*插入节点(列表、字符*方向、整数步){
/*调用generate_node()创建新节点*/
节点_t*新建;
新建=生成_节点();
/*将步骤信息放入节点*/
新建->步骤=步骤;
/*为方向字符串分配空间*/
/*这是必需的,因为*方向是指针*/
新建->方向=malloc(strlen(方向)+1);
/*将方向信息复制到节点中*/
strcpy(新建->方向,方向);
/*在列表末尾插入信息*/
新建->下一步=空;
如果(列表->脚==NULL){
/*第一次插入列表*/
列表->头部=列表->脚部=新建;
}否则{
列表->脚->下一步=新建;
列表->脚=新建;
}
/*返回修改后的列表*/
退货清单;
}
*生成新节点的函数*/
节点
*生成_节点(void){
node_t*newnode;
/*为新节点创建空间*/
newnode=malloc(sizeof(*newnode));
如果为空,则退出(新节点,“分配”);
/*将节点信息初始化为空*/
newnode->direction=NULL;
newnode->steps=0;
返回newnode;
}
/*创建空的链接列表*/
列表
*初始化\u列表(无效){
列表_t*列表;
为列表创建空间*/
列表=malloc(sizeof(*list));
如果为空,则退出(列表,“分配”);
/*将指针设置为NULL*/
/*我们还不想让他们指着任何东西*/
列表->头=列表->脚=空;
退货清单;
}
/*用于打印整个列表的函数*/
无效的
打印列表(列表*列表){
/*从名单的最前面开始*/
节点\u t*curr=list->head;
while(curr){
printf(“%s%d\n”,当前->方向,当前->步骤);
/*逐步浏览列表*/
当前=当前->下一步;
}
}
/*功能