示例#1
0
node_t* search_element(list_t *list, content_t element) {
	node_t *cur = NULL;
	node_t *_element = NULL;

	if (list == NULL) {
		printf("Initialize your list.");
		return NULL;
	}
	if (list->size == 0) {
		printf("No elements to search\n");
		return NULL;
	}

	_element = create_node(element);

	for (cur = list->head; cur != NULL; cur = cur->next) {
		if (COMP(_element, cur) == 0) {
			destroy_node(_element);
			return cur;
		}
	}

	destroy_node(_element);
	return NULL;
}
示例#2
0
struct node *apply_bin_op(struct node *root, struct node *lhs, struct node *rhs)
{
	long l, r;
	char op, buf[25];

	sscanf(lhs->token, "%ld", &l);
	destroy_node(lhs);

	sscanf(rhs->token, "%ld", &r);
	destroy_node(rhs);

	op = *root->token;
	switch (op) {
	case '+':
		sprintf(buf, "%ld", l + r);
		break;
	case '-':
		sprintf(buf, "%ld", l - r);
		break;
	case 't':
		sprintf(buf, "%ld", l * r);
		break;
	}

	destroy_node(root);
	return create_node(buf, NUMBER);
}
示例#3
0
	void destroy_node( aabb_node* p )
	{
		if( !p ) { return; }
		destroy_node( p->car );
		destroy_node( p->cdr );
		pool_.deallocate( p );
	}
示例#4
0
/*Prune node 'n' from the tree rooted at 't->tree', rearranging the tree as
  necessary in order to preserve the descendants of 'n'.*/
static void prune(btree *t, node *n)
  {
  register node **prune_site, *largest;
  register int ref_count_of_largest;
  prune_site = (n->parent==NULLTREE? &(t->tree): n==n->parent->left_child? &(n->parent->left_child): &(n->parent->right_child));
  if(n->left_child == NULLTREE)
    {
    *prune_site = n->right_child;
    if(*prune_site != NULLTREE)
      (*prune_site)->parent = n->parent;
    destroy_node(n);
    }
  else if(n->right_child == NULLTREE)
    {
    *prune_site = n->left_child;
    if(*prune_site != NULLTREE)
      (*prune_site)->parent = n->parent;
    destroy_node(n);
    }
  else
    {
  /*find the largest value in the left subtree of 'n'*/
    for(largest = n->left_child; largest->right_child != NULLTREE; largest = largest->right_child)
      ;
  /*adjust reference counts to reflect the pruning of this largest value*/
    ref_count_of_largest = largest->ref_count;
    for(largest = n->left_child; largest->right_child != NULLTREE; largest = largest->right_child)
      largest->subtree_ref_count -= ref_count_of_largest;
  /*prune the largest value by replacing it with its left subtree*/
    if(largest==largest->parent->left_child)
      {
      largest->parent->left_child = largest->left_child;
      if(largest->parent->left_child != NULLTREE)
	largest->parent->left_child->parent = largest->parent;
      }
    else
      {
      largest->parent->right_child = largest->left_child;
      if(largest->parent->right_child != NULLTREE)
	largest->parent->right_child->parent = largest->parent;
      }
  /*substitute this largest-valued node for node 'n'*/
    if(n->parent == NULLTREE)
      t->tree = largest;
    else if(n == n->parent->left_child)
      n->parent->left_child = largest;
    else
      n->parent->right_child = largest;
    largest->parent = n->parent;
    largest->left_child = n->left_child;
    largest->right_child = n->right_child;
    if(largest->left_child != NULLTREE)
      largest->left_child->parent = largest;
    if(largest->right_child != NULLTREE)
      largest->right_child->parent = largest;
    largest->subtree_ref_count = largest->ref_count + (largest->left_child==NULLTREE? 0: largest->left_child->subtree_ref_count) + (largest->right_child==NULLTREE? 0: largest->right_child->subtree_ref_count);
    destroy_node(n);
    }
  }
void destroy_node(queue_head* qh) {
	if (qh != NULL) {
		destroy_node(qh->left_subtree);
		destroy_node(qh->right_subtree);

		free(qh);
	}
}
示例#6
0
// Recursively destroys the tree
static void destroy_node(art_node *n) {
    // Break if null
    if (!n) return;

    // Special case leafs
    if (IS_LEAF(n)) {
        free(LEAF_RAW(n));
        return;
    }

    // Handle each node type
    int i;
    union {
        art_node4 *p1;
        art_node16 *p2;
        art_node48 *p3;
        art_node256 *p4;
    } p;
    switch (n->type) {
        case NODE4:
            p.p1 = (art_node4*)n;
            for (i=0;i<n->num_children;i++) {
                destroy_node(p.p1->children[i]);
            }
            break;

        case NODE16:
            p.p2 = (art_node16*)n;
            for (i=0;i<n->num_children;i++) {
                destroy_node(p.p2->children[i]);
            }
            break;

        case NODE48:
            p.p3 = (art_node48*)n;
            for (i=0;i<n->num_children;i++) {
                destroy_node(p.p3->children[i]);
            }
            break;

        case NODE256:
            p.p4 = (art_node256*)n;
            for (i=0;i<256;i++) {
                if (p.p4->children[i])
                    destroy_node(p.p4->children[i]);
            }
            break;

        default:
            abort();
    }

    // Free ourself on the way up
    free(n);
}
示例#7
0
文件: cbtree.c 项目: dimitri/libusual
/* recursive freeing */
static void destroy_node(struct CBTree *tree, struct Node *node)
{
	if (is_node(node)) {
		destroy_node(tree, node->child[0]);
		destroy_node(tree, node->child[1]);
		cx_free(tree->cx, node);
	} else if (tree->obj_free_cb) {
		void *obj = get_external(node);
		tree->obj_free_cb(tree->cb_ctx, obj);
	}
}
示例#8
0
文件: tree.c 项目: Limaa/libds
void destroy_node(tnode_p node){
	if(node->left != NULL) destroy_node(node->left);
	if(node->right != NULL) destroy_node(node->right);

	node->left = NULL;
	node->right = NULL;
	node->parent = NULL;
	free(node->data);
	node->data = NULL;
	free(node);
}
示例#9
0
void destroy_nodes(Node *node) {
  // Recursively frees all the Nodes.
  if(node != NULL) {
    if(node->next == NULL)
      destroy_node(node);
    else {
      destroy_nodes(node->next);
      destroy_node(node);
    }
  }
}
示例#10
0
void destroy_chain(pnode *phead)
{
    pnode ptmp = NULL;
    if (*phead != NULL) {
        while ((*phead)->next != *phead) {
            ptmp = (*phead)->next;
            (*phead)->next = ptmp->next;
            ptmp->next->prev = *phead;
            destroy_node(&ptmp);
        }
        destroy_node(phead);
    }
}
示例#11
0
文件: channel.c 项目: crawford/zigzag
void destroy_channel(channel_t *channel) {
	while (channel->subscribers->head != NULL) {
		destroy_node(channel->subscribers, channel->subscribers->head);
	}
	free(channel->subscribers);
	free(channel);
}
示例#12
0
文件: cbtree.c 项目: dimitri/libusual
/* Free tree and all it's internal nodes. */
void cbtree_destroy(struct CBTree *tree)
{
	if (tree->root)
		destroy_node(tree, tree->root);
	tree->root = NULL;
	cx_free(tree->cx, tree);
}
示例#13
0
文件: func.c 项目: Gonefar/BinaryTree
int process_choice(tree *pTree, int choice)
{
	char *words = (char *)Malloc(64*sizeof(char));
	node *temp = NULL;
	char ch = 'A';

	switch(choice)
		{
			case ADD_NODE:				
				printf("Input the adding node content:\n");
				while(scanf("%s", words) > 0)
					{
						
						//scanf("%s", words);
						ch = getchar();
						insert_node(pTree, words);
						if(ch == '\n')
							{
								break;
							}
						words = (char *)Malloc(64*sizeof(char));
					}
				break;
				
			case DELETE_NODE:
				printf("Input the destroy node content\n");
				while(scanf("%s", words) > 0)
					{
					
						//scanf("%s", words);
						destroy_node(pTree, words);
						ch = getchar();
						if(ch == '\n')
							{
								break;
							}
						words = (char *)Malloc(64*sizeof(char));
					}
				break;
					
			case FIND_NODE:
				printf("Input the search node content\n");
				scanf("%s", words);
				temp = search_node_by_content( pTree, words);
				break;
				
			case SHOW_TREE:
				print_tree(pTree);
				break;
				
			case QUIT:
				print_tree(pTree);
				printf("Nodes num:%d, row:%d\n", pTree->nodeNum, DEPTH);
				exit(1);
				break;
		}
			
	print_choice();	
	return 1;
}
示例#14
0
int main(int argc, const char **argv)
{
	struct dsp_node *node;
	int ret = 0;
	unsigned i;

	signal(SIGINT, signal_handler);

#ifdef DEBUG
	debug_level = 3;
#endif
	ntimes = 1000;

	argc--; argv++;
	handle_options(&argc, &argv);

	dsp_handle = dsp_open();

	if (dsp_handle < 0) {
		pr_err("dsp open failed");
		return -1;
	}

	if (!dsp_attach(dsp_handle, 0, NULL, &proc)) {
		pr_err("dsp attach failed");
		ret = -1;
		goto leave;
	}

	node = create_node();
	if (!node) {
		pr_err("dsp node creation failed");
		ret = -1;
		goto leave;
	}

	run_task(node, ntimes);
	destroy_node(node);

leave:
	if (proc) {
		if (!dsp_detach(dsp_handle, proc)) {
			pr_err("dsp detach failed");
			ret = -1;
		}
		proc = NULL;
	}

	for (i = 0; i < ARRAY_SIZE(events); i++)
		free(events[i]);

	if (dsp_handle > 0) {
		if (dsp_close(dsp_handle) < 0) {
			pr_err("dsp close failed");
			return -1;
		}
	}

	return ret;
}
示例#15
0
文件: treetest.c 项目: Limaa/libds
int main(void){
	struct tree tr;
	tr.root = NULL;
	tr.cmpfunc = intcmpfunc;

	insert_int(&tr, 15);
	insert_int(&tr, 5);
	insert_int(&tr, 3);
	insert_int(&tr, 12);
	insert_int(&tr, 10);
	insert_int(&tr, 6);
	insert_int(&tr, 7);
	insert_int(&tr, 16);
	insert_int(&tr, 20);
	insert_int(&tr, 18);
	insert_int(&tr, 23);

	findthree(&tr);

	traverse(tr.root, print_data);

	print_successor(tr.root->left);

	rb_delete(&tr, tr.root->left);
	rb_delete(&tr, tr.root->left->right);
	rb_delete(&tr, tr.root->left->left);
	/*left_rotate(&tr, tr.root->right);
	right_rotate(&tr, tr.root->right);*/

	traverse(tr.root, print_data);

	destroy_node(tr.root);

	return 0;
}
示例#16
0
int pop(LinkedList *list) {
  // Yields the last item in the list.
  int result = 0;
  Node *temp;

  if(list != NULL && list->length > 0) {
    temp = list->end;
    result = temp->data;
    list->length -= 1;
    
    if(list->length == 0) {  // We emptied the list.
      list->root = NULL;
      list->end = NULL;
    }
    else {  // Still other nodes left.
      temp->prev->next = NULL;
      list->end = temp->prev;
    }

    destroy_node(temp);
  }
  else {
    perror("ERROR: Empty List passed!");
  }

  return result;
}
示例#17
0
LinkedList * list_remove(LinkedList *list, int data) {
  // Removes a given piece of data from the list.
  Node *curr;

  if(list != NULL) {
    curr = list->root;

    while(curr != NULL) {
      if(curr->data == data) {
	list->length -= 1;

	if(curr == list->root) {  // Removing the root.
	  list->root = list->root->next;
	  
	  if(list->length == 0)
	    list->end = NULL;
	  else
	    list->root->prev = NULL;
	}
	else if(curr->next == NULL) {  // Removing last node.
	  curr->prev->next = NULL;
	  list->end = curr->prev;
	}
	else  // A normal node.
	  curr->prev->next = curr->next;

	destroy_node(curr);
	break;
      }
      curr = curr->next;
    }
  }

  return list;
}
示例#18
0
node *load_bptree_node(unsigned long node_id)
{
     int i;
     node *nd;    
     node_block  *nd_block = (node*)read_data(node_id);
    
     if(nd_block)
     {   
        nd = malloc(sizeof(node));
        if(nd)
        {
            nd->block = nd_block;
            nd->num_keys = nd_block->num_keys;
            nd->is_leaf = nd_block->is_leaf;
            for(i = 0; i < nd->num_keys ; i++)
            {
                nd->keys[i] = read_data(nd_block->keys_id[i]);
                if(NULL == nd->keys[i])
                {
                    destroy_node(nd);
                    return NULL;
                }
            }
            for(i = 0; i < size ; i++)
            {
                nd->pointers[i] = NULL;
            }             
            return nd;
        }
        free(nd_block);   
     } 
   
     return NULL;
}
示例#19
0
static int alloc_nodes(void)
{
	int ret, i;

	nodes = calloc(connections, sizeof *nodes);
	if (!nodes) {
		printf("cmatose: unable to allocate memory for test nodes\n");
		return -ENOMEM;
	}

	for (i = 0; i < connections; i++) {
		nodes[i].id = i;
		if (opts.dst_addr) {
			ret = init_node(nodes + i, info);
			if (ret)
				goto err;
		}
	}
	return 0;
err:
	while (--i >= 0)
		destroy_node(nodes + i);
	free(nodes);
	return ret;
}
示例#20
0
void destroy_tree(t_tree node)
{
	if (node == NULL)
		return;
	destroy_tree(node->Node.Stmnt.Next);
	destroy_node(node);
}
示例#21
0
static gboolean
dsp_deinit(GstDspDummy *self)
{
	gboolean ret = TRUE;

	if (self->node) {
		if (!destroy_node(self->dsp_handle, self->node)) {
			GST_ERROR("dsp node destroy failed");
			ret = FALSE;
		}
		self->node = NULL;
	}

	if (self->proc) {
		if (!dsp_detach(self->dsp_handle, self->proc)) {
			GST_ERROR("dsp detach failed");
			ret = FALSE;
		}
		self->proc = NULL;
	}

	if (self->dsp_handle >= 0) {
		if (dsp_close(self->dsp_handle) < 0) {
			GST_ERROR("dsp close failed");
			ret = FALSE;
		}
		self->dsp_handle = -1;
	}

	return ret;
}
示例#22
0
Npfile *np_net_make_root(void)
{
	// [net]/tcp/clone
	// [net]/dns
	
	Npfile *netroot = make_node(0, "net", P9_QTDIR, &tcp_gen_vtab);
	if (netroot == 0)
		goto error;
	Npfile *tcpdir = make_node(netroot, "tcp", P9_QTDIR, &tcp_gen_vtab);
	tcpdir->mode |= S_IWUSR;
	if (tcpdir == 0)
		goto error;
	Npfile *clone = make_node(tcpdir, "clone", P9_QTFILE, &tcp_clone_vtab);
	if (clone == 0)
		goto error;
	Npfile *dns = make_node(netroot, "dns", P9_QTFILE, &dns_vtab);
	if (dns == 0)
		goto error;

	return netroot;

error:
	destroy_node(netroot);
	return 0;
}
示例#23
0
/**
  Removes all instances of ptr from the queue.

  This function should not use the comparer function, but check if the data contained in each element of the queue is equal (==) to ptr.

  @param q a pointer to an instance of the priqueue_t data structure
  @param ptr address of element to be removed
  @return the number of entries removed
 */
int priqueue_remove(priqueue_t *q, void *ptr)
{
    int number_removed = 0;

    while(q->size >0 && ptr == get_obj_node(q->head)) {
        priqueue_poll(q);
        number_removed ++;
    }

    node_t *iterator, *node_ptr;
    if(q->size > 0)
    {
        iterator = q->head;
        while(iterator->next != NULL ) {
            if(get_obj_node(iterator->next) == ptr) {
                node_ptr = iterator -> next;
                iterator -> next = node_ptr -> next;
                q->size --;
                destroy_node(node_ptr);
                number_removed ++;
                continue;
            }
            iterator = iterator -> next;
        }
    }

    return number_removed;
}
// return the top element and remove it from the stack
int pop(STACK *pStack) {
  NODE* pNode = pStack->head;
  pStack->head = pStack->head->next;
  int val = pNode->val;
  destroy_node(pNode);
  return val;
}
示例#25
0
static void
destroy_children (FMTreeModel *model, TreeNode *parent)
{
	while (parent->first_child != NULL) {
		destroy_node (model, parent->first_child);
	}
}
示例#26
0
static void destroy_nodes(Parts db)
{
  Node *next;
  for (Node *n = db->head; n; n = next) {
    next = n->next;
    destroy_node(n);
  }
}
示例#27
0
文件: linkedList.c 项目: manurs/libs
/**
 * @page destroy_all_nodes \b destroy_all_nodes
 *
 * @brief Destroy all nodes of a list.
 *
 * @param node Node to be destroyed.
 * @return 0 always.
 *
 * @section SYNOPSIS
 *  \b #include \b "/includes/lista.h"
 *  \b lib/libLista.a 
 *
 *  \b int \b destroy_all_nodes (\b Nodo * node \b)
 * 
 * @section descripcion DESCRIPCIÓN
 *
 * Deletes the node of a list an al the "next" nodes of that node.
 * This function shuld be called from destroy_list(3)
 * 
 * recives:
 *    - The node of the list
 *
 * @section return RETORNO
 * 0 always.
 *
 * @section seealso VER TAMBIÉN
 * \b create_list(3), \b destroy_node(3), \b destroy_list(3), \b delete_elem_list(3),\b is_empty_list(3), \b find(3), \b insert_list(3)
 *
 * @section authors AUTOR
 * Nestor Campillo ([email protected])
 * Adrian Bueno ([email protected])
*/
int destroy_all_nodes (Node * node){
    if (node->next != NULL){
        destroy_all_nodes(node->next);     
    }
    destroy_node(node);
    node=NULL;
    return 0;
}
示例#28
0
static void destroy_nodes(void)
{
	int i;

	for (i = 0; i < connections; i++)
		destroy_node(nodes + i);
	free(nodes);
}
示例#29
0
static void destroy_nodes(void)
{
	int i;

	for (i = 0; i < connections; i++)
		destroy_node(&test.nodes[i]);
	free(test.nodes);
}
示例#30
0
void basic_btree<key_type_, value_type_, traits_type>::clear_r(
	branch_node *b, size_type h
)
{
	if (h > 2) {
		for (auto n : b->c)
			clear_r(n.b, h - 1);
	} else {
		for (auto n : b->c) {
			auto c(n.l->c.size());
			destroy_node(n.l);
			val_count -=c;
		}
	}

	destroy_node(b);
}