int main(int argc, char* argv[]) { int i, j, k; char c; node *head = (node*) malloc(sizeof(node)); // 헤드 노드 생성 head->next = NULL; // 잘못된 주소 할당 방지 위해 next 포인터 0x0 으로 만들어 놓기 -> head 노드가 노드의 끝이라는 것 명시 printf("Main: %p, %p\n", head, head->next); // 헤드 노드 및 next 포인터 프린트 while(1) { printf("Input command(I: insert, A: append, C: Append sequentially, D: delete, P: print, R: print all. E: exit.\n"); scanf(" %c", &c); if(c =='E' || c == 'e') break; switch(c) { case 'I': case 'i': printf("Input number and position (For example, 4 5 measn put number 4 in fifth node)\n"); scanf("%d %d", &i, &j); node_insert(head, i, j); break; case 'A' : case 'a' : printf("Input number (for example, 4 means append number 4)\n"); scanf("%d", &i); node_append(head, i); break; case 'C' : case 'c' : printf("Input number (for example, 4 8 means append number 4 5 6 7 8 in a row)\n"); scanf("%d %d", &i, &j); for(k=i; k<=j; k++) node_append(head, k); break; case 'D' : case 'd' : printf("Input node position to delete (For example, 5 means delete node in postition 5)\n"); scanf("%d", &i); node_remove(head, i); break; case 'P' : case 'p' : printf("Input node position to print(For example, 5 means print number in fifth node)\n"); scanf("%d", &i); node_print(head, i); break; case 'R' : case 'r' : node_print_all(head); break; } } free(head); return 0; }
// add a key value pait to the htable void ht_add( Htable htable, int key, int val, int d){ if( !ht_exists( htable, key, d) ){ //fprintf( stdout, "ADDING A LEGAL VALUE\n"); node_append( &(htable->table)[ ht_mod_hash( htable, key)], key, val, d); } else{ fprintf( stderr, "ADDING A DUPLICATE VALUE\n"); node_append( &(htable->table)[ ht_mod_hash( htable, key)], key, val, d); } }
struct mdoc_node * mdoc_block_alloc(struct mdoc *mdoc, int line, int pos, enum mdoct tok, struct mdoc_arg *args) { struct mdoc_node *p; p = node_alloc(mdoc, line, pos, tok, MDOC_BLOCK); p->args = args; if (p->args) (args->refcnt)++; switch (tok) { case MDOC_Bd: /* FALLTHROUGH */ case MDOC_Bf: /* FALLTHROUGH */ case MDOC_Bl: /* FALLTHROUGH */ case MDOC_En: /* FALLTHROUGH */ case MDOC_Rs: p->norm = mandoc_calloc(1, sizeof(union mdoc_data)); break; default: break; } node_append(mdoc, p); mdoc->next = MDOC_NEXT_CHILD; return(p); }
int mdoc_block_alloc(struct mdoc *m, int line, int pos, enum mdoct tok, struct mdoc_arg *args) { struct mdoc_node *p; p = node_alloc(m, line, pos, tok, MDOC_BLOCK); p->args = args; if (p->args) (args->refcnt)++; switch (tok) { case (MDOC_Bd): /* FALLTHROUGH */ case (MDOC_Bf): /* FALLTHROUGH */ case (MDOC_Bl): /* FALLTHROUGH */ case (MDOC_Rs): p->norm = mandoc_calloc(1, sizeof(union mdoc_data)); break; default: break; } if ( ! node_append(m, p)) return(0); m->next = MDOC_NEXT_CHILD; return(1); }
void mdoc_node_relink(struct mdoc *mdoc, struct mdoc_node *p) { mdoc_node_unlink(mdoc, p); node_append(mdoc, p); }
int main(int argc, char *argv[]) { Node *head; node_create(&head, 3); node_append(head, 2); node_append(head, 4); node_append(head, 5); printf("BEFORE\n"); node_print(head); node_delete_elem(head->next->next); printf("AFTER\n"); node_print(head); return 0; }
void mdoc_tail_alloc(struct mdoc *mdoc, int line, int pos, enum mdoct tok) { struct mdoc_node *p; p = node_alloc(mdoc, line, pos, tok, MDOC_TAIL); node_append(mdoc, p); mdoc->next = MDOC_NEXT_CHILD; }
void mdoc_word_alloc(struct mdoc *mdoc, int line, int pos, const char *p) { struct mdoc_node *n; n = node_alloc(mdoc, line, pos, MDOC_MAX, MDOC_TEXT); n->string = roff_strdup(mdoc->roff, p); node_append(mdoc, n); mdoc->next = MDOC_NEXT_SIBLING; }
struct mdoc_node * mdoc_body_alloc(struct mdoc *mdoc, int line, int pos, enum mdoct tok) { struct mdoc_node *p; p = node_alloc(mdoc, line, pos, tok, MDOC_BODY); node_append(mdoc, p); mdoc->next = MDOC_NEXT_CHILD; return(p); }
void mdoc_addspan(struct mdoc *mdoc, const struct tbl_span *sp) { struct mdoc_node *n; n = node_alloc(mdoc, sp->line, 0, MDOC_MAX, MDOC_TBL); n->span = sp; node_append(mdoc, n); mdoc->next = MDOC_NEXT_SIBLING; }
int mdoc_body_alloc(struct mdoc *m, int line, int pos, enum mdoct tok) { struct mdoc_node *p; p = node_alloc(m, line, pos, tok, MDOC_BODY); if ( ! node_append(m, p)) return(0); m->next = MDOC_NEXT_CHILD; return(1); }
struct mdoc_node * mdoc_head_alloc(struct mdoc *mdoc, int line, int pos, enum mdoct tok) { struct mdoc_node *p; assert(mdoc->first); assert(mdoc->last); p = node_alloc(mdoc, line, pos, tok, MDOC_HEAD); node_append(mdoc, p); mdoc->next = MDOC_NEXT_CHILD; return(p); }
void mdoc_addeqn(struct mdoc *mdoc, const struct eqn *ep) { struct mdoc_node *n; n = node_alloc(mdoc, ep->ln, ep->pos, MDOC_MAX, MDOC_EQN); n->eqn = ep; if (ep->ln > mdoc->last->line) n->flags |= MDOC_LINE; node_append(mdoc, n); mdoc->next = MDOC_NEXT_SIBLING; }
int main(int argc, char *argv[]) { Node *head, *tmp; node_create(&head, 3); node_append(head, 1); node_append(head, 2); node_append(head, 4); node_append(head, 1); node_append(head, 1); node_append(head, 2); printf("BEFORE\n"); tmp = head; while (tmp != NULL) { printf("elem=%d\n", tmp->data); tmp = tmp->next; } node_rm_dup(head); printf("AFTER\n"); tmp = head; while (tmp != NULL) { printf("elem=%d\n", tmp->data); tmp = tmp->next; } node_delete_all(&head); if (head == NULL) { printf("delete succeeded\n"); } return 0; }
void mdoc_endbody_alloc(struct mdoc *mdoc, int line, int pos, enum mdoct tok, struct mdoc_node *body, enum mdoc_endbody end) { struct mdoc_node *p; p = node_alloc(mdoc, line, pos, tok, MDOC_BODY); p->pending = body; p->norm = body->norm; p->end = end; node_append(mdoc, p); mdoc->next = MDOC_NEXT_SIBLING; }
int mdoc_head_alloc(struct mdoc *m, int line, int pos, enum mdoct tok) { struct mdoc_node *p; assert(m->first); assert(m->last); p = node_alloc(m, line, pos, tok, MDOC_HEAD); if ( ! node_append(m, p)) return(0); m->next = MDOC_NEXT_CHILD; return(1); }
int mdoc_endbody_alloc(struct mdoc *m, int line, int pos, enum mdoct tok, struct mdoc_node *body, enum mdoc_endbody end) { struct mdoc_node *p; p = node_alloc(m, line, pos, tok, MDOC_BODY); p->pending = body; p->end = end; if ( ! node_append(m, p)) return(0); m->next = MDOC_NEXT_SIBLING; return(1); }
int mdoc_word_alloc(struct mdoc *m, int line, int pos, const char *p) { struct mdoc_node *n; n = node_alloc(m, line, pos, MDOC_MAX, MDOC_TEXT); n->string = roff_strdup(m->roff, p); if ( ! node_append(m, n)) return(0); m->next = MDOC_NEXT_SIBLING; return(1); }
static int mdoc_span_alloc(struct mdoc *m, const struct tbl_span *sp) { struct mdoc_node *n; /* FIXME: grab from tbl_span. */ n = node_alloc(m, 0, 0, MDOC_MAX, MDOC_TBL); n->span = sp; if ( ! node_append(m, n)) return(0); m->next = MDOC_NEXT_SIBLING; return(1); }
int mdoc_word_alloc(struct mdoc *m, int line, int pos, const char *p) { struct mdoc_node *n; size_t sv, len; len = strlen(p); n = node_alloc(m, line, pos, MDOC_MAX, MDOC_TEXT); n->string = mandoc_malloc(len + 1); sv = strlcpy(n->string, p, len + 1); /* Prohibit truncation. */ assert(sv < len + 1); if ( ! node_append(m, n)) return(0); m->next = MDOC_NEXT_SIBLING; return(1); }
void mdoc_elem_alloc(struct mdoc *mdoc, int line, int pos, enum mdoct tok, struct mdoc_arg *args) { struct mdoc_node *p; p = node_alloc(mdoc, line, pos, tok, MDOC_ELEM); p->args = args; if (p->args) (args->refcnt)++; switch (tok) { case MDOC_An: p->norm = mandoc_calloc(1, sizeof(union mdoc_data)); break; default: break; } node_append(mdoc, p); mdoc->next = MDOC_NEXT_CHILD; }
int mdoc_addspan(struct mdoc *m, const struct tbl_span *sp) { struct mdoc_node *n; assert( ! (MDOC_HALT & m->flags)); /* No text before an initial macro. */ if (SEC_NONE == m->lastnamed) { mdoc_pmsg(m, sp->line, 0, MANDOCERR_NOTEXT); return(1); } n = node_alloc(m, sp->line, 0, MDOC_MAX, MDOC_TBL); n->span = sp; if ( ! node_append(m, n)) return(0); m->next = MDOC_NEXT_SIBLING; return(1); }
int mdoc_addeqn(struct mdoc *m, const struct eqn *ep) { struct mdoc_node *n; assert( ! (MDOC_HALT & m->flags)); /* No text before an initial macro. */ if (SEC_NONE == m->lastnamed) { mdoc_pmsg(m, ep->ln, ep->pos, MANDOCERR_NOTEXT); return(1); } n = node_alloc(m, ep->ln, ep->pos, MDOC_MAX, MDOC_EQN); n->eqn = ep; if ( ! node_append(m, n)) return(0); m->next = MDOC_NEXT_SIBLING; return(1); }
node_t* node_statement_append_statement(node_t* first_statement, node_t* second_statement) { if ( first_statement == NULL ) return second_statement; return node_append(first_statement, second_statement); }
event_ref new_timer_event(uint8_t timer, uint16_t period, uint8_t repeat, uint16_t repeat_count) { event_ref e = 0; action_ref check_action = 0; node_ref period_arg = 0; node_ref trigger_arg = 0; time_ref time_trigger = 0; time_ref time_period = 0; for(;;) { // Create a new time period time_period = new_time(); if(!time_period) break; // Set up the period if(timer == CLOCK_TIME) { time_period->clock_time = period; } else if (timer == MILLISECONDS) { time_period->milliseconds = period; } else if (timer == SECONDS) { time_period->seconds = period; } else if (timer == MINUTES) { time_period->minutes = period; } else if (timer == HOURS) { time_period->hours = period; } else if (timer == DAYS) { time_period->days = period; } // Create a new time trigger time_trigger = new_time(); if(!time_trigger) break; // Set up the trigger = global + period add_time_to_time(time_trigger, global_time()); add_time_to_time(time_trigger, time_period); // Create a new event e = new_event(); if(!e) break; // Create a new action check_action = new_action(); if(!check_action) break; // Set its function action_set_func(check_action, &check_time_args); // Create a trigger arg trigger_arg = new_node(time_trigger, &time_delete); if(!trigger_arg) break; // Create the period arg period_arg = new_node(time_period, &time_delete); if(!period_arg) break; node_append(period_arg, trigger_arg); action_set_args(check_action, period_arg); event_set_check(e, check_action); event_set_repeat(e, repeat, repeat_count); return e; } event_delete(&e); node_delete(&period_arg); node_delete(&trigger_arg); action_delete(&check_action); time_delete(&time_period); time_delete(&time_trigger); return FAILURE; }
void test() { test_code("VERSION", node_str(VERSION)); test_code("PLATFORM", node_str(PLATFORM)); test_code("ARCH", node_str(ARCH)); test_code("COPYRIGHT", node_str(COPYRIGHT)); test_code("\'()", node_empty_list()); test_code("(quote)", node_empty_list()); test_code("(+ 1 2)", node_num(3)); test_code("(- 9 5)", node_num(4)); test_code("(* 1 5)", node_num(5)); test_code("(/ 12 2)", node_num(6)); test_code("(% 7 8)", node_num(7)); test_code("(+ 1.5 1.5)", node_num(3)); test_code("1e+300", node_num(1e+300)); test_code("(string 1e+300)", node_str("1e+300")); test_code("(** 2 3 2)", node_num(64)); test_code("(sqrt 4)", node_num(2)); test_code("(sin (/ pi 2))", node_num(1)); test_code("(cos pi)", node_num(-1)); test_code("(ln e)", node_num(1)); test_code("(log 100)", node_num(2)); test_code("(not true)", node_nil()); test_code("(not nil)", node_true()); test_code("(and true true)", node_true()); test_code("(and true nil)", node_nil()); test_code("(and true true true nil)", node_nil()); test_code("(and nil true)", node_nil()); test_code("(and nil nil)", node_nil()); test_code("(or true nil)", node_true()); test_code("(or true true)", node_true()); test_code("(or nil true)", node_true()); test_code("(or nil nil)", node_nil()); test_code("(or nil nil nil true)", node_true()); node *l=node_empty_list(); l=node_append(l, node_num(1)); l=node_append(l, node_num(2)); l=node_append(l, node_num(3)); test_code("(push \'(2 3) 1)", l); test_code("(push (quote 2 3) 1)", l); test_code("(push \"at\" \"c\")", node_str("cat")); test_code("(set \'a \'(2 3)) (push a 1) a", l); test_code("(set \'b (pop a)) b", node_num(1)); l=node_empty_list(); l=node_append(l, node_num(2)); l=node_append(l, node_num(3)); test_code("a", l); test_code("(exists? x)", node_nil()); test_code("(set \'x 5) (exists? x)", node_true()); test_code("(string nil)", node_str("nil")); test_code("(string 5)", node_str("5")); test_code("(string 3.14)", node_str("3.14")); test_code("(string \'(1 2 3))", node_str("(1 2 3)")); test_code("(string \"hi\")", node_str("hi")); test_code("(chomp \"Herb\\n\")", node_str("Herb")); test_code("(chomp \"Herb\\r\")", node_str("Herb")); test_code("(chomp \"Herb\\r\\n\")", node_str("Herb")); test_code("(chomp \"Herb\")", node_str("Herb")); test_code("(chop \"Herb\\n\")", node_str("Herb")); test_code("(chop \"Herb\\r\")", node_str("Herb")); test_code("(chop \"Herb\")", node_str("Her")); test_code("(atom? 1)", node_true()); test_code("(length 1)", node_num(0)); test_code("(compare 1 2)", node_num(-1)); test_code("(compare nil nil)", node_num(0)); test_code("(compare \'(1 2) \'(1 2))", node_num(0)); test_code("(< -1 0)", node_true()); test_code("(< 1 2)", node_true()); test_code("(> 2 1)", node_true()); test_code("(< 2 1)", node_nil()); test_code("(<= 1 1)", node_true()); test_code("(>= 2 2)", node_true()); test_code("(= 1 1)", node_true()); test_code("(= 1 2)", node_nil()); test_code("(= (copy \'(1 2 3)) \'(1 2 3))", node_true()); test_code("(= nil true)", node_nil()); test_code("(!= nil true)", node_true()); test_code("(= (copy +) +)", node_true()); l=node_empty_list(); l=node_append(l, node_num(1)); l=node_append(l, node_num(2)); l=node_append(l, node_num(3)); test_code("(append \'(1 2) 3)", l); test_code("(prepend \'(2 3) 1)", l); test_code("(eval)", node_nil()); test_code("(eval 1)", node_num(1)); test_code("(eval \'(1 2 3))", node_nil()); test_code("(eval \'(+ 1 1))", node_num(2)); test_code("(eval \'(eval \'(+ 1 1)))", node_num(2)); node *calculation=node_empty_list(); calculation=node_append(calculation, node_id("+")); calculation=node_append(calculation, node_num(1)); calculation=node_append(calculation, node_num(1)); test_code("\'(+ 1 1)", calculation); test_code("(cat \'(1) \'(2) \'(3))", l); test_code("(cat \'() \'(1 2) \'(3))", l); test_code("(cat \'(1 2) \'(3) \'())", l); test_code("(cat \"c\" \"a\" \"t\")", node_str("cat")); test_code("(cat \"\" \"ca\" \"t\")", node_str("cat")); test_code("(cat \"ca\" \"t\" \"\")", node_str("cat")); test_code("(length \"hi\")", node_num(2)); l=node_empty_list(); l=node_append(l, node_num(1)); l=node_append(l, node_num(2)); test_code("\'(1 2)", l); test_code("(atom? \'(1 2))", node_nil()); test_code("(length \'(1 2))", node_num(2)); test_code("(empty? nil)", node_true()); test_code("(empty? \'())", node_true()); test_code("(empty? \'(1))", node_nil()); test_code("(first \'(1 2 3))", node_num(1)); test_code("(first \"abc\")", node_str("a")); test_code("(number \"-1\")", node_num(-1)); test_code("(set \'a 1) (+ a 1)", node_num(2)); test_code("z", node_nil()); test_code("(needs-closing-paren", node_nil()); test_code("(- (* 2 2 2 2 2 2 2 2 2 2) 1022)", node_num(2)); test_code("(> pi 3)", node_true()); test_code("(= pi 3)", node_nil()); test_code("(< e 3)", node_true()); test_code("(= e 3)", node_nil()); test_code("(> (time) 0)", node_true()); l=node_empty_list(); l=node_append(l, node_nil()); l=node_append(l, node_true()); test_code("\'(nil true)", l); test_code("(set \'a \'(1 2 3)) (first a)", node_num(1)); l=node_empty_list(); l=node_append(l, node_num(2)); l=node_append(l, node_num(3)); test_code("(set \'a \'(1 2 3)) (rest a)", l); test_code("(set \'a \'(1 2 3)) (last a)", node_num(3)); test_code("(list? a)", node_true()); test_code("(atom? a)", node_nil()); test_code("(set \'3 2)", node_nil()); test_code("(* 3 3)", node_num(9)); test_code("(index \"robert\" \"bert\")", node_num(2)); test_code("(index \'(1 2 3) 1)", node_num(0)); test_code("(index \'(1 2 3) 4)", node_num(-1)); test_code("(index \"a b c\" \"\")", node_num(-1)); test_code("(in? \'(1 2 3) 1)", node_true()); test_code("(in? \'(1 2 3) 4)", node_nil()); test_code("(at \'(1 2 3) 0)", node_num(1)); test_code("(at \'(1 2 3) 1)", node_num(2)); test_code("(at \'(1 2 3) 2)", node_num(3)); test_code("(at \'(1 2 3) 3)", node_nil()); test_code("(count \'(1 2 3) 1)", node_num(1)); test_code("(count \'(1 1 1) 1)", node_num(3)); test_code("(count \'(1 2 3) 4)", node_num(0)); test_code("(count \'() 1)", node_num(0)); l=node_empty_list(); l=node_append(l, node_str("a")); l=node_append(l, node_str("b")); l=node_append(l, node_str("c")); test_code("(split \"a b c\" \" \")", l); l=node_empty_list(); l=node_append(l, node_str("a b c")); test_code("(split \"a b c\" \"!\")", l); test_code("(split \"a b c\" \"\")", l); test_code("(join \'(\"a\" \"b\" \"c\") \" \")", node_str("a b c")); test_code("(join \'(\"a\" \"b\" \"c\") \", \")", node_str("a, b, c")); test_code("(join \'(\"a\" \"b\" \"c\") \"\")", node_str("abc")); test_code("(join \'(\"a\") \" \")", node_str("a")); test_code("(join \'() \" \")", node_str("")); node *k=node_empty_list(); k=node_append(k, node_str("a")); k=node_append(k, node_str("b")); k=node_append(k, node_str("c")); node *v=node_empty_list(); v=node_append(v, node_num(1)); v=node_append(v, node_num(2)); v=node_append(v, node_num(3)); node *h=node_hash(k, v); test_code("(set \'a (hash \'(\"a\" \"b\" \"c\") \'(1 2 3)))", h); test_code("(hash? a)", node_true()); test_code("(hash? \'(1 2))", node_nil()); test_code("(hash-keys a)", k); test_code("(hash-values a)", v); test_code("(set \'a (hash \'() \'())) (hash-set a \"guy\" \"robert\") (hash-get a \"guy\")", node_str("robert")); test_code("(hash? (env))", node_true()); test_code("(env \"COOLNAME\" \"Bobby\")", node_str("Bobby")); test_code("(env \"COOLNAME\")", node_str("Bobby")); test_code("(env \"COOLNAME\" \"red=blue\")", node_str("red=blue")); test_code("(env \"COOLNAME\")", node_str("red=blue")); test_code("(hash? (env))", node_true()); test_code("(if (< 1 2) 1 2)", node_num(1)); test_code("(if (< 1 2) 1)", node_num(1)); test_code("(if (> 1 2) 1 2)", node_num(2)); test_code("(if (> 1 2) 1)", node_nil()); test_code("(unless (< 1 2) 1 2)", node_num(2)); test_code("(unless (< 1 2) 1)", node_nil()); test_code("(unless (> 1 2) 1 2)", node_num(1)); test_code("(unless (> 1 2) 1)", node_num(1)); test_code("(set \'i 0)", node_num(0)); test_code("(while (< i 5) (set \'i (+ i 1)))", node_num(5)); test_code("(for (j 0 5) j)", node_num(4)); test_code("(block (+ 2 2) (- 4 1) (* 1 2) (/ 5 5))", node_num(1)); test_code("(let (\'y 3) (+ y 2))", node_num(5)); test_code("y", node_nil()); test_code("(set \'f (lambda (x) (+ x 1))) (f 4)", node_num(5)); node *vars=node_empty_list(); vars=node_append(vars, node_id("x")); vars=node_append(vars, node_id("y")); node *exps=node_empty_list(); exps=node_append(exps, node_id("*")); exps=node_append(exps, node_id("x")); exps=node_append(exps, node_id("y")); node *temp=node_empty_list(); temp=node_append(temp, exps); node *lambda=node_lambda(vars, temp); test_code("(lambda (x y) (* x y))", lambda); test_code("((lambda (x y) (* x y)) 2 3)", node_num(6)); test_code("(set \'multiply (lambda (x y) (* x y))) (multiply 5)", node_nil()); test_code("(atom? multiply)", node_true()); test_code("(set \'a (lambda () 5)) (a)", node_num(5)); test_code("(set \'a (lambda (x y) (+ x y) (- x y) (* x y) (/ x y))) (a 1 2)", node_num(0.5)); vars=node_empty_list(); vars=node_append(vars, node_id("x")); exps=node_empty_list(); exps=node_append(exps, node_id("*")); exps=node_append(exps, node_id("x")); exps=node_append(exps, node_id("x")); temp=node_empty_list(); temp=node_append(temp, exps); node *def=node_lambda(vars, temp); test_code("(def (square x) (* x x))", def); test_code("(square)", node_nil()); test_code("(square 4)", node_num(16)); test_code("(set \'plus +)", node_builtin("+", node_add)); test_code("(set \'+ -)", node_builtin("-", node_sub)); test_code("(+ 1 1)", node_num(0)); test_code("(set '+ plus)", node_builtin("+", node_add)); test_code("(+ 1 1)", node_num(2)); if (FAILURES<1) printf("ALL %d TESTS PASS\n", SUCCESSES); else printf("%d TESTS FAIL\n", FAILURES); }