//! <b>Requires</b>: this_node and other_node must be nodes inserted //! in circular lists or be empty circular lists. //! //! <b>Effects</b>: Swaps the position of the nodes: this_node is inserted in //! other_nodes position in the second circular list and the other_node is inserted //! in this_node's position in the first circular list. //! //! <b>Complexity</b>: Linear to number of elements of both lists //! //! <b>Throws</b>: Nothing. static void swap_nodes(node_ptr this_node, node_ptr other_node) { if (other_node == this_node) return; bool this_inited = base_t::inited(this_node); bool other_inited = base_t::inited(other_node); if(this_inited){ base_t::init_header(this_node); } if(other_inited){ base_t::init_header(other_node); } bool empty1 = base_t::unique(this_node); bool empty2 = base_t::unique(other_node); node_ptr prev_this (get_previous_node(this_node)); node_ptr prev_other(get_previous_node(other_node)); node_ptr this_next (NodeTraits::get_next(this_node)); node_ptr other_next(NodeTraits::get_next(other_node)); NodeTraits::set_next(this_node, other_next); NodeTraits::set_next(other_node, this_next); NodeTraits::set_next(empty1 ? other_node : prev_this, other_node); NodeTraits::set_next(empty2 ? this_node : prev_other, this_node); if(this_inited){ base_t::init(other_node); } if(other_inited){ base_t::init(this_node); } }
void move_head(uint8_t new_dir) { if (new_dir) { //Copy head to new position head = get_next_node(head); //increment head corners[head].x = corners[get_previous_node(head)].x; corners[head].y = corners[get_previous_node(head)].y; change_direction(); //change direction } //Have we left the game board? if ((corners[head].x == 0) && (dirX == -1)) { game_over(); return; } if ((corners[head].y == 0) && (dirY == -1)) { game_over(); return; } if ((corners[head].x == GAMEBOARD_X-1) && (dirX == 1)) { game_over(); return; } if ((corners[head].y == GAMEBOARD_Y-1) && (dirY == 1)) { game_over(); return; } corners[head].x += dirX; corners[head].y += dirY; ++snake_length_current; }
//! <b>Requires</b>: this_node and other_node must be nodes inserted //! in circular lists or be empty circular lists. //! //! <b>Effects</b>: Swaps the position of the nodes: this_node is inserted in //! other_nodes position in the second circular list and the other_node is inserted //! in this_node's position in the first circular list. //! //! <b>Complexity</b>: Linear to number of elements of both lists //! //! <b>Throws</b>: Nothing. static void swap_nodes(const node_ptr & this_node, const node_ptr & other_node) { if (other_node == this_node) return; const node_ptr this_next = NodeTraits::get_next(this_node); const node_ptr other_next = NodeTraits::get_next(other_node); const bool this_null = !this_next; const bool other_null = !other_next; const bool this_empty = this_next == this_node; const bool other_empty = other_next == other_node; if(!(other_null || other_empty)){ NodeTraits::set_next(this_next == other_node ? other_node : get_previous_node(other_node), this_node ); } if(!(this_null | this_empty)){ NodeTraits::set_next(other_next == this_node ? this_node : get_previous_node(this_node), other_node ); } NodeTraits::set_next(this_node, other_empty ? this_node : (other_next == this_node ? other_node : other_next) ); NodeTraits::set_next(other_node, this_empty ? other_node : (this_next == other_node ? this_node : this_next ) ); }
//! <b>Requires</b>: nxt_node must be a node of a circular list. //! //! <b>Effects</b>: Links this_node before nxt_node in the circular list. //! //! <b>Complexity</b>: Linear to the number of elements in the circular list. //! //! <b>Throws</b>: Nothing. static void link_before (node_ptr nxt_node, node_ptr this_node) { base_t::link_after(get_previous_node(nxt_node), this_node); }
//! <b>Requires</b>: this_node must be in a circular list, be an empty circular list or be inited. //! //! <b>Effects</b>: Unlinks the node from the circular list. //! //! <b>Complexity</b>: Linear to the number of elements in the circular list //! //! <b>Throws</b>: Nothing. static void unlink(node_ptr this_node) { if(NodeTraits::get_next(this_node)) base_t::unlink_after(get_previous_node(this_node)); }
main(int argc ,char *argv[]) { int i; /*checking for creation and printing the tree ---Working*/ node *tree ; tree=create_tree(); read_ssf_from_file(tree, argv[1]); print_tree(tree); printf("Checked read and print\n\n"); /*checking for get_nth_child ----Working*/ node *child = get_nth_child(tree, 2); print_node_without_index(child); printf("Checked get_nth_child\n\n"); /*checking for getchildren-----Working*/ list_of_nodes *l1; l1=getchildren(tree); printf("\n\nsize=%d\n", l1->size); for (i=0; i< l1->size; i++) { print_attr_of_node(l1->l[i] ); printf("\n"); } printf("Checked getchildren\n\n"); /*checking for getleaves----Working*/ list_of_nodes *l2; l2=getleaves(tree); printf("\n\nsize=%d\n", l2->size); for (i=0; i< l2->size; i++) { print_attr_of_node(l2->l[i] ); printf("\n"); } printf("Checked getleaves\n\n"); /*checking for getleaves_child---Working*/ list_of_nodes *l3; l3=getleaves_child(l1->l[1]); printf("\n\nsize=%d\n", l3->size); for (i=0; i< l3->size; i++) { print_attr_of_node(l3->l[i] ); printf("\n"); } printf("Checked getleaves_child\n\n"); /*checking for get_nodes---Working*/ list_of_nodes *l4; l4=get_nodes(2,"NNS",tree); printf("\n\nsize=%d\n",l4->size); for (i=0; i< l4->size; i++) { print_attr_of_node(l4->l[i]); printf("\n"); } printf("Checked get_nodes\n\n"); /*checking for get_pattern---Working*/ list_of_nodes *l5; l5=get_pattern(2,".*N.*",tree); printf("\n\nsize=%d\n",l5->size); for (i=0; i< l5->size; i++) { print_attr_of_node(l5->l[i]); printf("\n"); } printf("Checked get_pattern\n\n"); //checking for delete_node------Working //printf("%d\n",delete_node(l5->l[1])); //print_tree(tree); //checking for count_leaf_nodes ----Working printf("count of leaf nodes----%d\n\n", count_leaf_nodes(tree)); //checking for get_field(s) ----Working char *str; str=get_field(l5->l[1],1); printf("%s\n",str); str=get_fields(l5->l[1]); printf("%s\n", str); printf("Checked get_field and get_fields\n\n"); //checking for get_next_node and get_previos_node----Working node *N; N=get_next_node(l1->l[1]); str=get_fields(N); printf("%s\n", str); N=get_previous_node(l1->l[1]); str=get_fields(N); printf("%s\n", str); printf("Checked get_next_node & get_previous_node\n\n"); //checking for insert_node_position------Working node *M; M=create_node_with_attr("iiit","NNP","<loc=hyd>",NULL); insert_node_into_position(tree,M,1); print_tree(tree); printf("Checked insert_node_position\n\n"); /*Checkin print_attr_of_or_node--------Working*/ print_attr_of_or_node(M->OR); printf("\nChecked print_attr_of_or_node\n\n"); }
//! <b>Requires</b>: nxt_node must be a node of a circular list. //! //! <b>Effects</b>: Links this_node before nxt_node in the circular list. //! //! <b>Complexity</b>: Linear to the number of elements in the circular list. //! //! <b>Throws</b>: Nothing. static void link_before (const node_ptr & nxt_node, const node_ptr & this_node) { base_t::link_after(get_previous_node(nxt_node), this_node); }