void test_gc_with_cycle() { Node *a, *b; char *result, *expected; GC_SAVE_RP; gc(); a = new_Node("a", NULL); b = new_Node("b", a); a->neighbor = b; ADD_ROOT(a); ADD_ROOT(b); ASSERT(align(2 * Node_type.size), heap_allocated()); gc(); expected = "heap2[64,1000]\n" "0000:Node[32]->[32]\n" "0032:Node[32]->[0]\n"; result = calloc(1000, sizeof(char)); heap_dump(result); ASSERT_STR(expected, result); // printf("%s\n", result); free(result); ASSERT(align(2 * Node_type.size), heap_allocated()); ASSERT_STR("a", b->neighbor->payload); ASSERT_STR("b", a->neighbor->payload); GC_RESTORE_RP; }
void list_add(List list, Item item) { if (list->head == NULL) { // list is empty list->head = new_Node(item, NULL); list->tail = list->head; } else { // list is not empty list->tail->next = new_Node(item, NULL); list->tail = list->tail->next; } list->size++; }
/* create and insert a node having (*key_p) and (*item_p) to (*list_p) */ void insert_List (List* list_p, int* key_p, void* item_p) { Node *node; Node *p; if (list_p -> len == 0){ node = new_Node(NULL,NULL,key_p,item_p); list_p -> head = node; list_p -> tail = node; list_p -> mid = node; } else{ if ((*list_p -> head -> key > *key_p) || (*list_p -> tail -> key < *key_p)){ if (*list_p -> head -> key > *key_p){ node = new_Node(NULL, list_p -> head, key_p, item_p); list_p -> head -> prev = node; list_p -> head = node; } else{ node = new_Node(list_p -> tail, NULL, key_p, item_p); list_p -> tail -> next = node; list_p -> tail = node; } } else{ if (!(*list_p -> mid -> key < *key_p && *list_p -> mid -> next -> key > *key_p)){ p = search_List(list_p, *key_p); node = new_Node(p, p -> next, key_p, item_p); node -> next -> prev = node; node -> prev -> next = node; } else{ node = new_Node(list_p -> mid, list_p -> mid -> next, key_p, item_p); node -> next -> prev = node; node -> prev -> next = node; } } } list_p -> len ++; if (list_p -> len%2 == 1){ if (*list_p -> mid -> key < *key_p) list_p -> mid = list_p -> mid -> next; } else{ if (*list_p -> mid -> key > *key_p) list_p -> mid = list_p -> mid -> prev; } }
void list_insert(List list, Item item, int index) { if (index > list->size) { // index exeeds list size return; } int i = 0; for (link x = list->head; x != NULL; x = x->next) { if (i == index) { x->next = new_Node(x->item, x->next); x->item = item; list->size++; return; } else { i++; } } if (i == list->size) { // Case of insertion at first position after last element list->tail->next = new_Node(item, NULL); list->tail = list->tail->next; list->size++; } }
/* Queries the test database for actors and movies and prints out the results from the query */ void testQuery() { mongo conn[1]; int status = mongo_client( conn, "127.0.0.1", 27017 ); Node *actor_node = new_Node(); Node *movie_node = new_Node(); actorNode(actor_node, "Shaq", conn); movieNode(movie_node, "Star Wars", conn); printf("actor name: %s actor type: %s number children:%d\n", actor_node->name, actor_node->type, actor_node->numberChildren); for (int i=0; i<actor_node->numberChildren; i++) { printf("actor child: %s\n", actor_node->children[i]); } printf("movie name: %s movie type: %s movie children:%d\n", movie_node->name, movie_node->type, movie_node->numberChildren); for (int i=0; i<movie_node->numberChildren; i++) { printf("movie child: %s\n", movie_node->children[i]); } }
/* Maze Constructor */ Maze * new_Maze () { Maze * this_maze; short i, j; this_maze = (Maze *) malloc(sizeof(Maze)); /* Allocate a new Node for each coord of maze */ for (i = 0; i < SIZE; ++i) for (j = 0; j < SIZE; ++j) MAPIJ = new_Node (i, j); /* setting the neighbors ptrs... must be done after all cells allocated */ for (i = 0; i < SIZE; i++) for (j = 0; j < SIZE; j++) { MAPIJ->left = (j == 0) ? NULL : (this_maze->map[i][j-1]); MAPIJ->right = (j == SIZE-1) ? NULL : (this_maze->map[i][j+1]); MAPIJ->up = (i == 0) ? NULL : (this_maze->map[i-1][j]); MAPIJ->down = (i == SIZE-1) ? NULL : (this_maze->map[i+1][j]); } return this_maze; }
//return root Node* insert(Node *node, int key){ if(node == NULL) return new_Node(key); if(key < node->key){ node->left = insert(node->left, key); } else if(key > node->key) { node->right = insert(node->right, key); } node->h = max(h(node->left), h(node->right))+1; int balance = getBalance(node); //left left if(balance > 1 && key < node->left->key){ return rightRotate(node); } //right right if(balance < -1 && key > node->right->key){ return leftRotate(node); } //left right if(balance > 1 && key > node->left->key){ node->left = leftRotate(node->left); return rightRotate(node); } //right left if(balance < -1 && key < node->right->key){ node->right = rightRotate(node->right); return leftRotate(node); } return node; }
node insert(node n, int e) { if (!n) return new_Node(e); if (e < n->element) n->left = insert(n->left, e); else n->right = insert(n->right, e); n->height = max(height(n->left), height(n->right)) + 1; int balance = get_balance(n); // Left-Left case if (balance > 1 && e < n->left->element) return right_rotate(n); // Left-Right case if (balance > 1 && e > n->left->element) { n->left = left_rotate(n->left); return right_rotate(n); } // Right-Right case if (balance < -1 && e > n->right->element) return left_rotate(n); // Right-Left case if (balance < -1 && e < n->right->element) { n->right = right_rotate(n->right); return left_rotate(n); } return n; }