Ejemplo n.º 1
0
int main(){
	int n=50;
	int q;

	// 1.
	//test addend and removeend
	linkedlist* list=linkedlist_init(sizeof(int));
	for(int i=0;i<n;i++){
		q=i+n;
		linkedlist_addend(list,&q);
	}
	for(int i=0;i<n;i++){
		int* ptr=linkedlist_rmend(list);
		printf("%d ",*ptr);
		free(ptr);
	}
	printf("\n");
	linkedlist_free(list);

	// 2.
	//test the iterators
	list=linkedlist_init(sizeof(int));
	for(int i=0;i<n;i++){
		q=i+n;
		linkedlist_addend(list,&q);
	}
	iterator* iter=linkedlist_iterator(list); //iterate from the start of the list
	while(linkedlist_iteratorhasnext(iter)){
		printf("%d ",*(int*)linkedlist_iteratornext(iter));
	}
	printf("\n");
	linkedlist_freeiter(iter);
	linkedlist_free(list);

	list=linkedlist_init(sizeof(int));
	for(int i=0;i<n;i++){
		q=i+n;
		linkedlist_addend(list,&q);
	}
	iterator* iterend=linkedlist_iteratorend(list); //iterate from the back of the list
	while(linkedlist_iteratorhasprev(iterend)){
		printf("%d ", *(int*)linkedlist_iteratorprev(iterend));
	}
	printf("\n");
	linkedlist_freeiter(iterend);
	linkedlist_free(list);

	//test the getfront and getend
	list=linkedlist_init(sizeof(char*));
	char* string="This is a string";
	char* string2="meow";
	linkedlist_addend(list,&string);
	linkedlist_addend(list,&string2);
	printf("%s %s\n",*(char**)linkedlist_getfront(list),*(char**)linkedlist_getend(list));
	linkedlist_free(list);
}
Ejemplo n.º 2
0
void normalizer_free(normalizer_t* normalizer) {

	linkedlist_free(normalizer->handlers, &coll_nop_unduper, NULL);
	regfree(normalizer->vdfnsn_regex);
	free(normalizer);

}
Ejemplo n.º 3
0
/*
 * prints the bits from the linked list as chars
 * takes chars from stream as needed with extend list
 * until the file reaches EOF.
 * prints the new eof mapping as well
 * fills out the rest of the bits with 0's
 */
void printbits(huffmantree* tree,FILE* stream,FILE* out){
	char** t=table(tree);
	linkedlist* list=treebits(tree,t);
	int counter=0;
	int maxcharlength=(tree->size+1)/2;
	int sum=0;
	int i=0;
	int eof=0;
	while(linkedlist_size(list)){
		//printf("%d\n",linkedlist_size(list));
		if(linkedlist_size(list)<maxcharlength){
			if(maxcharlength>CHAR_BIT) extendlist(list,t,maxcharlength/CHAR_BIT,stream);
			else extendlist(list,t,maxcharlength,stream);
		}
		if(feof(stream)&&!eof){
			extendlistwitheof(list,t);
			eof=1;
		}
		char* frontbit=(char*)linkedlist_rmfront(list);
		sum=sum*2+(*frontbit-'0');
		free(frontbit);
		counter++;
		if (counter%CHAR_BIT==0){
			fprintf(out,"%c",sum);
			sum=0;
		}
	}
	i=0;
	while(counter%CHAR_BIT!=0){
		i++;
		sum=sum*2;
		counter++;
	}
	if(i)fprintf(out,"%c",sum);
	linkedlist_free(list);
	free_table(t);
}
int main()
{
    struct linkedlist *l;
    struct node *n, *before;

    printf("testing list create...");
    if ((l = linkedlist_create()) == NULL)
        goto fail;
    printf("[ok]\n");

    printf("testing node create...");
    if ((n = node_create('a')) == NULL)
        goto fail;
    printf("[ok]\n");

    printf("testing insert of 1st node...");
    linkedlist_insert_before(l, NULL, n);
    if (l->head != n)
        goto fail;
    printf("[ok]\n");

    printf("testing insert at head...");
    n = node_create('c');
    linkedlist_insert_before(l, l->head, n);
    if (l->head != n)
        goto fail;
    printf("[ok]\n");

    printf("testing get node...");
    before = linkedlist_get_node(l, 1);
    if (before->c != 'a')
        goto fail;
    printf("[ok]\n");

    printf("testing insert in middle...");
    n = node_create('b');
    linkedlist_insert_before(l, before, n);
    n = linkedlist_get_node(l, 1);
    if (n->c != 'b')
        goto fail;
    printf("[ok]\n");

    printf("testing count...");
    if (linkedlist_count(l) != 3)
        goto fail;
    printf("[ok]\n");

    printf("testing del...");
    linkedlist_del(l, n);
    printf("[ok]\n");

    printf("testing free...");
    linkedlist_free(l);
    printf("[ok]\n");

    return 0;

fail:
    printf("[failed]\n");
    return -1;
}