void test_assignment(void) { printf("\ntest_assignment\n"); { struct Node* f0 = node_new(__ASSIGNMENT); struct Node* n0 = create_declarator_scalar(); struct Complex* n1val = complex_new(111,222); struct Node* n1 = node_new_leaf(__CONST_FLOAT, (void*)n1val); node_link(f0, n0); node_link(f0, n1); calcnode(f0); } { struct Node* f0 = node_new(__ASSIGNMENT); struct Node* n0 = create_declarator_array(); struct Complex* n1val = complex_new(333,444); struct Node* n1 = node_new_leaf(__CONST_FLOAT, (void*)n1val); node_link(f0, n0); node_link(f0, n1); calcnode(f0); } g(); }
int add_at_first(linkedlist_t* _list, void* data) { linkedlist_t* list = (linkedlist_t*) _list; node* n, *slider; if(list->size == 0) { return add_at_last(list, data); } else if(list->size == 1) { n = node_new(data); set_head(list, n); set_prev(get_tail(list), n); set_next(n, get_tail(list)); list->size++; } else // list->size > 1 { slider = get_head(list); n = node_new(data); set_next(n, slider); set_prev(slider, n); set_head(list, n); list->size++; } return list->size; }
int main() { Queue queue = queue_new(); enqueue(queue, node_new(1)); printf("1.Simon node is %p\n", queue->rear); enqueue(queue, node_new(2)); printf("1.Filoa node is %p\n", queue->rear); enqueue(queue, node_new(3)); printf("1.Cindy node is %p\n", queue->rear); enqueue(queue, node_new(4)); printf("1.Gateman node is %p\n", queue->rear); printf("queue len is %d\n", queue->len); printQueue(queue); int i = 0; LLQNode node; printf("5. node is %p\n", node); int len = queue->len; for(i = 0; i < len; i++) { printf("2. i is %d\n", i); dequeue(queue, &node); node_print(node); printf("2. %p\n", node); printf("\n"); free(node); } printf("\n"); return 0; }
/* insert a new node to a list. */ int node_list_add( Tnode_list_ptr L, int n, int tag ) { // locate where to add the node in the list Tnode_ptr node; node = L->opt; L->n++; // the list is empty, so add the node as is if( node==NULL ) { L->head = node_new( n, tag, L->block, NULL, NULL ); L->opt = L->head; L->start = L->head; return 1; } // the new node goes at the start; if( tag < L->start->tag ) { L->start = node_new( n, tag, L->block, NULL, L->start ); L->opt = L->start; return 1; } // the new node goes at the end if( tag > L->head->tag ) { L->head = node_new( n, tag, L->block, L->head, NULL ); L->opt = L->head; return 1; } // the new node is below the current optimal insertion range if( tag < node->tag ) { while( tag < node->tag ) { node = node->prev; } L->opt = node_new( n, tag, L->block, node, node->next ); return 1; } // the new node is above the current optimal insertion range if( tag > node->tag ) { while( tag > node->tag ) { node = node->next; } L->opt = node_new( n, tag, L->block, node->prev, node ); return 1; } ERROR( "node_list_add() : attempt to add node with non-unique tag" ); return 0; }
list_t *list_new(void){ list_t *list = malloc(sizeof(struct list_s)); list->count = 0; list->head = node_new(NULL, -1); list->tail = node_new(NULL, -1); list->head->next = list->tail; list->tail->prev = list->head; return list; }
Node* node_new_expr_index1(Node* left, Node* index) { Node* expr_list = node_new(EXPR_LIST); node_add_child(expr_list, index); Node* rv = node_new(EXPR_INDEX); node_add_child(rv, left); node_add_child(rv, expr_list); return rv; }
list_t * list_new_dealloc(list_dealloc_fn deallocator) { list_t * self = malloc(sizeof(struct list_s)); self->deallocator = deallocator; self->size = 0; self->head = node_new(NULL); self->tail = node_new(NULL); self->head->next = self->tail; self->tail->prev = self->head; return self; }
Node unary_operator(Node optr, Node expr) /*;unary_operator*/ { /* Set up the AST node for a unary operator. */ Node node, arg_list_node; node = node_new(as_un_op); arg_list_node = node_new(as_list); N_LIST(arg_list_node) = tup_new1((char *)expr); insert_2child(node, optr, arg_list_node); return(node); }
/* * 头部添加 */ void list_prepend(LinkList *list, ElementType data) { NODE *new_node; if( list_empty(list) ) { new_node = node_new(NULL, data); } else { new_node = node_new(list->head->next, data); } list->head = new_node; list->length++; }
/* Push a node at the end of Queue @param: takes Queue pointer, index value as well as data to be set @return: return the current pointer of the Queue */ Queue* queue_enqueue( Queue* st, char *string ){ if(queue_size(st)==0){ st->head=node_new(string,NULL); st->tail=st->head; return st; } st->tail->next=node_new(string,NULL); st->tail=st->tail->next; return st; }
Node binary_operator(Node optr, Node expr1, Node expr2) /*;binary_operator*/ { /* Set up the AST node for a binary operator. */ Node node, arg_list_node; node = node_new(as_op); arg_list_node = node_new(as_list); N_LIST(arg_list_node) = tup_new2((char *)expr1, (char *)expr2); insert_2child(node, optr, arg_list_node); return(node); }
Ret buddy_alloc(Buddy* thiz, size_t size, size_t* offset) { return_val_if_fail(thiz!=NULL && size>0 && size<=thiz->size && offset!=NULL, RET_FAIL); Node* iter = NULL; size_t i; for(i=size; i<=thiz->size; i++) { if(thiz->free_list[i] != NULL) { iter = thiz->free_list[i]; break; } } if(iter == NULL) { return RET_FAIL; } size_t found_size = i+1; if(found_size > size) { //split. for(;;) { size_t new_size = size - 1; //buddy. Node* n = node_new(new_size, iter->offset + 1<<new_size); insert_head(&(thiz->free_list[new_size-1]), n); if(new_size == size) { Node* nn = node_new(new_size, iter->offset); insert_head(&(thiz->alloc_list[new_size-1]), nn); break; } } } else { Node* nnn = node_new(iter->size, iter->offset); insert_head(&(thiz->alloc_list[size-1]), nnn); } *offset = iter->offset; return RET_OK; }
END_TEST START_TEST(test_node_free) { node *n = node_new("a", "a"); node *l = node_new("b", "b"); node_insert(n, l); ck_assert_ptr_eq(l, n->right); node_free(n); // @TODO vérifier que n et l sont désalloués }
/* * Find a node by the name under root or create it. */ static nodePtr theoldreader_source_find_or_create_folder (const gchar *name, nodePtr root) { nodePtr folder = NULL; GSList *iter_parent; /* find a node by the name under root */ iter_parent = root->children; while (iter_parent) { if (g_str_equal (name, node_get_title (iter_parent->data))) { folder = (nodePtr)iter_parent->data; break; } iter_parent = g_slist_next (iter_parent); } /* if not found, create new folder */ if (!folder) { folder = node_new (folder_get_node_type ()); node_set_title (folder, name); node_set_parent (folder, root, -1); feedlist_node_imported (folder); subscription_update (folder->subscription, FEED_REQ_RESET_TITLE | FEED_REQ_PRIORITY_HIGH); } return folder; }
struct node *node_add(struct node *n, int value) { if ( NULL == n ) return node_new(value); else { n->next = node_add(n->next, value); return n; } }
//insere um elemento à cabeça da lista bool list_insert(LinkedList *list, int value){ Node *node; node = node_new(); node -> elem = value; if(list -> head == NULL){ // se a lista estiver vazia a cabeca e a cauda passam a ser // o nó com o elemento que se prentende inserir list -> head = node; list -> tail = node; list -> size++; return true; } else{ // insere no inicio da lista, a cabeça passa a ser o novo no list -> head -> prev = node; node -> next = list -> head; list -> head = node; list -> size++; return true; } return false; }
struct Node * node_number_new(double value) { struct NodeNumber *self = node_new(NODE_NUMBER, 0); self->value = value; return NODE(self); }
static void* save_state_cmd (int argc, char **argv, void *data) { Node *pos = (Node *) data; Node *i; Node *j; if (savedtree != NULL) { tree_free (savedtree); } savedtree = node_new (); i = node_root (pos); j = savedtree; do { j = savedtree = tree_duplicate (i, j); i = node_down (i); j = node_insert_down (j); } while (i != NULL); j = node_remove (j); { int no; no = node_no (pos); savedtree = node_root (savedtree); while (--no) savedtree = node_recurse (savedtree); } return pos; }
// insere no fim da lista bool list_insertEnd(LinkedList *list, int value){ Node *node, *tmp; node = node_new(); node -> elem = value; // coloca a cabeca num nó temporario tmp = list -> head; if(list -> head == NULL){ // se a lista estiver vazia a cabeca passa a ser o elemento que se prentende inserir list -> head = node; list -> head -> next = NULL; list -> size++; return true; } else{ // percorre a lista ate ao fim. while(tmp -> next != NULL) tmp = tmp -> next; // insere no fim da lista node -> next = NULL; tmp -> next = node; list -> size++; return true; } return false; }
Node * node_add_new(Node * node, const char * tag) { Node *n = node_new(tag); nodes_append(node, n); n->parent = node; return n; }
bool list_prepend(list_t *l, void *ptr) { node_t *n; LIST_CHECK(l); n = node_new(ptr); if (!n) return false; n->prev = NULL; n->next = l->first; if (l->first) { l->first->prev = n; } if (!l->last) { l->last = n; } l->first = n; l->length++; LIST_CHECK(l); return true; }
bool rbtree_put(RBTree *tree, const void *key, const void *value) { RBNode *y = tree->root; RBNode *x = tree->root->left; while (x != tree->nil) { y = x; int cmp = tree->key_compare(key, x->key); if (cmp == 0) { tree->value_destroy(x->value); x->value = tree->value_copy(value); return true; } x = (cmp < 0) ? x->left : x->right; } RBNode *z = node_new(tree, y, true, key, value); if (y == tree->root || tree->key_compare(z->key, y->key) < 0) { y->left = z; } else { y->right = z; } put_fix(tree, z); tree->size++; return false; }
/** * Add "broadcast-friends" to the list of subscriptions if required */ static void google_source_add_broadcast_subscription (GoogleSourcePtr gsource) { const gchar* title = "Friend's Shared Items"; GSList * iter = NULL; nodePtr node; iter = gsource->root->children; while (iter) { node = (nodePtr) iter->data ; if (!node->subscription || !node->subscription->source) continue; if (g_str_equal (node->subscription->source, GOOGLE_READER_BROADCAST_FRIENDS_URL)) { return; } iter = g_slist_next (iter); } /* aha! add it! */ node = node_new (feed_get_node_type ()); node_set_title (node, title); node_set_data (node, feed_new ()); node_set_subscription (node, subscription_new (GOOGLE_READER_BROADCAST_FRIENDS_URL, NULL, NULL)); node->subscription->type = &googleSourceFeedSubscriptionType; node_set_parent (node, gsource->root, -1); feedlist_node_imported (node); subscription_update (node->subscription, FEED_REQ_RESET_TITLE | FEED_REQ_PRIORITY_HIGH); subscription_update_favicon (node->subscription); }
int wb_tree_probe(wb_tree *tree, void *key, void **dat) { int rv = 0; wb_node *node, *parent = NULL; float wbal; ASSERT(tree != NULL); node = tree->root; while (node) { rv = tree->key_cmp(key, node->key); if (rv < 0) parent = node, node = node->llink; else if (rv > 0) parent = node, node = node->rlink; else { *dat = node->dat; return 0; } } if ((node = node_new(key, *dat)) == NULL) return -1; if ((node->parent = parent) == NULL) { ASSERT(tree->count == 0); tree->root = node; tree->count = 1; return 0; } if (rv < 0) parent->llink = node; else parent->rlink = node; while ((node = parent) != NULL) { parent = node->parent; node->weight++; wbal = WEIGHT(node->llink) / (float)node->weight; if (wbal < ALPHA_0) { wbal = WEIGHT(node->rlink->llink) / (float)node->rlink->weight; if (wbal < ALPHA_3) { rot_left(tree, node); } else { rot_right(tree, node->rlink); rot_left(tree, node); } } else if (wbal > ALPHA_1) { wbal = WEIGHT(node->llink->llink) / (float)node->llink->weight; if (wbal > ALPHA_2) { rot_right(tree, node); } else { rot_left(tree, node->llink); rot_right(tree, node); } } } tree->count++; return 1; }
Node * node_store_get_or_create_node (NodeStore *self, SheetPos pos) { Node *node; g_return_val_if_fail (self != NULL, NULL); g_return_val_if_fail (IS_NODE_STORE (self), NULL); node = g_hash_table_lookup (self->nodes, &pos); if (!node) { // Create a node at (x, y) and return it. node = node_new (pos); g_signal_connect_object (G_OBJECT (node), "node_dot_added", G_CALLBACK (node_dot_added_callback), G_OBJECT (self), 0); g_signal_connect_object (G_OBJECT (node), "node_dot_removed", G_CALLBACK (node_dot_removed_callback), G_OBJECT (self), 0); g_hash_table_insert (self->nodes, &node->key, node); } // If there was a previously stored node here, just // return that node. return node; }
Node * Text(const char *text) { Node *n = node_new(""); n->text = strdup(text); return n; }
Node* node_new_inherit(int type, Node* ancestor) { Node* node = node_new(type); node->filename = ancestor->filename; node->line = ancestor->line; return node; }
/** * Inserts a new node to the list before the current position. The nodes * payload pointer will be set to ``ptr''. * * @param iter a valid list_iter. * @param ptr the payload pointer for the new node. * @return false if the item could not be prepended. Returns true on success. */ bool list_iter_prepend(list_iter_t *iter, void *ptr) { node_t *n; LIST_ITER_CHECK(iter); /* Items must not be prepended to removed items! */ RUNTIME_ASSERT(&iter->removed != iter->node); n = node_new(ptr); if (!n) return false; n->prev = iter->node ? iter->node->prev : NULL; n->next = iter->node; if (!iter->l->last) iter->l->last = n; if (iter->l->first == iter->node) iter->l->first = n; if (iter->node) iter->node->prev = n; iter->stamp++; iter->l->stamp++; iter->l->length++; LIST_CHECK(iter->l); LIST_ITER_CHECK(iter); return true; }
int queue_insert_in_order(queue_t queue, PFany comp, void * item){ node_t node; node_t new_node = node_new(item); if (queue) { if (!queue->head || comp(queue->head->datum,item) >= 0) { new_node->next_node = queue->head; queue->head = new_node; queue->size+=1; return 0; } node = queue->head; while(node) { if(comp(node->datum,item) == 0) { new_node->next_node = node->next_node; node->next_node = new_node; queue->size+=1; return 0; } else if (comp(node->datum,item) < 0 && !node->next_node){ node->next_node = new_node; new_node->next_node = NULL; queue->size+=1; return 0; } else if (comp(node->datum,item) < 0 && comp(node->next_node->datum, item) >= 0) { new_node->next_node = node->next_node; node->next_node = new_node; queue->size+=1; return 0; break; } else { node =node->next_node; } } } return 0; }
node* get_P(){ node* val = node_new(); if (syntax_errno) return 0; if (*cur_c == '(') { ++cur_c; val = get_E(); if (*cur_c != ')') { syntax_errno = cur_c; return 0; } ++cur_c; return val; } else { val = get_N(); return val; } return 0; }