/** * Puts a new node containing i at the appropriate position in a list * sorted in ascending order. */ void insert_sorted(int i) { // initalize new node node* new_node = create_node(); // set new node's i value new_node->i = i; // set new node's next value to NULL new_node->next = NULL; // check if list is empty if (first == NULL) first = new_node; // then check if i belongs at beginning of list else if (new_node->i < first->i) { new_node->next = first; first = new_node; } // else check if i belongs at end or middle of the list else { // initialize pred_node with same value's as first node's node* pred_node = first; while (true) { // check for insertion at end of list if (pred_node->next == NULL) { pred_node->next = new_node; break; } // check for insertion in the middle of the list else if (pred_node->next->i > new_node->i) { new_node->next = pred_node->next; pred_node->next = new_node; break; } // in each loop, set pred_node to the next node in the linked list pred_node = pred_node->next; } } }
void LinkedList::add_at_n (int data,int n) { node *temp = create_node(data); node *temp2 = head; for(int i=0; i< n-2;i++) { temp2 = (*temp2).link; } node* temp3 = (*temp2).link; (*temp2).link = temp; (*temp).link = temp3; }
/** push_back * * Adds the data to the back/end of the linked list * * @param llist a pointer to the list. * @param data pointer to data the user wants to store in the list. */ void push_back(list* llist, void* data) { node *newTail = create_node(data); if (llist->size == 0) { llist->head = newTail; llist->tail = newTail; } else { node *oldTail = llist->tail; oldTail->next = newTail; llist->tail = newTail; newTail->prev = oldTail; } llist->size++; }
int main(int argc,char* argv[]) { struct node *first = NULL; struct node *last = NULL; struct node *p = NULL; if ( (first = create_node()) == NULL ) { return 0; } if ( (first = add_to_node( first, 0)) == NULL ) { printf("add false!\n"); } last = first; if ( (first = add_to_node( first, 0)) == NULL ) { printf("add false!\n"); } if ( (first = add_to_node( first, 1)) == NULL ) { printf("add false!\n"); } if ( (first = add_to_node( first, 2)) == NULL ) { printf("add false!\n"); } if ( (first = add_to_node( first, 3)) == NULL ) { printf("add false!\n"); } first = delete_node(first); show(first); printf("\b\n"); show(last); printf("\b\n"); return 0; }
//creates a bst with the minimum height bst* createMinBST(int a[], int start, int end){ if (start > end) return NULL; int middle = (start+end)/2; bst* root = create_node(a[middle]); root->left = createMinBST(a, start, middle-1); root->right = createMinBST(a, middle + 1, end); return root; }
t_cmd *create_last_node(char *str, int **occ) { t_cmd *node; int cmd_len; node = NULL; if (str) { cmd_len = ft_strlen(str); node = create_node(str, NULL); (*occ)++; } return (node); }
/** * Puts a new node containing i at the front (head) of the list. */ void prepend(int i) { // initalize new node node* new_node = create_node(); // set new node's i value new_node->i = i; // set new node's next pointer to first node in list new_node->next = first; // new node now becomes first/front/head node in list first = new_node; }
Node * merge_two(Node * head) {//merge the last two nodes in the list into one tree node if (head -> next -> next == NULL) {//we are at the left child to merge, head -> next is the right child Node * newParent = create_node(0); newParent -> left = head; newParent -> right = head -> next; newParent -> left -> next = NULL; return (newParent); } //head -> next becomes the new parent head -> next = merge_two(head -> next); return (head); }
void init(const char *s) { int i; int freq[128] = {0}; char cxt[16]; while (*s) { freq[(int)*s]++; s++; } for (i = 0; i < 128; i++) { if (freq[i]) { insert(create_node(freq[i], i, 0, 0)); } } while (tail > 2) { insert(create_node(0, 0, pop(), pop())); } prepare(tree[1], cxt, 0); }
errcode_t setup_tree(root_t **roots, const char *word) { root_t *root; node_t *p; int len, i, idx; char c; len = strlen(word); assert(roots && word && len > 0); if ((c = to_lowercase(word[0])) < 0) { return 0; } idx = c - 'a'; root = roots[idx]; for (i = 1, p = root->n; i < len; i++) { /* Illegal word, skip it, resulting in leaf node's cnt == 0 */ if ((c = to_lowercase(word[i])) < 0) { return 0; } idx = c - 'a'; if (p->children[idx] != NULL) { p = p->children[idx]; continue; } pthread_mutex_lock(&root->mutex); if (!p->children[idx]) { if (!(p->children[idx] = create_node(c))) { pthread_mutex_unlock(&root->mutex); return ERR_NO_MEM; } } pthread_mutex_unlock(&root->mutex); p = p->children[idx]; } /* update counter on the leaf node */ pthread_mutex_lock(&root->mutex); p->cnt++; pthread_mutex_unlock(&root->mutex); return 0; }
/** * Puts a new node containing i at the end (tail) of the list. */ void append(int i) { // initalize new node node* new_node = create_node(); // set new node's i value new_node->i = i; // set new node's next value to NULL new_node->next = NULL; // insert node immediately following previous node insert_node(new_node); }
Node* Parser::parse_expression() { // expects TOKEN_INTEGER, TOKEN_WORD or TOKEN_OPENING_PAREN as current token Node *node = create_node(NODE_EXPRESSION); if (_current->kind == TOKEN_INTEGER) { Node *node_const = create_node(NODE_CONSTANT); node_const->constant_number = _current->integer; node->next.push_back(node_const); } else if (_current->kind == TOKEN_WORD) { Node *node_word = create_node(NODE_WORD); node_word->word_word = _current->word; node->next.push_back(node_word); } else if (_current->kind == TOKEN_OPENING_PAREN) { node->next.push_back(parse_function_call()); } else { printf("Error: unknown expression: "); _current->print(); } next_token(); return node; }
/** push_back * * Adds the data to the back/end of the linked list * * @param llist a pointer to the list. * @param data pointer to data the user wants to store in the list. */ void push_back(list* llist, void* data) { node* add_node = create_node(data); if(llist->head == NULL && llist->tail == NULL){ llist->head = add_node; llist->tail = add_node; } else{ add_node->prev = llist->tail; llist->tail->next = add_node; llist->tail = add_node; } llist->size += 1; }
Node * addNode(Node *node, int value){ if(node == NULL){ return create_node(value); } else{ if (node->value > value){ node->left = addNode(node->left, value); } else{ node->right = addNode(node->right, value); } } return node; }
void add_iterative(struct linkedlist* l, int x) //@ requires list(l, ?vs); //@ ensures list(l, append(vs, cons(x, nil))); { //@ open list(l, vs); if(l->head == 0) { struct node* n = create_node(0, x); l->head = n; //@ open lseg(0, 0, _); //@ close lseg(0, 0, _); //@ close lseg(n, 0, cons(x, nil)); //@ close list(l, append(vs, cons(x, nil))); } else { struct node* head = l->head; struct node* current = l->head; //@ close lseg(head, head, nil); //@ open lseg(head, 0, vs); while(current->next != 0) //@ invariant current!= 0 &*& lseg(head, current, ?vs1) &*& current->value |-> ?v &*& current->next |-> ?n &*& malloc_block_node(current) &*& lseg(n, 0, ?vs2) &*& vs == append(vs1, cons(v, vs2)); { //@ open lseg(n, 0, _); struct node* oldcurrent = current; current = current->next; //@ appendlemma(head, oldcurrent); //@ appendlemma2(vs1, v, vs2); } //@ open lseg(0, 0, _); struct node* nn = create_node(0, x); current->next = nn; //@ close lseg(0, 0, nil); //@ close lseg(nn, 0, _); //@ close lseg(current, 0, _); //@ appendlemma3(head, current); //@ appendlemma2(vs1, v, cons(x, nil)); //@ close list(l, append(vs, cons(x, nil))); } }
node *unbalanced_binary_tree_2() { node *node_pointer = create_node(); node_pointer->left = create_node(); node_pointer->right = create_node(); node_pointer->right->left = create_node(); node_pointer->right->left->left = create_node(); node_pointer->right->right = create_node(); return node_pointer; }
int dictionary_insert(struct dictionary *dict, const wchar_t *word) { assert(dict != NULL); struct dictionary *node = NULL; /* Pierwsze slowo */ if (dict->children_size == 0) { for(node = dict; *word; node = *node->children) { put_child(node, create_node(*word)); word++; } put_child(node, create_node(NULL_MARKER)); return 1; } node = dict; struct dictionary *found = NULL; while (find_child(node, &found, *word) && *word) { node = found; word++; } if (*word == L'\0') { if (find_child(node, &found, NULL_MARKER)) return 0; put_child(node, create_node(NULL_MARKER)); } else { struct dictionary *tmp = create_node(*word); put_child(node, tmp); dictionary_insert(tmp, ++word); } return 1; }
void CGI_write_zboco(double parent_id, cgns_zboco *zboco) { int n; if (zboco->link && keep_links) { create_node (zboco->id, parent_id, 0); return; } /* ZoneBC_t */ if (cgi_new_node(parent_id, "ZoneBC", "ZoneBC_t", &zboco->id, "MT", 0, 0, 0)) error_exit (NULL, 0); /* BC_t */ for (n=0; n<zboco->nbocos; n++) CGI_write_boco (zboco->id, &zboco->boco[n]); /* Descriptor_t */ for (n=0; n<zboco->ndescr; n++) create_node (zboco->descr[n].id, zboco->id, 1); /* ReferenceState_t */ if (zboco->state) create_node (zboco->state->id, zboco->id, 1); /* DataClass_t */ if (zboco->data_class && cgi_write_dataclass (zboco->id, zboco->data_class)) error_exit (NULL, 0); /* DimensionalUnits_t */ if (zboco->units) create_node (zboco->units->id, zboco->id, 1); if (keep_nodes || FileVersion >= 2100) { /* UserDefinedData_t */ for (n=0; n<zboco->nuser_data; n++) create_node (zboco->user_data[n].id, zboco->id, 1); } }
/* * Insert an item to the tree. * Return 0 if a new node was created, 1 if a node was updated, or -1 on error. */ int mtree_trie_insert(struct mtree_trie *trie, const char *key, void *item) { struct mtree_trie *u; struct mtree_trie *node; size_t bit; assert(trie != NULL); assert(item != NULL); assert(key != NULL && *key != '\0'); u = find_node(trie, key); if (u != NULL && strcmp(u->key, key) == 0) { /* * Already in the trie, just update the item. */ u->item = item; return (1); } if ((node = create_node(key, item)) == NULL) return (-1); if (u == NULL) { /* * Trie is empty, insert to the left under the head. */ for (bit = 0; KEY_BIT(key, node->key_len, bit) == 0; bit++) ; node->bit = bit; node->left = trie; node->right = node; trie->left = node; } else { for (bit = 0;; bit++) { int kbit = KEY_BIT(key, node->key_len, bit); if (kbit != KEY_BIT(u->key, u->key_len, bit)) { if (kbit == 0) node->right = NULL; else node->left = NULL; node->bit = bit; break; } } trie->left = insert_node(trie->left, node, trie); } return (0); }
int main() { struct Tnode *root = NULL, *node; int key , ival, *sum, ssum = 0; sum = &ssum; do { printf("\n[1] Insert value"); printf("\n[2] Left Branch Sum "); printf("\n[3] Right Branch Sum "); printf("\n[4] Exit "); printf("\nEnter the input key : "); scanf("%d", &key); switch (key) { case 1: printf("\n Enter the value to be inserted :"); scanf("%d", &ival); if (create_node(ival, &node)) insert_node(&root, &node); else printf("\n Memory is Full !! "); break; case 2: *sum = 0; if (root != NULL) { branchsum(&(root->left), &sum); printf("\n Left Branch Sum = %d", *sum); } else printf("\n Enter some value in tree"); break; case 3: *sum = 0; if (root != NULL) { branchsum(&(root->right), &sum); printf("\n Right Branch Sum = %d", *sum); } else printf("\n Enter some value in tree"); break; case 4: break; default: printf("\t Enter the Correct key !!!!! "); break; } } while (key != 4); printf("\n Thank you for using codingstreet.com 's datastructure solution "); return 0; }
/* * We're forced to use a dirty hack here, due to Theora's idiotic API * Theora needs three separate pieces of data, called headers to initialize * its internal decoder structure. After all three pieces have been received, * we can call theora_decode_init. * We use a counter and a flag to make sure we have decoded our three headers and then * we call theora_decode_init so we can initialize a theora_state structure. * We use the ts structure to convert a granule position into an actual timestamp. * There are many ways in which this can fail, but we rely on having all three headers * at the beginning of the ogg video bitstream. * * To whoever came up with this convoluted scheme: please consider a change of careers. */ int read_theora_cb(OGGZ *oggz, ogg_packet *op, long serialno, void *data) { struct op_node *node; struct theora_headers *th; long timestamp = 0; //mylog("Got theora packet, serialno=%d, size=%d, packetno=%lld, granulepos=%lld\n", // serialno, op->bytes, op->packetno, op->granulepos); th = (struct theora_headers *)video_stream->data; if ( theora_packet_isheader(op) ) { theora_decode_header(&(th->ti), &(th->tc), op); th->header_count++; } if ( th->header_count >= 3 && !th->have_headers ) { theora_decode_init(&(th->ts), &(th->ti)); th->have_headers = 1; } if ( th->have_headers ) { double d; d = theora_granule_time(&(th->ts), op->granulepos); timestamp = (long)(d * 1000); } if ( timestamp < 0 ) { timestamp = video_stream->page_ts + video_stream->page_count * THEORA_FRAME_DURATION; video_stream->page_count++; } else { video_stream->page_ts = timestamp; video_stream->page_count = 0; } if ( !theora_packet_isheader(op) ) { node = create_node(op, serialno, timestamp); append_node(video_stream, node); } return 0; }
// Add an item to the list at a specified location int insert (list* l, void* item, int index) { assert (l != NULL); int valid = index >= 0 && index <= size(l); if (valid) { // Go ahead and create the node node* temp = create_node (item); node* n = l -> front; // If this is the first item to be inserted, the logic is simple if (size(l) == 0) { temp -> next = temp; temp -> prev = temp; l -> front = temp; l -> rear = temp; } else if (size(l) == 1) { // If there's only one other item, logic is still simple n -> prev = temp; // Point n.prev to temp n -> next = temp; // Point n.next to temp temp -> prev = n; // Point temp.prev to n temp -> next = n; // Point temp.prev to n if (index) { // This will trigger if we're appending l -> front = temp; l -> rear = n; } else { // And this will trigger when we're prepending l -> front = n; l -> rear = temp; } } else { // Now for the general case // Traverse to the index we're wanting to get to int i = 0; for (i = 0; i < index - 1; i++) { n = n -> next; } // n points to the node prior to the location we want to insert. node* a = n -> next; // Make pointer to temp.next temp -> next = n -> next; // Point temp.next at what n.next is pointing to temp -> prev = n; // Point temp.prev at n n -> next = temp; // Point n.next at temp a -> prev = temp; // Point a -> prev to temp // If we want to prepend or append, we'll need to take care of one of the l pointers if(index == size(l)) { // We're appending l -> front = temp; } else if(index == 0) { // We're prepending l -> rear = temp; } } // Lastly, bump the size of l l -> size++; } return valid; }
int insert_node(tree *pTree, char *words) { node *search_result = NULL; node *new_node = NULL; new_node = (node *)create_node(words); if(pTree->root == NULL) { pTree->root = new_node; pTree->nodeNum = 1; pTree->depth = 1; return 1; } search_result = search_node_by_content(pTree, words); if(!strcmp(search_result->words, words)) { search_result->wordsNum++; } else { if(search_result != NULL && search_result->left == NULL \ && search_result->right == NULL) { bind_node(search_result, new_node); pTree->nodeNum++; } else if((search_result != NULL) && (search_result->left != NULL)) { search_result->right = new_node; new_node->parent = search_result; //new_node->row = search_result->row + 1; //new_node->col = 2 * search_result->col; pTree->nodeNum++; } else if((search_result != NULL) && (search_result->right != NULL)) { search_result->left = new_node; new_node->parent = search_result; //new_node->row = search_result->row + 1; //new_node->col = (2 * search_result->col - 1); pTree->nodeNum++; } } }
X_OVR void insert(const key_type& key) { if (NULL == this->root()) { set_root(create_node(key, NULL)); return; } Node* node = this->root(); while (true) { if (key < node->key) // Pass to the left { if (NULL == node->left) // Found slot { node->left = create_node(key, node); break; } else node = node->left; // Keep going } else if (key > node->key) // Pass to the right { if (NULL == node->right) // Found slot { node->right = create_node(key, node); break; } else node = node->right; // Keep going } else // Equal to current node, skip inserting { break; } } }
//Driver program int main(){ int i; for(i=1; i<=NUM_NODE; i++){ graph[i] = create_node(i); } add_edge_1(1,2); add_edge_1(2,3); add_edge_1(3,4); add_edge_1(5,6); for(i=1; i<=NUM_NODE; i++){ visited[i] = false; } connected_component(graph); }
void insert_last(int ele, int i){ newnode = create_node(ele); if (first[i]==last[i] && last[i]==NULL) { first[i] = last[i] = newnode; first[i]->next = NULL; last[i]->next = NULL; } else { last[i]->next = newnode; last[i] = newnode; last[i]->next = NULL; //Technically not needed ? Done while creating node } }
int read_to_db(Parts db, FILE *fp, off_t record_size) { Node *n; Part p; for (;;) { p = new_part(); if (fread(p, record_size, 1, fp) < 1) { destroy_part(p); if (ferror(fp)) return -1; break; } if (db->head) { n->next = create_node(p); n = n->next; } else { db->head = create_node(p); n = db->head; } db->count++; } return 0; }
void insert( const vector_type& min, const vector_type& max, const T& x ) { //aabb_node* n = new aabb_node; aabb_node* n = create_node(); n->min = min; n->max = max; n->car = NULL; n->cdr = NULL; n->user = x; if( !root_ ) { root_ = n; return; } insert_aux( &root_, n ); }
static VALUE deque_push_back(VALUE self, VALUE obj) { deque *deque = get_deque_from_self(self); deque_node *node = create_node(obj); if(deque->back) { node->left = deque->back; deque->back->right = node; deque->back = node; } else { deque->front = node; deque->back = node; } deque->size++; return obj; }
//takes dec, hashes: if already exists->nada; othrwise pleaces in ll and advances finalCount // // void insert(unsigned long long num){ int index; unsigned long long thingy = ARR_SIZE; index = num%thingy; if(hashTable[index].head == NULL){ struct node *newnode = create_node(num); hashTable[index].head = newnode; finalCount++; return; } //not the first insertion; so iterate over ll and make sure no duplicates // // struct node *ptr;//create ptr ptr = hashTable[index].head;//point ptr at indexed head while(ptr!=NULL){ if(ptr->data == num){ return;//return without advancing count } ptr = ptr->next;//advance ptr } //at this point, no dups have been found, so go ahead and place node/advance finalCt // // struct node *newnode = create_node(num); newnode->next = hashTable[index].head; hashTable[index].head = newnode; finalCount++;//advance finalct for unique insertion return; }//end insert