List * db_load_products (void) { FILE *fp; char line[LINE_BUFFER_SIZE]; Product *product; List *ret = NULL; fp = fopen (PRODUCTS_DB_FILE, "r"); if (fp == NULL) { perror ("Error opening " PRODUCTS_DB_FILE); return NULL; } while (fgets (line, LINE_BUFFER_SIZE, fp)) { product = get_product_from_line (line); if (product) { ret = list_prepend (ret, product); } } fclose (fp); ret = list_reverse (ret); return ret; }
int main() { List *list = list_create(sizeof(int)); printf("List: "); int data = 1; list_push_back(list, &data); data = 3; list_push_back(list, &data); data = 5; list_push_back(list, &data); data = 0; list_push_back(list, &data); list_print(list, intPrinter); printf("\r\nRevd: "); list_reverse(list); list_print(list, intPrinter); data = -1; list_push_front(list, &data); printf("\r\nFron: "); list_print(list, intPrinter); list_pop_back(list, &data); printf("\r\nPopb: "); list_print(list, intPrinter); list_pop_front(list, &data); printf("\r\nPopf: "); list_print(list, intPrinter); printf("\r\n"); return 0; }
} END_TEST // Ensure a list can be reversed with the expected values // ✔ Data should be ordered as expected START_TEST (test_reverse_list) { kld_list_t * list = (kld_list_t *) new_list(); char * vals[4] = {"foo", "bar", "baz", "buz"}; char * reversed_vals[4] = {"buz", "baz", "bar", "foo"}; int i; for(i = 0; i < 4; i++) { list_append(list, vals[i]); } kld_list_t * reversed_list = (kld_list_t*) list_reverse(list); kld_list_node_t * rev_current = reversed_list->head; int rev_count; for(rev_count = 0; rev_count < 4; rev_count++) { fail_if(strcmp(rev_current->data, reversed_vals[rev_count]) != 0, "Unexpected data"); rev_current = rev_current->next; } } END_TEST
void oqueue_flush(OQueue *q, FILE *fh) { q->data = list_reverse(q->data); list_foreach(q->data, CBFUNC(fputs), fh); list_destroy(q->data, DESTROYFUNC(free)); q->data = NULL; }
int main(int argc, char *argv[]) { int i = 0; struct list *l; struct node *n; char *s = "Hello"; l = list_new(); for (i = 0; i < strlen(s); i++) list_push(l, s + i); n = l->first; while (n) { printf("%c\n", *((char *)n->data)); n = n->next; } list_reverse(l); n = l->first; while (n) { printf("%c\n", *((char *)n->data)); n = n->next; } }
static void fix_arguments_order(CommandNode *node) { List subnodes; if (node == NULL) return; switch (node->type) { case CN_COMMAND: node->arguments = list_reverse(node->arguments); break; case CN_SUBSHELL: subnodes = node->expression; while (subnodes != NULL) { fix_arguments_order(list_head_command(subnodes)); subnodes = subnodes->next; } break; default: break; } }
void get_relative_name_as_list(Term* term, Block* relativeTo, Value* nameOutput) { set_list(nameOutput, 0); // Walk upwards and build the name, stop when we reach relativeTo. // The output list will be reversed but we'll fix that. while (true) { set_value(list_append(nameOutput), unique_name(term)); if (term->owningBlock == relativeTo) { break; } term = parent_term(term); // If term is null, then it wasn't really a child of relativeTo if (term == NULL) { set_null(nameOutput); return; } } // Fix output list list_reverse(nameOutput); }
int main(void) { int i; struct list_node *head; struct list_node *node; init_list(&head); for (i = 1; i < 10; ++i) { node = malloc(sizeof(struct list_node)); node->node_data = i; node->next = NULL; list_append(node, head); } list_print(head); printf("head->node_data = %d\n", head->node_data); list_reverse(head); list_print(head); printf("the last 3th node data:%d\n", list_last(head, 3)->node_data); list_destroy(head); return 0; }
int main() { struct list* test_list = list_create(); char* str1 = "string_1"; list_push_front(test_list, str1); char* str2 = "string_2"; list_push_front(test_list, str2); char* str3 = "string_3"; list_push_front(test_list, str3); list_push_front(test_list, "string_4"); /*list_bubble_sort(test_list, (list_compare)strcmp);*/ list_reverse(test_list); /*list_remove(test_list, test_list->first);*/ list_print(test_list); /*list_clear(test_list);*/ return 0; }
void builtin_fc_list(t_argparser_result *result) { int first_index; int second_index; t_line *save; if (!history_get_first(edit_singleton()->history)) save = NULL; else { save = line_copy(history_get_from_last(edit_singleton()->history, 0)); history_pop_last(edit_singleton()->history); } builtin_fc_list_get_indexes(result, &first_index, &second_index); if (first_index == -1 || second_index == -1) { shenv_singl_error(1, "fc: history specification out of range"); return ; } if (argparser_result_opt_is_set(result, "r")) list_reverse(result, first_index, second_index); else list_normal(result, first_index, second_index); if (save) history_push(edit_singleton()->history, save); }
static CppContext *make_virt_cpp_context(CppContext *ctx, List *tokens) { CppContext *r = make_cpp_context(NULL); r->at_bol = false; r->defs = ctx->defs; r->ungotten = list_reverse(tokens); r->in_macro = true; return r; }
// This is where most of the strict/lazy distinction is. static value_t *e_fncall(env_t *env, expr_t *expr) { eli_closure_t c; binding_t *fn; // Call-by-value (strict function calls): evaluate each argument to // a value in the given environment. c.env = env; c.list = list_empty(); list_iterate(fncall_args(expr), e_expr_list_i, &c); list_reverse(c.list); switch (fncall_fn(expr)->type) { case p_var: // The function is literally the name of a function, and is // defined in the global environment. fn = (binding_t *)env_lookup(global_env, var_name(fncall_fn(expr))); assert(fn != NULL); // We must have exactly as many arguments as parameters. assert(list_length(c.list) == list_length(fn->params)); // Bind the function's parameters to the given arguments in a new // scope derived from the global scope. env = global_env; env_new_scope(&env); list_zip_with(fn->params, c.list, e_bind_params_i, env); // Evaluate the function's body in the new environment. return e_expr(env, fn->body); case p_datacons: { value_t *result; result = alloc_value(v_datacons); datacons_tag(result) = datacons_tag(fncall_fn(expr)); datacons_params(result) = c.list; // FIXME we'd like to assert that we got the right number of // arguments, but we don't know how many the data constructor // wanted. return result; } default: fprintf(stdout, "e_fncall: expression:\n"); pp_expr(stdout, fncall_fn(expr), 2); fprintf(stdout, "\non line %d is not a function-variable or a data constructor.\n", fn->line_num); error(""); return NULL; } }
static void test_list(void) { List *list = make_list(); assert_int(0, list_len(list)); list_push(list, (void *)1); assert_int(1, list_len(list)); list_push(list, (void *)2); assert_int(2, list_len(list)); Iter *iter = list_iter(list); assert_int(1, (long)iter_next(iter)); assert_int(false, iter_end(iter)); assert_int(2, (long)iter_next(iter)); assert_int(true, iter_end(iter)); assert_int(0, (long)iter_next(iter)); assert_int(true, iter_end(iter)); List *copy = list_copy(list); assert_int(2, list_len(copy)); assert_int(1, (long)list_get(copy, 0)); assert_int(2, (long)list_get(copy, 1)); List *rev = list_reverse(list); iter = list_iter(rev); assert_int(2, (long)iter_next(iter)); assert_int(1, (long)iter_next(iter)); assert_int(0, (long)iter_next(iter)); assert_int(2, list_len(rev)); assert_int(1, (long)list_pop(rev)); assert_int(1, list_len(rev)); assert_int(2, (long)list_pop(rev)); assert_int(0, list_len(rev)); assert_int(0, (long)list_pop(rev)); List *list2 = make_list(); list_push(list2, (void *)5); list_push(list2, (void *)6); assert_int(5, (long)list_shift(list2)); assert_int(6, (long)list_shift(list2)); assert_int(0, (long)list_shift(list2)); List *list3 = make_list(); assert_int(0, (long)list_head(list3)); assert_int(0, (long)list_tail(list3)); list_push(list3, (void *)1); assert_int(1, (long)list_head(list3)); assert_int(1, (long)list_tail(list3)); list_push(list3, (void *)2); assert_int(1, (long)list_head(list3)); assert_int(2, (long)list_tail(list3)); List *list4 = make_list(); list_push(list4, (void *)1); list_push(list4, (void *)2); assert_int(1, (long)list_get(list4, 0)); assert_int(2, (long)list_get(list4, 1)); assert_int(0, (long)list_get(list4, 2)); }
/* Read an s-expression from a FILE and buffer lines in a linked-list */ char *read_sexp(FILE *in) { struct list_t *head = NULL; int parens = 0; int quote = 0; char *buf = malloc(sizeof(*buf) * MAX_LINE); if(buf == NULL) return NULL; buf[0] = '\0'; /* while there are valid lines and while the parens are not matched */ char *str; while((str = fgets(buf, MAX_LINE, in)) != NULL) { quote = strip_comments(buf, quote); if(buf[0] == '\n' || buf[0] == '\0') /* skip totally blank lines */ continue; /* break if we've read a matched s-expression */ if((parens += count_parens(buf, MAX_LINE)) == 0) break; head = list_push(buf, head); buf = malloc(sizeof(*buf) * MAX_LINE); if(buf == NULL) { list_for_each(head, &list_free_node); return NULL; } } if(str == NULL) { list_for_each(head, &list_free_node); free(buf); return NULL; } quote = strip_comments(buf, quote); head = list_push(buf, head); head = list_reverse(head); size_t len = 0; struct list_t *n; for(n = head; n != NULL; n = n->next) len += strlen((char *) n->data); char *concat = malloc(sizeof(*concat) * (len+1)); char *i = concat; for(n = head; n != NULL; n = n->next) { len = strlen(n->data); strncpy(i, (char*)n->data, len); i += len; } *i = '\0'; list_for_each(head, &list_free_node); return concat; }
/* Recursively reverse all child lists, since we use prepend() when * constructing the tree. Should be quicker. Done in-place. */ static void tree_reverse_children(XmlNode *root) { const List *iter; if(root == NULL) return; root->children = list_reverse(root->children); for(iter = root->children; iter != NULL; iter = list_next(iter)) tree_reverse_children(list_data(iter)); }
List * xmlnode_iter_begin(const XmlNode *root) { List *flat = NULL; /* Build the flat list, recursively. This is done in reverse. */ iter_traverse(root, &flat); flat = list_reverse(flat); /* Reverse the list before returning it. */ return flat; }
/* 测试用例2 */ void test2 (void) { LIST list; list_init (&list); int i; for (i = 0; i < 10; ++i) list_append (&list, i); list_print (&list); /* 0 1 2 3 ... */ list_reverse (&list); list_print (&list); /* 9 8 7 6 ... */ list_deinit (&list); }
int main(int argc, char **argv) { int i; struct list_t *head = NULL; for(i = 1; i < argc; i++) { head = list_push(argv[i], head); } list_for_each(head, &list_print_node); printf("\n"); head = list_reverse(head); list_for_each(head, &list_print_node); printf("\n"); }
pointer list_append(VM, pointer a, pointer b) { pointer p = b, q; if (!AR_ISNIL(a)) { a = list_reverse(vm, a); while (!AR_ISNIL(a)) { q = cdr(a); cdr(a) = p; p = a; a = q; } } return p; }
static value_t *e_tuple(env_t *env, expr_t *expr) { value_t *result; eli_closure_t c; c.env = env; c.list = list_empty(); list_iterate(tuple_val(expr), thunk_list_i, &c); list_reverse(c.list); result = alloc_value(v_tuple); tuple_val(result) = c.list; return result; }
/* Make a list out of a chain of strings. */ static List *make_token_list(Buffer *strings) { size_t pos = 0; List *list = NULL; while (pos<strings->length) { size_t delta = strlen(strings->c_str+pos); list = list_push(list, char *, strings->c_str + pos); pos += delta+1; } return list_reverse(list); }
static void emit_func_call(Node *node) { SAVE; int opos = stackpos; bool isptr = (node->type == AST_FUNCPTR_CALL); Ctype *ftype = isptr ? node->fptr->ctype->ptr : node->ftype; List *ints = make_list(); List *floats = make_list(); List *rest = make_list(); classify_args(ints, floats, rest, node->args); save_arg_regs(list_len(ints), list_len(floats)); bool padding = stackpos % 16; if (padding) { emit("sub $8, %%rsp"); stackpos += 8; } emit_args(list_reverse(rest)); if (isptr) { emit_expr(node->fptr); push("rax"); } emit_args(ints); emit_args(floats); pop_float_args(list_len(floats)); pop_int_args(list_len(ints)); if (isptr) pop("r11"); if (ftype->hasva) emit("mov $%d, %%eax", list_len(floats)); if (isptr) emit("call *%%r11"); else emit("call %s", node->fname); maybe_booleanize_retval(node->ctype); if (list_len(rest) > 0) { emit("add $%d, %%rsp", list_len(rest) * 8); stackpos -= list_len(rest) * 8; } if (padding) { emit("add $8, %%rsp"); stackpos -= 8; } restore_arg_regs(list_len(ints), list_len(floats)); assert(opos == stackpos); }
static int close_subshell(ParserContext *ctx) { CommandNode *subshell; if (ctx->expr_stack == NULL) return 0; subshell = cmdnode_subshell(list_reverse(ctx->current_expr)); ctx->current_expr = list_push(list_head_list(ctx->expr_stack), subshell); ctx->expr_stack = list_pop(ctx->expr_stack); ctx->current_command = subshell; return 1; }
int main(void) { int i; struct node_t *head = NULL; for (i = 0; i < 10; i++) list_append(&head, &i, sizeof(int)); list_traverse(head, print); head = list_reverse(head); list_traverse(head, print); return 0; }
void test_reverse_list(void) { List *l = list_prepend(NULL, INT_TO_POINTER(1)); l = list_prepend(l, INT_TO_POINTER(2)); l = list_prepend(l, INT_TO_POINTER(3)); l = list_reverse(l); cut_assert_not_null(l); cut_assert_equal_pointer(INT_TO_POINTER(1), l->data); cut_assert_not_null(l->next); cut_assert_equal_pointer(INT_TO_POINTER(2), l->next->data); cut_assert_not_null(l->next->next); cut_assert_equal_pointer(INT_TO_POINTER(3), l->next->next->data); cut_assert_null(l->next->next->next); }
int main(int argc, char* argv[]) { int ret = 0, val = -1; struct Tlist* soustraite = NULL; // On déclare la tête de liste struct Tlist* head = (struct Tlist*)malloc(sizeof(struct Tlist)); // Le dernier element en traitement struct Tlist* last = head; head->nb = 0; head->next = 0; // Tant qu'on entre pas 0 while (val != 0) { // On lit une valeur ret = 0; while (ret != 1) { printf("Entrez des nombres (0 = stop): "); ret = scanf("%d", &val); CLRBUF; } // On ajoute la valeur à la liste last = last->next = (struct Tlist*)malloc(sizeof(struct Tlist)); last->nb = val; last->next = 0; } // On inverse la liste soustraite = list_reverse(head); // On l'affiche printf("Liste inversee: \n"); list_print(soustraite); printf("\n"); return 0; }
static SDSAttInfo *read_attributes(const char *path, int obj_id, int natts) { SDSAttInfo *att, *att_list = NULL; char buf[H4_MAX_NC_NAME + 1]; int32 type, nvalues; int i, status; for (i = 0; i < natts; i++) { memset(buf, 0, sizeof(buf)); status = SDattrinfo(obj_id, i, buf, &type, &nvalues); CHECK_HDF_ERROR(path, status); if (!strncasecmp("coremetadata", buf, 12) || !strncasecmp("structmetadata", buf, 14) || !strncasecmp("archivemetadata", buf, 15) || !strncasecmp("archivedmetadata", buf, 16)) continue; // skip these useless attributes // read attribute data size_t typesize = h4_typesize(type); if (type == DFNT_CHAR8 || type == DFNT_UCHAR8) nvalues++; void *data = xmalloc(typesize * nvalues); status = SDreadattr(obj_id, i, data); CHECK_HDF_ERROR(path, status); if (type == DFNT_CHAR8 || type == DFNT_UCHAR8) ((char *)data)[nvalues - 1] = '\0'; // stick attribute in struct in list att = NEW(SDSAttInfo); att->name = xstrdup(buf); att->type = h4_to_sdstype(type); att->count = (size_t)nvalues; att->bytes = typesize; att->data.v = data; att->next = att_list; att_list = att; } return (SDSAttInfo *)list_reverse((List *)att_list); }
int main(int argc, char* argv[]) { List ls; list_init(&ls); list_add(&ls, 0); list_add(&ls, 1); list_add(&ls, 2); list_add(&ls, 3); list_add(&ls, 4); list_add(&ls, 5); list_add(&ls, 6); list_add(&ls, 7); list_print(&ls); list_reverse(&ls); list_print(&ls); return 0; }
void test_sort(size_t sz) { unsigned i, a; int s1=0, s2=0, x1=0, x2=0; list_t* l; list_node_t* n; printf("test_sort(%d) ... ", sz); l = list_create(); for (i = 0; i < sz; i++) { a = rnd() % 100; if (i % 2) list_append(l, (void*)a); else list_prepend(l, (void*)a); s1 += a; x1 ^= a; } list_sort(list_reverse(l), cf); for (i = 0, n = l->first; n != NULL; i++, n = n->next) { if (n->next) assert(cf(n->data, n->next->data) <= 0); s2 += (unsigned)n->data; x2 ^= (unsigned)n->data; } assert(s1 == s2); assert(x1 == x2); assert(i == l->len); list_destroy(l); printf("ok\n"); }
void Term__trace_dependents(VM* vm) { Term* term = as_term_ref(vm->input(0)); Block* untilBlock = as_block(vm->input(1)); Value* out = vm->output(); set_list(out, 0); std::set<Term*> included; UpwardIterator upwardIterator(term); upwardIterator.stopAt(untilBlock); // Look at starting term, because UpwardIterator doesn't yield starting term. // TODO: Should fix UpwardIterator for (int i=0; i < term->numInputs(); i++) { Term* input = term->input(i); if (input != NULL) { set_term_ref(list_append(out), input); included.insert(input); } } for (; upwardIterator.unfinished(); upwardIterator.advance()) { Term* current = upwardIterator.current(); if (current == term || included.find(current) != included.end()) { for (int i=0; i < current->numInputs(); i++) { Term* input = current->input(i); if (input != NULL) { set_term_ref(list_append(out), input); included.insert(input); } } } } // Order results in the same order as the code. list_reverse(out); }