Example #1
0
void test_dlist_remove_data_from_existing_until_empty(void)
{
    unsigned long *val;
    unsigned long i;

    val = NULL;
    i = dlist_size(test_dlist);
    while(i > 0) {
        val = dlist_index(test_dlist, --i);
        assert_true(val != NULL);
        assert_true(dlist_remove_data(test_dlist, val) == 0);
        assert_true(i == dlist_size(test_dlist));
    }

    assert_true(dlist_is_empty(test_dlist));
}
Example #2
0
void print_list(const DList *list) {

DListElmt          *element;

int                *data,
                   i;

/*****************************************************************************
*                                                                            *
*  Display the doubly-linked list.                                           *
*                                                                            *
*****************************************************************************/

fprintf(stdout, "List size is %d\n", dlist_size(list));

i = 0;
element = dlist_head(list);

while (1) {

   data = dlist_data(element);
   fprintf(stdout, "list[%03d]=%03d\n", i, *data);

   i++;

   if (dlist_is_tail(element))
      break;
   else
      element = dlist_next(element);

}

return;

}
Example #3
0
/* dlist_remove */
int dlist_remove(DList *list, DListElmt *element, void **data)
{
    /* Do not allow a NULL element or removal from an empty list. */
    if (element == NULL || dlist_size(list) == 0)
        return -1;
    
    /* Remove the element from the list. */
    *data = element->data;
    
    if (element == list->head)
    {
        /* Handle removal from the head of the list. */
        list->head = element->next;
        
        if (list->head == NULL)
            list->tail = NULL;
        else
            element->next->prev = NULL;
    }
    else
    {
        /* Handle removal from other than the head of the list. */
        element->prev->next = element->next;
        if (element->next == NULL)
            list->tail = element->prev;
        else
            element->next->prev = element->prev;
    }
    
    /* Free the storage allocated by the abstract datatype. */
    list->size--;
    return 0;
}
Example #4
0
void _hashtable_rehash(HASHTABLE *t, size_t newsize)
{
	ARRAY table;
	DLIST *bucket;
	size_t i, j, hash;
	DLIST_ITER it, end;

	array_init(&table, sizeof(DLIST));
	array_resize(&table, newsize);
	for (i = 0; i != newsize; ++i) {
		dlist_init((DLIST*)array_at(&table, i), t->element_size);
	}
	
	j = array_size(&t->table);
	for (i = 0; i != j; ++i) {
		bucket = (DLIST*)array_at(&t->table, i);
		if (dlist_size(bucket)) {
			end = dlist_end(bucket);
			for (it = dlist_begin(bucket); it != end; it = dlist_next(it)) {
				hash = _hashtable_hash(t, dlist_at(it)) % newsize;
				dlist_push((DLIST*)array_at(&table, hash), dlist_at(it));
			}	
		}
	}
	array_destroy(&t->table);
	memcpy(&t->table, &table, sizeof(ARRAY));
}
Example #5
0
int dlist_remove(DList *list, DListNode *element, void **data)
{
    if (dlist_size(list) == 0 || element == NULL) {
        printf("dlist_remove error for dlist_size(list) = 0 || element = NULL");
        return -1;
    }

    *data = element->data;
    
    if (dlist_head(list) == element) {
        list->head = element->next;
        if (list->head == NULL)
            list->tail = NULL;
        else
            element->next->prev = list->head;

    } else {
        element->prev->next = element->next;
        if (element->next == NULL)
            list->tail = element->prev;
        else
            element->next->prev = element->prev;
    }

    free(element);
    list->size--;

    return 0;
}
Example #6
0
boolean dlist_remove_front( lc_dlist_t* p_list ) /* O(1) */
{
	lc_dlist_node_t* p_node;
	boolean result = TRUE;

	assert( p_list );
	assert( dlist_size(p_list) >= 1 );

	p_node = p_list->head->next;

	if( p_node )
	{
		p_node->prev = NULL;

		if( p_node->next == NULL )
		{
			p_list->tail = p_node;
		}
	}
	else
	{
		p_list->tail = NULL;
	}

	DESTROY_CHECK(
		result = p_list->destroy( p_list->head->data );
	);
Example #7
0
File: dlist.c Project: xynhcwg/impc
int dlist_remove(Dlist *dlist,DlistElmt *element,void **data)
{
	if(element == NULL || dlist_size(dlist)=0)
		return -1;
	*data=element->data;
	if(element==dlist->head)
	{
		dlist->head=element->next;
		if(dlist->head == NULL)
			dlist->tail=NULL;
		else
			element->next->prev=NULL;
	}
	else
	{
		element->prev->next=element->next;
		if(element->next == NULL)
			dlist->tail=element->prev;
		else
			element->next->prev=element->prev;
	}
	free(element);
	dlist->size--;
	return 0;
}
Example #8
0
int dlist_remove(dlist_t *list, dlist_elem_t *element, void **data)
{
    if (element == NULL || dlist_size(list) == 0)
        return -1;

    *data = element->data;
    if (element == list->head)
    {
        list->head =element->next;
        if (list->head == NULL)
            list->tail = NULL;
        else
            element->next->prev = NULL;
    }
    else
    {
        element->prev->next = element->next;
        if (element->next == NULL)
            list->tail = element->prev;
        else
            element->next->prev = element->prev;
    }

    free(element);
    list->size--;
    return 0;
}
Example #9
0
File: list.c Project: Engma90/labs
int dlist_remove(DList *list, listElement *element, void **data)
{
    //NULL gali buti tik kai sarasas tuscias
    if (element == NULL && dlist_size(list) != 0)
        return(-1);

    *data = element->data;
    if (element == list->head){
        //pasalinam is pradzios
        list->head = element->next;
        if (list->head != NULL)
            element->next->prev = NULL;
    } 
    else {
        //pasalinam ne is pradzios
        element->prev->next = element->next;
        if (element->next == NULL)
            list->tail = element->prev;
        else
            element->next->prev = element->prev;
    }
    //atlaisvinam atminti reikalinga saraso elementui
    free(element);
    list->size--;
    return(0);
}
Example #10
0
void test_dlist_mergesort_existing(void)
{
    unsigned long old_size;
    /* DListIterator *it; */

    assert_true(test_dlist != NULL);
    assert_false(dlist_is_empty(test_dlist));

    old_size = dlist_size(test_dlist);

    assert_true(dlist_mergesort(test_dlist, (CompareFn)ulong_compare) == 0);

    /* Verify */
    assert_true(dlist_is_sorted(test_dlist, (CompareFn)ulong_compare));
    assert_true(dlist_size(test_dlist) == old_size);
}
Example #11
0
void test_dlist_prepend_to_empty(void)
{
    unsigned long *val = NULL;

    assert_true(test_dlist == NULL);

    test_dlist = dlist_create();

    assert_true(test_dlist != NULL);
    assert_true(dlist_is_empty(test_dlist));

    val = make_ulong_ptr(9999);
    assert_true(val != NULL);

    assert_true(dlist_prepend(test_dlist, val) == 0);

    /* Verify */
    val = NULL;
    val = dlist_index(test_dlist, 0);
    assert_true(val != NULL);
    assert_ulong_equal(9999, *val);
    assert_true(dlist_size(test_dlist) == 1);

    dlist_free_all(test_dlist, NULL);
    test_dlist = NULL;
}
Example #12
0
// remove an element from the list and set the pointer to data in that element
int dlist_remove (DList *list, DListElmt *element, void **data)
{
    if (dlist_size(list) == 0 || element == NULL) {
        // removing from empty list is not allowed
        // cannot remove a null element
        return -1;
    }

    *data = element->data;

    if (element == list->head) {
        // removing from the head of the list
        list->head = element->next;

        if (list->head == NULL) {
            list->tail = NULL;
        } else {
            list->head->prev = NULL;
        }
    } else {
        // removing from other part of the list
        element->prev->next = element->next;

        if (element->next == NULL) {
            list->tail = element->prev;
        } else {
            element->next->prev = element->prev;
        }
    }

    free (element);
    list->size--;
    return 0;
}
Example #13
0
File: hash.c Project: Tumas/labs
void*
spellcast_hash_iterator_next(hash_iterator *h_iter)
{
    int bucket, i;
    listElement *element;

    while (h_iter->index != h_iter->htable->buckets) {
        bucket = hash_key(h_iter->htable, h_iter->index);

        while (dlist_size(&h_iter->htable->table[bucket]) != h_iter->inner_index) {
            element = dlist_head(&h_iter->htable->table[bucket]);

            for (i = 0; i < h_iter->inner_index; i++) {
                element = dlist_next(element);
            }

            h_iter->inner_index++;
            return dlist_data(element);
        }

        h_iter->inner_index = 0;
        h_iter->index++;
    }

    h_iter->index = 0;
    return NULL;
}
Example #14
0
File: list.c Project: Engma90/labs
int dlist_insert(DList *list, const void *data)
{
    listElement *new_element;

    //issiskiriam vietos naujam elementui
    if ((new_element = (listElement*) malloc(sizeof(listElement))) == NULL)
        return(-1);
    new_element->data = (void*)data;

    if (dlist_size(list) == 0){
        //iterpimas i tuscia sarasa
        list->head = new_element;
        list->tail = new_element;
        new_element->prev = NULL;
        new_element->next = NULL;
    }
    else {
        //iterpimas i netuscia sarasa
        //iterpiame i saraso pabaiga
        new_element->next = NULL;
        new_element->prev = list->tail;
        list->tail->next = new_element;
        list->tail = new_element;
    }
    //Atnaujinam saraso dydi
    list->size++;
    return(0);
}
Example #15
0
int dlist_ins_prev(DList *list, DListElmt *element, const void *data)
{
        DListElmt *new_element;

        /*
         * Only allow element to be NULL if the list is empty
         */
	if (element == NULL && dlist_size(list) != 0)
		return -1;

        /*
         * Allocate storage for element and set payload
         */
        if ((new_element = (DListElmt *)malloc(sizeof(DListElmt))) == NULL)
                return -1;

        new_element->data = (void *) data;

	/*
	 * Insert element into list
	 */
	if (dlist_size(list) == 0) {	/* Case 1: List is empty */
		new_element->next = NULL;
		new_element->prev = NULL;

		list->head = new_element;
		list->tail = new_element;
	}
	else {				/* Case 2: List is not empty */
		/* Hook up new_element */
		new_element->prev = element->prev;
		new_element->next = element;

		/* Hook up element */
		element->prev = new_element;

		/* Hook up next element and update head if necessary*/
		if (dlist_is_head(list, element))
			list->head = new_element;
		else
			new_element->prev->next = new_element;
	}

	return 0;
}
Example #16
0
//删除所有节点,利用init函数的destroy函数来清理内存
void dlist_destroy(DList* list)
{
	void *data;
	while((dlist_size(list))>0){
		if(dlist_remove(list,dlist_tail(list),(void **)&data)==0
				&& list->destroy != NULL){
			list->destroy(data);
		}
	}
}
Example #17
0
char *rant_op_size(bot_t * bot, dlist_t ** dl)
{
	char *str = NULL;

	if (!bot || !dl)
		return NULL;

	str = str_unite("%i", dlist_size(*dl));
	return str;
}
Example #18
0
File: list.c Project: Engma90/labs
void dlist_destroy(DList *list)
{
    void *data;
    while (dlist_size(list) > 0){
        if (dlist_remove(list, dlist_tail(list), (void**)&data) == 0 && list->destroy != NULL){
            //saukiam vartotojo nustatyta funkcija destroy dinaminei atminciai atlaisvinti
            list->destroy(data);
        }
    }
}
Example #19
0
void dlist_destroy(dlist_t *list)
{
    void *data;
    while (dlist_size(list) > 0)
    {
        if (dlist_remove(list, dlist_tail(list), (void **)&data) == 0 && list->destroy != NULL)
            list->destroy(data);
    }
    memset(list, 0, sizeof(dlist_t));
}
Example #20
0
// insert a data into list after the given element
//
// case 1 : list is not empty && element is null : -1
// case 2 : new element allocation fail : -1
// case 3 : list is empty : insert the data, update size
// case 4 : list is not empty :
//      case 4.1 :
//      case 4.2 :
//
int dlist_ins_next (DList *list, DListElmt *element, const void *data)
{
    if (element == NULL && dlist_size(list) != 0) {
        // case 1 : not invalid element of a non-empty list
        return -1;
    }

    // allocate memory for new element
    DListElmt *new_element;

    if ((new_element = (DListElmt *)malloc(sizeof(DListElmt)) == NULL)) {
        // case 2 : could not allocate memory for new element
        return -1;
    }

    new_element->data = (void *)data;

    if (dlist_size(list) == 0) {
        // case 3 : empty list
        list->head = new_element;
        list->tail = new_element;
        list->head->prev = NULL;
        list->head->next = NULL;
    } else {
        // case 4 : non-empty list
        new_element->next = element->next;
        new_element->prev = element;

        if (element->next == NULL) {
            // case 4.1 : given element is the last element in list
            list->tail = new_element;
        } else {
            // case 4.2 : given element is not the last element
            element->next->prev = new_element;
        }

        element->next = new_element;

    }

    list->size++;
    return 0;
}
Example #21
0
File: list.c Project: Engma90/labs
/*  ***********************************************     */
static listElement* get_element(DList *list, int n)
{
    if (n < 0 || n > dlist_size(list)-1)
        return (NULL);
    int i = 0;
    listElement *temp = list->head;
    
    for (; i < n ; i++)
        temp = temp->next;
    return(temp);
}
Example #22
0
File: dlist.c Project: xynhcwg/impc
void dlist_destroy(Dlist *dlist)
{
	void *data;
	while(dlist_size(dlist)>0)
	{
		if(dlist_remove(dlist,dlist_head(dlist),(void **)&data)==0 && dlist->destroy != NULL)
			dlist->destroy(data);
	}
	memset(dlist,0,sizeof(Dlist));
	return ;
}
Example #23
0
void test_dlist_create(void)
{
    test_dlist = dlist_create();

    assert_true(test_dlist != NULL);
    assert_true(dlist_size(test_dlist) == 0);
    assert_true(dlist_is_empty(test_dlist));

    dlist_free(test_dlist);
    test_dlist = NULL;
}
Example #24
0
void test_dlist_reverse_existing(void)
{
    unsigned long old_size, val_check;
    unsigned long *val;
    DListIterator *it;

    old_size = dlist_size(test_dlist);

    assert_true(dlist_reverse(test_dlist) == 0);

    /* Verify */
    val = NULL;
    assert_true(old_size == dlist_size(test_dlist));

    for(it = dlist_begin(test_dlist), val_check = 999;
            it != NULL; it = dlist_next(it), val_check--) {
        val = (unsigned long *)dlist_get_data(it);
        assert_ulong_equal(val_check, *val);
    }
}
Example #25
0
/* dlist_ins_prev */
int dlist_ins_prev(DList *list, DListElmt *element, const void *data)
{
    DListElmt *new_element;
    
    /* Do not allow a NULL element unless the list is empty. */
    if (element == NULL && dlist_size(list) != 0)
        return -1;
    
    /* Allocate storage to be managed by the abstract datatype. */
    if ((new_element = (DListElmt *)malloc(sizeof(DListElmt))) == NULL)
        return -1;
    
    /* Insert the new element into the list. */
    new_element->data = (void *)data;
    
    if (dlist_size(list) == 0)
    {
        /* Handle insertion when the list is empty. */
        list->head = new_element;
        list->head->prev = NULL;
        list->head->next = NULL;
        list->tail = new_element;
    }
    else
    {
        /* Handle insertion when the list is not empty. */
        new_element->next = element;
        new_element->prev = element->prev;
        
        if (element->prev == NULL)
            list->head = new_element;
        else
            element->prev->next = new_element;
        
        element->prev = new_element;
    }
    
    /* Adjust the size od the list to account for the inserted element. */
    list->size++;
    return 0;
}
Example #26
0
int dlist_ins_next(DList *list, DListElmt *element, const void *data)
{
    DListElmt *new_element;
	if (element == NULL && dlist_size(list) != 0)
    {
        return -1;
    }


    if ((new_element = (DListElmt*)malloc(sizeof(DListElmt))) == NULL)    
    {
        return -1;
    }

    new_element->data = (void*)data;
    if (dlist_size(list) == 0)
    {
        list->head = new_element;
        list->head->prev = NULL;
        list->head->next = NULL;
        list->tail = new_element;
    }
    else
    {
        new_element->next = element->next;
        new_element->prev = element;
        if (element->next == NULL)
        {
            list->tail = new_element;
        }
        else
        {
            element->next->prev = new_element;
        }
        element->next = new_element;
    }
    list->size++;
    return 0;
}
Example #27
0
// destroy a list by removing all the elements
void dlist_destroy (DList *list)
{
    void *data;

    while (dlist_size(list) > 0) {
        if ((dlist_remove(list, dlist_tail(list), (void **)&data)) == 0 && list-destroy != NULL) {
            list->destroy (data);
        }
    }

    memset (list, 0, sizeof(DList));
    return;
}
Example #28
0
static inline int
dlist_attach(Dlist *dl, Dlist_data *inserted, Dlist_data *inserted_n)
{
  Dlist_data *inserted_p = dlist_prev(inserted_n);

  dlist_next(inserted_p) = inserted;
  dlist_prev(inserted) = inserted_p;
  dlist_next(inserted) = inserted_n;
  dlist_prev(inserted_n) = inserted;

  dlist_size(dl)++;

  return 1;
}
Example #29
0
void dlist_destroy(DList *list)
{
    /* Destroy the double list */
    void    *data;
    while(dlist_size(list) != 0) {
        if (dlist_remove(list, dlist_tail(list), (void **)&data) == 0 && 
            list->destroy != NULL) {
            list->destroy(data);
        }
    }

    memset(list, 0, sizeof(DList));
    return;
}
Example #30
0
/* dlist_destroy */
void dlist_destroy(Dlist *list) {
    void *data;
    /* Remove each element. */
    while(dlist_size(list) > 0) {
        if(dlist_remove(list, dlist_tail(list), (void **)&data) == 0 && list->destroy != NULL) {
            /* call a user-defined function to free dynamically allocated data. */
            list->destroy(data);
        }
    }

    /* no operations are allowed now, but clear the structure as a precaution. */
    memset(list, 0, sizeof(DList));
    return;
}