Пример #1
0
void test(int n) {
    struct ListNode* head = NULL;
    struct ListNode* prev = NULL;
    for (int i = 0; i < n; ++i) {
        struct ListNode* node = (struct ListNode*)malloc(sizeof(struct ListNode));
        node->val = i;
        node->next = NULL;
        if (prev) {
            prev->next = node;
        } else {
            head = node;
        }
        prev = node;
    }

    printf(" original list is:");
    list_print(head);

    reorderList(head);

    printf("reordered list is:");
    list_print(head);

    printf("\n");
}
Пример #2
0
void
test_communicate() {
    int valuebuf[] = { 1 };
    size_t i;
    ListObject *lp = list_new();
    for(i = 0; i < 10; i++) {
        *valuebuf = i;
        list_add(lp, valuebuf);
    }
    ListObject *lp2 = list_new();
    for(i = 5; i < 15; i++) {
        *valuebuf = i;
        list_add(lp2, valuebuf);
    }
    printf("list1 is:\n");
    list_print(lp);
    printf("list2 is:\n");
    list_print(lp2);
    SetObject *sp = set_fromlist(lp, int_hash, lp->keycmp, lp->keydup, 0);
    printf("set from  list1 is:\n");
    set_print_int(sp);
    SetObject *sp2 = set_fromlist(lp2, int_hash, lp2->keycmp, lp2->keydup, 0);
    printf("set from  list2 is:\n");
    set_print_int(sp2);
    printf("so, intersection of the two list is\n");
    set_print_int(set_and(sp, sp2));
    list_free(lp);
    list_free(lp2);
    set_free(sp);
    set_free(sp2);
}
Пример #3
0
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;
}
Пример #4
0
int main(void)
{
    struct node head_a;
    link a;
    int i;

    void list_print(link);
    link insertion_sort(link);


    srand((unsigned)time(NULL));
    a = &head_a;

    for (i=0; i<N; i++)
    {
        a->next = (link)malloc(sizeof(head_a));
        a = a->next;
        a->data = rand() % 60;
        a->next = NULL;
    }

    list_print(head_a.next);

    a = insertion_sort(head_a.next);

    list_print(a);
    return 0;
}
Пример #5
0
LIST *
compile_set(
	PARSE	*parse,
	LOL	*args,
	int	*jmp )
{
	LIST	*nt = (*parse->left->func)( parse->left, args, jmp );
	LIST	*ns = (*parse->right->func)( parse->right, args, jmp );
	LISTITEM	*l;

	if( DEBUG_COMPILE )
	{
	    debug_compile( 0, "set" );
	    list_print( nt );
	    printf( " %s ", set_names[ parse->num ] );
	    list_print( ns );
	    printf( "\n" );
	}

	/* Call var_set to set variable */
	/* var_set keeps ns, so need to copy it */

	for( l = list_first(nt); l; l = list_next( l ) )
	    var_set( list_value(l), list_copy( L0, ns ), parse->num );

	list_free( nt );

	return ns;
}
Пример #6
0
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;
}
Пример #7
0
int main()
{
  list_init(&list_head);

  list_add(list_head, 0);
  list_add(list_head, -10);
  list_add(list_head, 10);

  assert(list_contain(list_head, 10));
  assert(list_contain(list_head, 0));
  assert(list_contain(list_head, -10));
  
  list_print(list_head);

  list_remove(list_head, 0);
  assert(!list_contain(list_head, 0));
  list_print(list_head);
  

  list_remove(list_head, 10);
  assert(!list_contain(list_head, 10));
  list_print(list_head);

  list_remove(list_head, -10);
  assert(!list_contain(list_head, -10));
  list_print(list_head);

  assert(list_contain(list_head, 0));

  return 0;
}
Пример #8
0
void test_insert_duplicate_copy(void)
{   
     List list = list_random(MAX_LIST_SIZE, ELEMENT_MAX_VALUE);
     printf("Random List ::: ");
     list_print(&list);

     List duplicate = list_duplicate(&list);
     printf("%sDuplicate List ::: ", cyan);
     list_print(&duplicate);

     uint low = rand() % list.size;
     uint high = (rand() % (list.size - low)) + low;
     List copy = list_copy_range(&list, low, high);
     printf("%sCopy List Range %d-%d::: ", red, low, high);
     list_print(&copy);
     
     Item random_item;
     random_item.item = rand() % ELEMENT_MAX_VALUE;
     random_item.freq = 1;
     uint random_position = rand() % list.size;
     list_insert(&list, random_item, random_position);
     printf("%sRandom List with %d inserted in %d ::: ", green, random_item.item, random_position);
     list_print(&list);
     printf("%s",none);
}
Пример #9
0
int main() {
        struct list *a = int_to_list(321);
        struct list *b = int_to_list(45);
        list_print(a);
        list_print(b);
        list_print(add_lists(a, b));
        return 0;
}
Пример #10
0
void entite_print_complex(Entite* entite){
	entite_print_simple(entite);
	printf("Les messages transmis par cette entite sont : \n");
	printf("Sur l'anneau 1 : \n");
	list_print(entite->mssgTransmisAnneau1);
	printf("Sur l'anneau 2 : \n");
	list_print(entite->mssgTransmisAnneau2);
	printf("\n");
}
Пример #11
0
int main( int argc, char * argv[])
{
	lnode * pnode;
	FILE * fp;
	char * line = NULL;
    	size_t len = 0;
    	ssize_t read;

#if 0
		pnode = (lnode *) malloc (sizeof(lnode));	
		if( pnode )
		{
			pnode->pline = "aabb";
			pnode->next = NULL;
		}

		list_insert( pnode );
		list_print();
	
	
		pnode = (lnode *) malloc (sizeof(lnode));	
		if( pnode )
		{
			pnode->pline = "ccdd";
			pnode->next = NULL;
		}

		list_insert( pnode );
		list_print();

		return 0;
#endif
	fp = fopen(argv[1], "r");
	if (fp == NULL)
		exit(EXIT_FAILURE);

	while ((read = getline(&line, &len, fp)) != -1) 
	{
		printf("Retrieved line of length %zu :\n", read);
		printf("%s", line);

		pnode = (lnode *) malloc (sizeof(lnode));	
		if( pnode )
		{
			pnode->pline = (char *) malloc ( strlen(line) );
			strcpy( pnode->pline, line );
//			pnode->pline = line;
			pnode->next = NULL;
		}

		list_insert( pnode );
		list_print();
	}

	return 0;
}
Пример #12
0
void descripe_command(cmdEntry* cmds, char* name, char* from)
{
    int i;
    int found = 0;
    for( i = 0; i < anz_commands; i++)
    {
        if( 0 == strcmp(cmds[i]->name, name))
        {
 						char temp[1024];
						if( from == NULL)
						{
	            printf("%s\n", cmds[i]->desc);
	            printf("%s  ",cmds[i]->name);
						}
						else
						{
							sprintf(temp, "%s\n", cmds[i]->desc);
							sprintf(temp, "%s  ", cmds[i]->name);
						}
            /* print all parameters with description of them*/
            if( cmds[i]->para != NULL)
            {
								to = from;
                singlePrint = true;
                list_print(cmds[i]->para);
                singlePrint = false;
                list_print(cmds[i]->para);
            }
            else
						{
								if( from == NULL)
	                printf("\n");
								else
									sprintf(temp, "\n");
						}

						if( from == NULL)
							send_msg(from, temp);
            found = 1;
            break;
        }
    }
    if( found == 0)
    {
				if( from == NULL)
					printf( "Don't know command \"%s\"!\ntype \"help\" to see the available commands\n", name);
				else
				{
					char temp[512];
					sprintf(temp,  "Don't know command \"%s\"!\ntype \"help\" to see the available commands\n", name);
					send_msg(from, temp);
				}
    }
}
Пример #13
0
Файл: lsex.c Проект: zxwbj/danei
/* 测试用例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);
}
Пример #14
0
int main() {
    Node *p = nullptr;
    list_appends(&p, {1, 2, 3, 4});
    list_appends(&p, {5, 6, 7});
    list_print(p);
    list_removeIf(&p, [](int i){return i%2;});
    list_print(p);
    list_removeIf_r(p, [](int i){return i%3;});
    list_print(p);

    puts("finish");
}
Пример #15
0
void test_list(void) {
    int i, arr[MAX];
    int k, s;
    list *l = EMPTY_LIST;

    generate(arr, MAX);

    for (i = 0; i < MAX; i++) {
        l = list_create(arr[i], 0);

        assert(l != EMPTY_LIST);
        assert(l->key == arr[i]);
        assert(list_count(l) == 1);
        assert(list_contains(l, arr[i]));

        l = list_destroy(l);

        assert(l == EMPTY_LIST);
    }
    for (i = 0; i < MAX; i++) {
        l = list_insert(l, arr[i], arr[i]);
        assert(list_contains(l, arr[i]));
    }
    assert(list_ordered(l));
    for (i = 0; i < MAX; i++) {
        l = list_remove_first(l, &k, &s);
        assert(list_ordered(l));
    }
    l = list_destroy(l);

    l = list_insert(l, 5, 0);
    l = list_insert(l, 4, 1);
    l = list_insert(l, 1, 2);
    l = list_insert(l, 2, 6);
    l = list_insert(l, 7, 4);
    l = list_insert(l, 0, 5);
    list_print(l);

    l = list_insert(l, 20, 10);
    l = list_insert(l, 25, -1);
    l = list_insert(l, 30, 3);
    list_print(l);

    l = list_remove_first(l, &k, &s);
    s = 15;
    l = list_insert(l, k, s);
    list_print(l);

    printf("count: %d\n", list_count(l));
    printf("last: %d\n", list_last(l)->key);

    l = list_destroy(l);
}
Пример #16
0
main()

{
	DynamicList* l = list_inicialize();

	/*Aluno* Al1 = (Aluno*)malloc(sizeof(Aluno));
	Aluno* Al2 = (Aluno*)malloc(sizeof(Aluno));

	Al1->RA = 1516;
	Al1->nome = "JOAO";

	Al2->RA = 2324;
	Al2->nome = "SIBA";
	*/

	printf("\nInseriu? %s", list_add_last(l, 'a')?"sim":"não");
	printf("\nInseriu? %s", list_add_last(l, 'b')?"sim":"não");

	printf("\nFirst %c\n", list_get_first(l));
	printf("\nLast %c\n", list_get_last(l));

	printf("\nInseriu? %s", list_add_last(l, 'c')?"sim":"não");
	printf("\nInseriu? %s", list_add_last(l, 'd')?"sim":"não");
	printf("\nInseriu? %s", list_add_first(l, 'e')?"sim":"não");
	printf("\nInseriu? %s", list_add_first(l, 'f')?"sim":"não");
	printf("\nInseriu? %s", list_add_first(l, 'g')?"sim":"não");

	printf("\nFirst %c\n", list_get_first(l));
	printf("\nLast %c\n", list_get_last(l));

	list_print(l);

	printf("\nSize %d\n", list_size(l));

	list_clear(l);

	printf("\nSize %d\n", list_size(l));

	printf("\nInseriu? %s", list_add_last(l, 'c')?"sim":"não");
	printf("\nInseriu? %s", list_add_last(l, 'd')?"sim":"não");
	printf("\nInseriu? %s", list_add_first(l, 'e')?"sim":"não");
	printf("\nInseriu? %s", list_add_first(l, 'f')?"sim":"não");
	printf("\nInseriu? %s", list_add_first(l, 'g')?"sim":"não");

	list_print(l);

	list_remove_first(l);	
	list_remove_first(l);	
	list_remove_first(l);	
	
	list_print(l);
}
Пример #17
0
int main() {

list_t * test = malloc (sizeof(list_t));
list_init(test);
list_add(test, "one");
list_add(test, "two");
list_add(test, "three");
list_print(test);
list_remove(test, "two");
list_print(test);
list_clear(test);
return 1;
}
Пример #18
0
int main() {
        struct list *l = list_new();
        list_add(l, 3);
        list_add(l, 5);
        list_add(l, 2);
        list_add(l, 6);
        list_add(l, 7);
        list_add(l, 2);
        list_add(l, 4);
        list_print(l);
        partition(l, 4);
        list_print(l);
        list_free(l);
        return 0;
}
Пример #19
0
LIST *
compile_switch(
	PARSE	*parse,
	LOL	*args,
	int	*jmp )
{
	LIST	*nt = (*parse->left->func)( parse->left, args, jmp );
	LIST	*result = 0;

	if( DEBUG_COMPILE )
	{
	    debug_compile( 0, "switch" );
	    list_print( nt );
	    printf( "\n" );
	}

	/* Step through cases */

	for( parse = parse->right; parse; parse = parse->right )
	{
	    if( !glob( parse->left->string, list_first(nt) ? list_value(list_first(nt)) : "" ) )
	    {
		/* Get & exec parse tree for this case */
		parse = parse->left->left;
		result = (*parse->func)( parse, args, jmp );
		break;
	    }
	}

	list_free( nt );

	return result;
}
Пример #20
0
void task1c() {
    link *list = NULL;
    link *new_list = NULL;
    int res = 0;
     
    list = list_append(list, 1);
    list = list_append(list, 3);
    list = list_append(list, 8);
      
    new_list = map(list,odd);
    res = reduce(new_list, max, 0);
     
    printf("The maximal odd number in the list is: %d\n", res);
    printf("new_list content is:\n");
    list_print(new_list);
         
    list_free(list);
    list_free(new_list);

    /*
    Will print:

        The maximal odd number in the list is: 3
        new_list content is:
        1
        3
        0
    */
}
Пример #21
0
Файл: dedup.c Проект: dzeban/cs
void dedup(struct list *head)
{
	struct list *cur, **pp;
	struct list *del;
	
	cur = head;
	while (cur)
	{
		pp = &(cur->next);
		while (*pp)
		{
			if (cur->n == (*pp)->n)
			{
				del = *pp;
				*pp = (*pp)->next;
				free(del);
			}
			else
			{
				pp = &((*pp)->next);
			}
			list_print(head);
		}
		cur = cur->next;
	}
}
Пример #22
0
void list_info ()
{
	printf("\n___INFO___\nList: ");
	list_print(mainlist);
	printf("\n\nList length: %d\n", list_length(mainlist));
	return;
}
Пример #23
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;
}
Пример #24
0
int main()
{
    list_t *list = list_new();
    dynamic_t * dll = dynamic_init(chooseLib ());
    if (NULL == dll) {
        printf("Can't load dynamic!\n");
        return 1;
    }
    if (NULL == dll->chk) {
        printf("Can't get check function!\n");
        return 1;
    }
    if (NULL == dll->react) {
        printf("Can't get reaction function!\n");
        return 1;
    }
    printf("Dynamic loaded!\n");
    srand(time(NULL));

    int i = 0;
    for (i = 0; i < 10; i++){
        list_add(list, rand() % 125);
    }
    list_print(list);
    dll->react(dll->chk(list));

    list_free(list);
    dynamic_clean(dll);
    return 0;
}
Пример #25
0
static void *
_routine_insert1(void *arg)
{
  thread_t *thread = (thread_t *) arg;
  list_t   *l = (list_t *) thread_arg(thread);
  long      i;
  kerrno_t  ret;
  kitem_t   result;
  long      t[] = {16, 14, 10, 12, 8, 6, 4, 2};

  for (i = 0; i < sizeof(t)/sizeof(long); ++i) {
    ret = list_insert(l, thread, (kitem_t) t[i]);
    if (ret!=KSUCCESS && ret!=KEEXIST)
      kerror("insert error: %ld[%d]\n", t[i], ret);

    result = list_find(l, thread, (kitem_t) t[i]);
    if ((long) result != t[i])
      kerror("find %ld error\n", t[i]);

    list_delete(l, thread, (kitem_t) t[i]);

    result = list_find(l, thread, (kitem_t) t[i]);
    if ((long) result != (long) KITEM_LONG_NULL)
      kerror("delete %ld[%ld] error\n", t[i], (long) result);
  }

  list_print(l, thread);
  return NULL;
}
Пример #26
0
void main(int argc, char* argv[])
{
	pstud phead,ptail,phead1;
	int i;
	phead=NULL;
	ptail=NULL;
	while(scanf("%d",&i) != EOF)
	{
		list_tail_insert(&phead,&ptail,i);
	}
	list_print(phead);
	phead1=phead->pnext;   
	list_split(phead,phead1);
	list_print(phead);
	list_print(phead1);
}
Пример #27
0
LIST * compile_switch( PARSE * parse, FRAME * frame )
{
    LIST * nt = parse_evaluate( parse->left, frame );
    LIST * result = 0;

    if ( DEBUG_COMPILE )
    {
        debug_compile( 0, "switch", frame );
        list_print( nt );
        printf( "\n" );
    }

    /* Step through cases. */
    for ( parse = parse->right; parse; parse = parse->right )
    {
        if ( !glob( parse->left->string, nt ? nt->string : "" ) )
        {
            /* Get & exec parse tree for this case. */
            parse = parse->left->left;
            result = parse_evaluate( parse, frame );
            break;
        }
    }

    list_free( nt );
    return result;
}
Пример #28
0
Файл: task2.c Проект: liranr23/C
int choose_action(cmdLine *pCmdLine, linked_list *head, var_list *var_head){
  if(strcmp(pCmdLine->arguments[0], "quit") == 0){
    return 1;
  }
  else if(strcmp(pCmdLine->arguments[0], "cd") == 0){
    if((chdir(pCmdLine->arguments[1])) < 0){
      perror("Error changing a dir: ");
    }
    return 0;
  }
  else if(strcmp(pCmdLine->arguments[0], "history") == 0){
    if(head == 0){
      perror("Error-no history");
      return 0;
    }
    list_print(head);
    return 0;
  }
  else if(strcmp(pCmdLine->arguments[0], "env") == 0){
    print_var(var_head);
    return 0;
  }
  else if(strcmp(pCmdLine->arguments[0], "rename") == 0){
    rename_var(var_head, pCmdLine->arguments[1], pCmdLine->arguments[2]);
    return 0;
  }
  else{
    execute(pCmdLine);
    return 0;
  }
}
Пример #29
0
void list_print(list *l) {
    if (!l) printf("null\n");
    else {
        printf("%d {%d} -> ", l->key, l->score);
        list_print(l->tail);
    }
}
Пример #30
0
LIST *
compile_on(
	PARSE	*parse,
	FRAME	*frame )
{
	LIST    *nt = parse_evaluate( parse->left, frame );
	LIST	*result = 0;

	if( DEBUG_COMPILE )
	{
	    debug_compile( 0, "on", frame );
	    list_print( nt );
	    printf( "\n" );
	}

	if( nt )
	{
	    TARGET *t = bindtarget( nt->string );
	    pushsettings( t->settings );

	    result = parse_evaluate( parse->right, frame );

	    popsettings( t->settings );
	}

	list_free( nt );

	return result;
}