c - How can I add items into a struct without creating variables -
i using struct store string , integer so:
struct movement { char *direction; int steps; };
i can add items struct doing this
struct movement m1= { "right",20 }; struct movement m2= { "left" ,10 };
the end result trying achieve collect user inputs (e.g. "right 20"), , store in struct. how can store unknown number of user inputs struct without use of variables (m1, m2 etc) since not know how many items there @ end.
use linked list. recursive data structure great want.
here example code wrote while ago might help:
#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 head , tail of 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 1 time */ list = insert_nodes(list, "right", 20); list = insert_nodes(list, "left", 10); print_list(list); /* freeing list @ end */ free_list(list); list = null; return 0; } /* function insert information node */ list_t *insert_nodes(list_t *list, char *direction, int steps) { /* called generate_node() create new node */ node_t *new; new = generate_node(); /* puts steps information node */ new->steps = steps; /* allocates space direction string */ /* needed because *direction pointer */ new->direction = malloc(strlen(direction)+1); /* copies direction info node */ strcpy(new->direction, direction); /* inserting information @ tail of list */ new->next = null; if (list->foot == null) { /* first insertion list */ list->head = list->foot = new; } else { list->foot->next = new; list->foot = new; } /* returns modified list */ return list; } .* function generates new nodes */ node_t *generate_node(void) { node_t *newnode; /* create space new node */ newnode = malloc(sizeof(*newnode)); exit_if_null(newnode, "allocation"); /* initialize node info nothing */ newnode->direction = null; newnode->steps = 0; return newnode; } /* creates empty linked list */ list_t *initialize_list(void) { list_t *list; create space list */ list = malloc(sizeof(*list)); exit_if_null(list, "allocation"); /* set pointers null */ /* don't want them pointing @ yet */ list->head = list->foot = null; return list; } /* function prints entire list */ void print_list(list_t *list) { /* start @ head of list */ node_t *curr = list->head; while (curr) { printf("%s %d\n", curr->direction, curr->steps); /* steps through list */ curr = curr->next; } } /* function frees nodes */ void free_list(list_t *list) { node_t *curr, *prev; /* start @ beginning of list */ curr = list->head; /* frees nodes 1 @ time */ while(curr) { prev = curr; curr = curr->next; free(prev); } /* frees entire list */ free(list); } /* function checks malloc(), , whether enough space allocated */ void exit_if_null(void *ptr, const char *msg) { if (!ptr) { printf("unexpected null pointer: %s\n", msg); exit(exit_failure); } }
Comments
Post a Comment