Example #1
0
File: set.c Project: rjose/products
int set_remove(Set *set, void **data)
{
        ListElmt *member, *prev_to_member;

        /*
         * Find member to remove
         */

        /* Start at the head of the list.
         * Note prev_to_member is NULL as it should be for member = HEAD */
        member = list_head(set);
        prev_to_member = NULL;
        for ( ; member != NULL; member = list_next(member)) {

                /* Logic is a little delicate here. When we break,
                 * prev_to_member will be correct. */
                if (set->match(*data, list_data(member)))
                        break;

                prev_to_member = member;
        }

        /*
         * If we didn't find the element, return -1
         */
        if (member == NULL)
                return -1;

        return list_rem_next(set, prev_to_member, data);
}
Example #2
0
 int chtbl_remove(CHTbl *htbl, void **data){
         ListElem *elem,*prev;
     
                 int bucket;
     
                 bucket = htbl->h(*data) % htbl->buckets;
     
                 prev =NULL;
     
                 for (elem=list_head(&htbl->table[bucket]);elem !=NULL;elem=list_next(elem)){
                 if (htbl->match(*data,list_data(elem))){
                         if (list_rem_next(&htbl->table[bucket],prev,data)==0){
                 
                                         htbl->size--;
                                 return 0;
                             } else{
                                 return -1;
                             }
                     }
         
                         prev = elem;
             }
     
                 //data not found
                         return -1;
     
             };
int set_remove(Set * set, void **data)
{

    ListElmt *member, *prev;

    /*****************************************************************************
    *  Find the member to remove.                                                *
    *****************************************************************************/

    prev = NULL;

    for (member = list_head(set); member != NULL;
            member = list_next(member)) {

        if (set->match(*data, list_data(member)))
            break;

        prev = member;

    }

    /*****************************************************************************
    *  Return if the member was not found.                                       *
    *****************************************************************************/

    if (member == NULL)
        return -1;

    /*****************************************************************************
    *  Remove the member.                                                        *
    *****************************************************************************/

    return list_rem_next(set, prev, data);

}
Example #4
0
/*****************************************************************************
*                                                                            *
*  ----------------------------- list_destroy -----------------------------  *
*                                                                            *
*****************************************************************************/
void list_destroy(List *list) {
	void *data;
	/*****************************************************************************
	*                                                                            *
	*  Remove each element.                                                      *
	*                                                                            *
	*****************************************************************************/
	
	while (list_size(list) > 0) {
	   if (list_rem_next(list, NULL, (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(List));
	
	return;
}
Example #5
0
//rehashing function
int rehash(CHTbl *htbl)
{
 void *key;
 void *data;
 int i, j, oldbuckets, size;
 
 oldbuckets = htbl->buckets;
 htbl->buckets *= 2;
 for(i = oldbuckets; i < htbl->buckets; ++i)
  list_init(&htbl->table[i], list_dest_elm, NULL, list_print_elm);

 htbl->h = f[((*fcptr)++)%3];

 for(i = 0; i < oldbuckets; ++i)
 {
  size = (&(htbl->table[i]))->size;
  for(j = 0; j < size; ++j)
  {
   list_rem_next(&(htbl->table[i]), NULL, (const void**) &key, (const void**) &data);
   htbl->size--;
   chtbl_insert(&htbl, key, data);
  }
 }
 return 0;
}
Example #6
0
/* chtbl_remove */
int chtbl_remove(CHTbl *htbl, void **data) {
	ListElmt *element, *prev;
	int bucket;

	/* Hash the key. */
	bucket = htbl->h(*data) % htbl->buckets;

	/* Search for the data in bucket. */
	prev = NULL;

	for (element = list_head(&htbl->table[bucket]); element != NULL; 
	     element = list_next(element)) {
		if (htbl->match(*data, list_data(element))) {
			/* Remove the data from the bucket. */
			if (list_rem_next(&htbl->table[bucket], prev, data) == 0) {
				htbl->size--;
				return 0;
			} else {
				return -1;
			}
		}
		prev = element;
	}

	/* Return that the data was not found. */
	return -1;
}
Example #7
0
int
chtbl_remove_kv(CHTbl *htbl, SweetListKv **data)
{
	ListElmt	*element,
				*prev;
	int			bucket;

	bucket = htbl->h((*data)->key) % htbl->buckets;

	prev = NULL;

	for (element = list_head(&htbl->table[bucket]); element != NULL; element = list_next(element)){
		if (htbl->match(*data, list_data(element))){
			if (list_rem_next(&htbl->table[bucket], prev, (void **)data) == 0){
				htbl->size--;
				return 0;
			}else {
				return -1;
			}
		}

		prev = element;
	
	}

	return -1;
}
/*删除哈希表中的元素*/
int chtbl_remove(CHTbl *chtbl,void **data){
    ListElmt *element,*prev;
    int bucket;
    
    /*Hash the key*/
    bucket = chtbl->h(*data)%chtbl->buckets;
    
    /*在哈希表中查找data*/
    prev = NULL;
    
    for (element = list_head(&chtbl->table[bucket]); element==NULL; element = list_next(element)) {
        if (chtbl->match(*data,list_data(element))) {
            if (list_rem_next(&chtbl->table[bucket], prev, data)==0) {
                chtbl->size--;
                return 0;
            }else{
                return -1;
            }
        }
        
        prev = element;
    }
    
    return -1;
}
Example #9
0
File: graph.c Project: whatot/ma_c
/* graph_destroy */
void graph_destroy(Graph *graph)
{
	AdjList *adjlist;

	/* Remove each adjacency-list structure and destroy its adjacency list.*/
	while (list_size(&graph->adjlists) > 0) {
		if (list_rem_next(&graph->adjlists, NULL, 
				  (void **)&adjlist) == 0) {

			set_destroy(&adjlist->adjacent);

			if (graph->destroy != NULL) {
				graph->destroy(adjlist->vertex);
			}

			free(adjlist);
		}
	}

	/* Destroy the list of adjacency-list structures, which is now empty. */
	list_destroy(&graph->adjlists);

	/* No operation are allowed now, 
	 * but clear the structures as a precaution. */
	memset(graph, 0, sizeof(Graph));
	return;
}
Example #10
0
int main(int argc, char **argv){
  ListElmt *node;
  int i;
  int x[] = {1,5,3,4,0,-2,100};   /* create a buffer of ints */
  char y[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g'};
  int tmp;
  char ytmp;
  int *xp = &tmp;
  char *yp = &ytmp;
  char *otherKey = "test";
  int otherVal = -121;
  int n = sizeof(x)/sizeof(int);

  /* create the list. would be nice if list_init did this */
  List *list = (List *) malloc(sizeof(List)); 
  /* pass list to list_init with destroy and tostring methods */
  list_init(list,int_destroy,NULL,int2str);
  /* insert at head of list */
  list_ins_next(list,NULL,&y[0],&x[0]);
  list_print(list);
  /* let's do a few more */
  node = list_head(list);
  for (i=1;i<n;++i){
    list_ins_next(list,node,&y[i],&x[i]);
    list_print(list);
    node = list_next(node);
  }
  /* now what if we want to, say, insert after the 3rd element of the list? */
  node = list->head;

  /* unfortunately we have to traverse the list from the head until we get the 3rd element */
  /* next we add this capability to the list class itself via a new higher level method */
  for (i=0;i<3;++i){
    node = list_next(node);
  }
  list_ins_next(list,node,otherKey,&otherVal);
  list_print(list);

  /* now let's remove some elements. let's start with head */
  list_rem_next(list,NULL,(const void **) &yp,(const void **) &xp);
  list_print(list);
  list_rem_next(list,NULL,(const void **) &yp,(const void **) &xp);
  list_print(list);
  printf("%d\n", *xp);

  return 0;
}
Example #11
0
File: list.c Project: pasoev/praxis
void list_destroy(List *list) {
    void *data;

    while (list->size > 0)
        if (list_rem_next(list, NULL, (void **) &data) == 0 && list->destroy != NULL)
            list->destroy(data);
    memset(list, 0, sizeof (List));
}
Example #12
0
void list_destroy(List *list){
  void *data;
  while (list_size(list) > 0){
    if (list_rem_next(list,NULL,(const void **) &data) == 0 &&
	list->destroy != NULL)
      list->destroy(data);
  }
  memset(list,0,sizeof(list));
}
Example #13
0
File: graph.c Project: whatot/ma_c
/* graph_rem_vertex */
int graph_rem_vertex(Graph *graph, void **data)
{
	ListElmt *element, *temp, *prev;
	AdjList *adjlist;
	int found;

	/* Traverse each adjacency list and the verticces it contains. */
	prev = NULL;
	found = 0;

	for (element = list_head(&graph->adjlists); element != NULL; 
	     element = list_next(element)) {
		/* Do not allow removal of the vertex if 
		 * it is an adjacency list. */
		if (set_is_member(&((AdjList *)list_data(element))
				->adjacent, *data)) {
			return -1;
		}

		/* Keep a pointer to the vertex to br removed. */
		if (graph->match(*data, 
				 ((AdjList *)list_data(element))->vertex)) {
			temp = element;
			found = 1;
		}

		/* Keep a pointer to the vertex before 
		 * the vertex to be removed. */
		if (!found) {
			prev = element;
		}
	}

	/* Return if the vertex was not found. */
	if (!found) {
		return -1;
	}

	/* Do not allow removal of the vertex if 
	 * its adjacency list is not empty. */
	if (set_size(&((AdjList *)list_data(temp))->adjacent) > 0) {
		return -1;
	}

	/* Remove the vertex. */
	if (list_rem_next(&graph->adjlists, prev, (void **)&adjlist) != 0) {
		return -1;
	}

	/* Free the storage allocated by the abstract datatype. */
	*data = adjlist->vertex;
	free(adjlist);

	/* Adjust the vertex count to account for the removed vertex. */
	graph->vcount--;
	return 0;
}
Example #14
0
int main() {
    // setup
    List *list1 = malloc(sizeof(List));
    int *five = malloc(sizeof(int));
    int *four = malloc(sizeof(int));
    int *three = malloc(sizeof(int));
    int *two = malloc(sizeof(int));
    int *one = malloc(sizeof(int));
    ListElmt *go_to_end = malloc(sizeof(ListElmt));
    void *tempvar = malloc(sizeof(int));

    printf("test list_init\n");
    list_init(list1, &destroy_int);
    printf("list_init success\n");

    *five = 5;
    *four = 4;
    *three = 3;
    printf("test list_ins_front\n");
    list_ins_front(list1, five);
    list_ins_front(list1, four);
    list_ins_front(list1, three);
    print_list(list1);
    printf("test list_ins_next\n");

    *two = 2;
    *one = 1;
    list_ins_next(list1, list_head(list1), two);

    go_to_end = list_head(list1);
    while(go_to_end->next != NULL) {
        go_to_end = go_to_end->next;
    }
    list_ins_next(list1, go_to_end, one);
    print_list(list1);

    printf("test list_rem_next\n");
    list_rem_next(list1, list_head(list1), tempvar);
    list1->destroy(tempvar);
    print_list(list1);

    printf("test list destroy\n");
    list_destroy(list1);
    print_list(list1);
    printf("list size: %d\n", list_size(list1));

    // clean up
    free(tempvar);
    //free(go_to_end);
    free(one);
    free(two);
    // the following are freed by list_destroy
    // free(three);
    // free(four);
    // free(five);
    free(list1);
}
Example #15
0
 void list_destroy(List *list){
   void *data;
   while(list_size(list)>0){
	     if(list_rem_next(list,NULL,(void **)&data)==0&&list->destroy!=NULL)
		 {
		   list->destroy(data);
		 }
   }
   memset(list,0,sizeof(List));//清空list
   return;
 }
Example #16
0
int queue_dequeue(Queue *queue, void **data) {

/*****************************************************************************
*                                                                            *
*  Dequeue the data.                                                         *
*                                                                            *
*****************************************************************************/

return list_rem_next(queue, NULL, data);

}
Example #17
0
int stack_pop(Stack *stack, void **data) {

/*****************************************************************************
*                                                                            *
*  Pop the data off the stack.                                               *
*                                                                            *
*****************************************************************************/

return list_rem_next(stack, NULL, data);

}
Example #18
0
int list_destroy(struct linked_list* list) {
  if (list == NULL) 
    return -1;
  void* data;
  while (list_size(list) > 0) {
    if (list_rem_next(list, NULL, (void **)&data) == 0 && list->destroy != NULL) {
      list->destroy(data);
    }
  }

  memset(list, 0, sizeof(struct linked_list));
  return 0;
}
Example #19
0
//list_destory
void list_destory(List *list) {
	void *data;
	while (list_size(list)>0){
		
		if(list_rem_next(list,NULL,(void**)&data) == 0) {
		//	list->destory(data);
			free(list);
		}
	}
	memset(list, 0, sizeof(List));
	return;

}
Example #20
0
void LinksAspect::delete_biedge(int i, int j)
{
  LinksInfo *iInfo = (*this)[i];
  LinksInfo *jInfo = (*this)[j];

  ListElmt *element, *prev;
  int retval=-2;

  if (i == j) return;
  prev = NULL;
  for (element = iInfo->Table.head; element != NULL;
		element = element->next)
  {
    if ( j == element->data->v2)
	{
	  list_rem_next(&iInfo->Table, prev);
	  Links--;
	  iInfo->Degree--;
	  break;
	}
    prev = element;
   }
   prev = NULL;
   for (element = jInfo->Table.head; element != NULL;
	   element = element->next)
   {
	 if ( i == element->data->v2)
	 {
	   list_rem_next (&jInfo->Table, prev);

	   Links--;
	   jInfo->Degree--;
	   break;
	 }
     prev = element;
   }
}
Example #21
0
File: sllist.c Project: pascal-p/c
void list_free(sllist_t **lst) {
  void *data;

  while ((*lst)->size > 0) {
    if (list_rem_next(*lst, NULL, (void **) &data) == 0 && 
        (*lst)->destroy != NULL) {
      (*lst)->destroy(data);
    }
  }
  //
  memset(*lst, 0, sizeof(sllist_t));
  free(*lst);
  *lst = NULL;
  return;
}
Example #22
0
int graph_rem_vertex(Graph *graph, void **data)
{
    ListElmt *element, *temp, *prev;
    AdjList *adjlist;
    int found;

    prev = NULL;
    found = 0;

    for (element = list_head(&graph->adjlists); element != NULL; list_next(element)) {

        // 如果在一个临近链表中,不允许移除其顶点
        if (set_is_member(&(AdjList *)list_data(element))->adjacent, *data) {
            return -1;
        }

        // 保持一个指针指向需要被移除的顶点
        if (graph->match(*data, ((AdjList *)list_data(element))->vertex)) {
            temp = element;
            found = 1;
        }

        // 保持一个指针在顶点被移除之前的顶点
        if (! found) {
            prev = element;
        }
    }

    // 如果顶点未被找到就返回
    if (! found) {
        return -1;
    }

    // 如果临近链表非空,则不允许移除其顶点
    if (set_size(&((AdjList *)list_data(temp))->adjacent) > 0) {
        return -1;
    }

    // 移除顶点
    if (list_rem_next(&graph->adjlists, prev, (void **)&adjlist) != 0) {
        return -1;
    }

    *data = adjlist->vertex;
    free(adjlist);
    graph->vcount--;
    return 0;
}
Example #23
0
int set_remove(Set *set, void **data)
{
	ListElmt *member, *prev;
	prev = NULL;
	for (member = list_head(set); member != NULL; member = list_next(member))
	{
		if (set->match(*data, list_data(member)))
			break;
		prev = member;
	}

	if (member == null)
		return -1;

	return list_rem_next(set, prev, data);
}
Example #24
0
int alloc_frame(List *frames)
{
    int frame_number, *data;
    
    if (list_size(frames) == 0) {
        return -1;
    }else{
        if (list_rem_next(frames, NULL, (void**)&data) != 0) {
            return -1;
        }else{
            frame_number = *data;
            free(data);
        }
    }
    return frame_number;
}
Example #25
0
void list_destroy(List *list)
{
  void *data;
  
  /// 删除每一个元素
  while (list_size(list) > 0) {
    if (list_rem_next(list, NULL, (void **)&data) == 0 && list->destroy != NULL) {
      /// 调用用户定义的 destroy 方法销毁动态创建的数据
      list->destroy(data);
    }
  }

  /// 清理链表结构体数据
  memset(list, 0, sizeof(List));
  
  return;
}
Example #26
0
/* alloc_frame */
int alloc_frame(List *frames){
    int frame_number, *data;
    
    if(list_size(frames) == 0)
        /* return that there are no frames available. */
        return -1;
    else{
        if(list_rem_next(frames, NULL, (void **)&data) != 0)
        /* return that a frame could not be retrieved. */
            return -1;
        else{
            /* store the number of the available frame. */
            frame_number = *data;
            free(data);
        }
    }
    return frame_number;
}
Example #27
0
// remove the element iteratively
void list_destory (List *list)
{
    void *data;

    while (list_size(list) > 0) {
        if ((list_rem_next (list, NULL, (void **)&data)) == 0 && list->destory != NULL) {
            // call a user defined function to destroy data
            list->destory (data);
        }
    }

    memset (list, 0, sizeof (List));
    // question : why using memset zero instead of free() ?
    // ans : the pointer is still held by the calling function, that function is able
    //      to decide whether to free this memory block or allocate another
    //      memory block and assign that block address to list.
    return;
}
Example #28
0
//frees up all memory from the table
void destroy_table(CHTbl *htbl)
{
 int i;
 ListElmt *element, *parent;
 List *clist;
 void *key, *data;
 for(i = 0; i < htbl->buckets; ++i)
 {
  clist = &htbl->table[i];
  parent = NULL;
  for(element = list_head(clist); element != NULL; element = list_next(element))
  {
   list_rem_next(clist, parent, (const void **) &key, (const void **)&data);
   clist->destroy(key, data);
   htbl->size--;
   parent = element;
  }
 }
}
Example #29
0
void graph_destroy(Graph *graph)
{
    AdjList *adjlist;

    // 移除每一个相邻的链表结构和链表
    while(list_size(&graph->adjlists) > 0) {
        if (list_rem_next(&graph->adjlists, NULL, (void **)&adjlist) == 0) {
            set_destroy(&adjlist->adjacent);

            if (graph->destroy != NULL) {
                graph->destroy(adjlist->vertex);
                free(adjlist);
            }
        }
    }

    list_destory(&graph->adjlists);
    memset(graph, 0 sizeof(Graph));
    return;
}
Example #30
0
//function to remove an item with the key "key" from the hash table
int htbl_remove(CHTbl *htbl, void *key)
{
 List *clist;
 ListElmt *element, *parent = NULL;
 int bucket;
 void *nkey, *data;

 bucket = htbl->h(key)%htbl->buckets;
 clist = &htbl->table[bucket];
 for(element = list_head(clist); element != NULL; element = list_next(element))
 {
  if(!htbl->match(key, list_key(element)))
  {
   list_rem_next(clist, parent, (const void **) &nkey, (const void **)&data);
   clist->destroy(nkey, data);
   htbl->size--;
   return 0;
  }
  parent = element;
 }

 return -1;
}