INLINE struct hacTree preClusterNodes(const struct sortWrapper *leafWraps, int i, int runLength, hacDistanceFunction *distF, hacMergeFunction *mergeF, void *extraData, struct lm *localMem) /* Caller has allocated a node, and this returns what to store there: * a recursively constructed cluster of nodes extracted from wrapped * leafNodes (leafWraps) starting at i, for runLength items. */ { struct hacTree ret = {NULL, NULL, NULL, NULL, 0, NULL}; if (runLength > 2) { struct hacTree *newClusters = lmAlloc(localMem, 2 * sizeof(struct hacTree)); int halfLength = runLength/2; newClusters[0] = preClusterNodes(leafWraps, i, halfLength, distF, mergeF, extraData, localMem); newClusters[1] = preClusterNodes(leafWraps, i+halfLength, runLength-halfLength, distF, mergeF, extraData, localMem); initNode(&ret, &(newClusters[0]), &(newClusters[1]), distF, mergeF, extraData); } else if (runLength == 2) { initNode(&ret, leafWraps[i].node, leafWraps[i+1].node, distF, mergeF, extraData); } else ret = *(leafWraps[i].node); return ret; }
ssize_t segmentTree<T>::initNode(const ssize_t nodeIdx, const ssize_t lftIdx, const ssize_t rhtIdx, const T *input) { //cout << "nodeIdx = " << nodeIdx << "," << "nodes =" << nodes << endl; cout << lftIdx << " "<< rhtIdx << endl; if(nodeIdx >= nodes) /*segment error*/ { return -1; } if(lftIdx == rhtIdx) { tree[nodeIdx] = input[lftIdx]; return 0; } ssize_t midIdx = (lftIdx + rhtIdx) >> 1; ssize_t curNode = nodeIdx; ssize_t lftNode = getLftNode(curNode); ssize_t rhtNode = getRhtNode(curNode); if(initNode(lftNode, lftIdx, midIdx, input) < 0) { return -1; } if(initNode(rhtNode, midIdx + 1, rhtIdx, input) < 0) { return -1; } tree[curNode] = (tree[lftNode] < tree[rhtNode] ? tree[lftNode] : tree[rhtNode]); return 0; }
void processNode(Knapsack* ks, Heap* h, Node* n) { //printf("Process: value=%d, weight=%d, total value %d\n", item->_value, item->_weight, n->_value); char* x = (char*)malloc(sizeof(char)*(n->_depth+2)); int i; for (i=0; i<=n->_depth; i++) { x[i] = n->_x[i]; } Item* nextItem = ks->_items[n->_depth+1]; x[n->_depth+1] = 0; double est = upperBound(ks, n->_depth+2, n->_capacity); // Following the convention that choosing (x_i = 0) means "branch to the right" // Rounding up. double rightUB = n->_value + est; Node* rightNode = initNode(n->_value, n->_depth+1, rightUB, n->_capacity, x); inspectNode(ks, h, rightNode); if (nextItem->_weight <= n->_capacity) { x = (char*)malloc(sizeof(char)*(n->_depth+2)); for (i=0; i<=n->_depth; i++) { x[i] = n->_x[i]; } x[n->_depth+1] = 1; int leftValue = n->_value + nextItem->_value; int leftCapacity = n->_capacity - nextItem->_weight; est = upperBound(ks, n->_depth+2, leftCapacity); double leftUB = leftValue + est; Node* leftNode = initNode(leftValue, n->_depth+1, leftUB, leftCapacity, x); inspectNode(ks, h, leftNode); } destroyNode(n); }
void addKey(HashTable *table, char *key, char *value){ //printf("Adding %s : %s to the table\n",key,value); if(table){ table->count += 1; if(table->count >= (table->maxSize)/2){ printf("After adding this one count is %08x\n",table->count); grow(table); } uint32_t k = prehash(key); if(table->values[k % table->maxSize]){ Node *n = initNode(key,value); //printf("Inserting at address: %08x\n",k%table->maxSize); // printf("Collision detected!\n"); insertNode(table->values[k % table->maxSize],n); }else{ //printf("Inserting at address: %08x\n",k%table->maxSize); table->values[k % table->maxSize] = initNode(key,value); } }else{ table = initTable(INITIAL_HASH_SIZE); uint32_t k = prehash(key); printf("Inserting at address: %08x\n",k%table->maxSize); table->values[k % table->maxSize] = initNode(key,value); } }
static struct hacTree *pairUpItems(const struct slList *itemList, int itemCount, int *retPairCount, struct lm *localMem, hacDistanceFunction *distF, hacMergeFunction *mergeF, hacCmpFunction *cmpF, void *extraData) /* Allocate & initialize leaf nodes and all possible pairings of leaf nodes * which will be our seed clusters. If cmpF is given, pre-sort the leaf nodes * and pre-cluster identical leaves before generating seed clusters. */ { struct hacTree *leafNodes = leafNodesFromItems(itemList, itemCount, localMem); if (cmpF != NULL) leafNodes = sortAndPreCluster(leafNodes, &itemCount, localMem, distF, mergeF, cmpF, extraData); int pairCount = (itemCount == 1) ? 1 : (itemCount * (itemCount-1) / 2); struct hacTree *pairPool = lmAlloc(localMem, pairCount * sizeof(struct hacTree)); if (itemCount == 1) initNode(pairPool, leafNodes, NULL, distF, mergeF, extraData); else { int i, j, pairIx; for (i=0, pairIx=0; i < itemCount-1; i++) for (j=i+1; j < itemCount; j++, pairIx++) initNode(&(pairPool[pairIx]), &(leafNodes[i]), &(leafNodes[j]), distF, mergeF, extraData); } *retPairCount = pairCount; return pairPool; }
void insertIterative (TNODE_p_t *root, String item) { int ch; TNODE_p_t t = *root; TNODE_p_t present = NULL; TNODE_p_t newnode; if (*root == NULL) { *root = initNode(item); return; } do { printf("\tCurrent: '%s' | 1. Left%s; 2. Right%s | Choice: ", t->data, ((t->left == NULL)?"(*)":""), ((t->right == NULL)?"(*)":"")); scanf(" %d", &ch); present = t; if (ch == 1) t = t->left; else if (ch == 2) t = t->right; if (t == NULL) { newnode = initNode(item); (ch == 1)?(present->left = newnode):(present->right = newnode); return; } } while (YES); }
// Assume the table exists void rehash(HashTable *table, char *key, char *value){ /* if(table){ // If the table exists, then add a new node to the given key uint32_t k = prehash(key); Node *newNode = initNode(key,value); printf("prehash %08x, max size: %08x\n",k,table->maxSize); printf("Storing at index %02d \n",k%table->maxSize); insertNode(table->values[k % table->maxSize],newNode); table->count++; } */ //printf("Adding %s : %s to the table\n",key,value); if(table){ table->count += 1; uint32_t k = prehash(key); if(table->values[k % table->maxSize]){ Node *n = initNode(key,value); //printf("Inserting at address: %08x\n",k%table->maxSize); //printf("Collision detected!\n"); insertNode(table->values[k % table->maxSize],n); }else{ //printf("Inserting at address: %08x\n",k%table->maxSize); table->values[k % table->maxSize] = initNode(key,value); } } }
bool isPalindromeIterate(struct node * ptr){ // This method is best implemented with a stack, the theme is linked lists so I won't. struct node * fast = ptr; struct node * slow = ptr; struct node * stack = NULL; //get middle; while(fast!=NULL && fast->next!=NULL){ if(stack == NULL) stack = initNode(slow->val); else { struct node * temp = initNode(slow->val); temp -> next = stack; stack = temp; } slow = slow->next; fast = fast->next->next; } if(fast!=NULL) slow = slow->next; // odd size, so we move past it. while(slow!=NULL){ if(slow->val != stack->val){ return false; } slow = slow->next; stack = stack->next; } return true; }
uint64_t findFile(const struct superblock* sb, const char* fname, int* exists) { assert(exists != NULL); struct inode* node, *ent; if (strcmp("/", fname) == 0) { *exists = TRUE; return sb->root; } initNode(&node, sb->blksz); initNode(&ent, sb->blksz); struct nodeinfo *meta = (struct nodeinfo*) malloc(sb->blksz); int len = 0; char** fileParts = getFileParts(fname, &len); *exists = FALSE; uint64_t fileBlock = sb->root; seek_read(sb, fileBlock, node); int it = 1; while (it < len) { int foundEnt = FALSE; int i = 0; do { while (node->links[i] != 0) { seek_read(sb, node->links[i], ent); seek_read(sb, ent->meta, meta); if (strcmp(meta->name, fileParts[it]) == 0) { foundEnt = TRUE; fileBlock = node->links[i]; it++; break; } i++; } if ((foundEnt || node->next == 0))break; seek_read(sb, node->next, node); } while (node->next != 0); if (foundEnt) { seek_read(sb, fileBlock, node); } else { *exists = FALSE; free(node); free(ent); free(meta); freeFileParts(&fileParts, len); return fileBlock; } } if (it >= len) *exists = TRUE; freeFileParts(&fileParts, len); free(node); free(ent); free(meta); return fileBlock; }
TreeNodePtr buildTree(TreeNodePtr root,char* str){ TreeNodePtr initNode(); if(*str=='@') return NULL; root = initNode(); root->data = *str-'0'; str++; root->left = buildTree(root->left,str); str++; root->right = buildTree(root->right,str); return root; }
void setUp(void) { initNode(&node0, 0, NULL, NULL, BLACK); initNode(&node5, 5, NULL, NULL, BLACK); initNode(&node10, 10, NULL, NULL, BLACK); initNode(&node20, 20, NULL, NULL, BLACK); initNode(&node25, 25, NULL, NULL, BLACK); initNode(&node30, 30, NULL, NULL, BLACK); initNode(&node50, 50, NULL, NULL, BLACK); initNode(&node100, 100, NULL, NULL, BLACK); initNode(&node150, 150, NULL, NULL, BLACK); initNode(&node200, 200, NULL, NULL, BLACK); }
/** *RIGHT double black *----------------- *Testing case(2a): The sibling is black and both sibling's children * are NULL (with the parent being black). * * ^ / // ^ * | 50(b) 50(db) | * | / \\ flip / \ | height * height | 25(b) 100(db) color 25(r) - | 1 * 2 | / \ --------> / \ | * | - - - - | * | v * | * | * v * */ void test_rightFlipColorCase2a_given_sibling_is_black_and_both_sibling_children_are_null_should_flip_color(void){ Node *root = &node50; Node *removeNode = &node100; initNode(&node25, 25, NULL, NULL, BLACK); initNode(&node100, 100, NULL, NULL, DOUBLE_BLACK); initNode(&node50, 50, &node25, &node100, BLACK); removeRightCase2a(&root, removeNode); TEST_ASSERT_EQUAL_NODE(25, NULL, NULL, RED, &node25); TEST_ASSERT_EQUAL_NODE(50, &node25, NULL, DOUBLE_BLACK, &node50); }
/** *RIGHT double black *----------------- *Testing case(2b): The sibling is black and both sibling's children * are black (with the parent being red). * * ^ / / ^ * | 100(r) 100(b) | * | / \\ flip / \ | height * height | 50(b) - color 50(r) - | 2 * 3 | / \ --------> / \ | * | - - - - v * | * v */ void test_rightFlipColorCase2b_given_sibling_and_sibling_childrens_are_black_with_parent_is_red_should_flip_color(void){ Node *root = &node100; Node *removeNode = &node150; initNode(&node150, 150, NULL, NULL, DOUBLE_BLACK); initNode(&node50, 50, NULL, NULL, BLACK); initNode(&node100, 100, &node50, &node150, RED); removeRightCase2b(&root, removeNode); TEST_ASSERT_EQUAL_NODE(50, NULL, NULL, RED, &node50); TEST_ASSERT_EQUAL_NODE(100, &node50, NULL, BLACK, &node100); }
int main() { struct TreeNode n3, n2, n1; initNode(&n3, 3, NULL, NULL); initNode(&n2, 2, &n3, NULL); initNode(&n1, 1, NULL, &n2); int sz; int *r = preorderTraversal(&n1, &sz); showArr(r, sz); free(r); return 0; }
struct Node* initBase(OppBase *base, bool isPlayFirst) { if (false == isPlayFirst) { base->nonDealerRoot = (struct Node*)malloc(sizeof(struct Node)); initNode(base->nonDealerRoot, strength); return base->nonDealerRoot; } else { base->dealerRoot = (struct Node*)malloc(sizeof(struct Node)); initNode(base->dealerRoot, prob); return base->dealerRoot; } }
TTN_p_t insertItem (TTN_p_t tree, int item) { if (tree == NULL) { TTN_p_t node = initNode(); node->k1 = item; return node; } if (tree->k1 == LESS) { tree->k1 = item; return tree; } else if (tree->k2 == LESS) { tree->k2 = MAX(item, tree->k1); tree->k1 = MIN(item, tree->k1); return tree; } else { if (item < tree->k1) { tree->left = insertItem(tree->left, item); } else if (item > tree->k2) { tree->right = insertItem(tree->right, item); } else { tree->mid = insertItem(tree->mid, item); } } if (tree->left == NULL && tree->mid == NULL && tree->right == NULL) { TTN_p_t leftNode = initNode(); TTN_p_t rightNode = initNode(); if (item < tree->k1) { // tree->k1 = tree->k1; leftNode->k1 = item; rightNode->k1 = tree->k2; } else if (item > tree->k2) { leftNode->k1 = tree->k1; tree->k1 = tree->k2; rightNode->k1 = item; } else { leftNode->k1 = tree->k1; tree->k1 = item; rightNode->k1 = tree->k2; } tree->k2 = LESS; tree->left = leftNode; tree->mid = rightNode; return tree; } return tree; }
int main(int argc, char *argv[]) { /* 这样做有一个问题, hear永远没有办法得到*/ RearLinkedNode *A = initNode(); int result = ShowNodes(A); RearLinkedNode *B = initNode(); result = ShowNodes(B); Connect(A, B); result = ShowNodes(B); return 0; }
/** *RIGHT double black *----------------- *Testing case(1b): The sibling is black and the sibling's right child * is red (with the parent being black). * * ^ / / ^ * | 50(b) rotate 25(b) | * | / \\ left / \ | height * height | 10(b) - right 10(b) 50(b) | 3 * 3 | / \ --------> / \ / \ | * | - 25(r) - - - - | * | / \ v * | - - * | * v * */ void test_leftRightRotateCase1b2_given_right_child_is_double_black_and_sibling_is_black_and_sibling_right_child_is_red_should_rotate_left_and_right(void){ Node *root = &node50; Node *removeNode = &node0; initNode(&node25, 25, NULL, NULL, RED); initNode(&node0, 0, NULL, NULL, DOUBLE_BLACK); initNode(&node10, 10, NULL, &node25, BLACK); initNode(&node50, 50, &node10, &node0, BLACK); removeRightCase1b2(&root, removeNode); TEST_ASSERT_EQUAL_NODE(10, NULL, NULL, BLACK, &node10); TEST_ASSERT_EQUAL_NODE(50, NULL, NULL, BLACK, &node50); TEST_ASSERT_EQUAL_NODE(25, &node10, &node50, BLACK, &node25); }
/** *LEFT double black *----------------- *Testing case(1a): The sibling is black and the sibling's right child * is red (with the parent being black). * * ^ / / ^ * | 10(b) rotate 20(b) | * | // \ Left / \ | height * height | - 20(b) ------> 10(b) 100(b) | 4 * 3 | / \ / \ / \ | * | - 100(r) - - - - | * | / \ v * | - - * | * v * */ void test_leftRotateCase1a2_given_left_child_is_double_black_and_sibling_is_black_and_sibling_left_child_is_red_should_rotate_right(void){ Node *root = &node10; Node *removeNode = &node0; initNode(&node100, 100, NULL, NULL, RED); initNode(&node0, 0, NULL, NULL, DOUBLE_BLACK); initNode(&node20, 20, NULL, &node100, BLACK); initNode(&node10, 10, &node0, &node20, BLACK); removeLeftCase1a2(&root, removeNode); TEST_ASSERT_EQUAL_NODE(100, NULL, NULL, BLACK, &node100); TEST_ASSERT_EQUAL_NODE(10, NULL, NULL, BLACK, &node10); TEST_ASSERT_EQUAL_NODE(20, &node10, &node100, BLACK, &node20); }
/** *LEFT double black *----------------- *Testing case(1a): The sibling is black and the sibling's right child * is red (with the parent being red). * * ^ / / ^ * | 20(r) rotate 50(r) | * | // \ Left / \ | height * height | 5(db) 50(b) ------> 20(b) 100(b) | 3 * 3 | / \ / \ / \ | * | - 100(r) - - - - | * | / \ v * | - - * | * v * */ void test_leftRotateCase1a1_given_sibling_is_black_and_sibling_right_child_is_red_should_rotate_left(void){ Node *root = &node20; Node *removeNode = &node5; initNode(&node100, 100, NULL, NULL, RED); initNode(&node5, 5, NULL, NULL, DOUBLE_BLACK); initNode(&node50, 50, NULL, &node100, BLACK); initNode(&node20, 20, &node5, &node50, RED); removeLeftCase1a1(&root, removeNode); TEST_ASSERT_EQUAL_NODE(100, NULL, NULL, BLACK, &node100); TEST_ASSERT_EQUAL_NODE(20, NULL, NULL, BLACK, &node20); TEST_ASSERT_EQUAL_NODE(50, &node20, &node100, RED, &node50); }
void parseChunkFile(char* chunkfile, linkedList* list){ cleanChunkList(list); FILE* fp; char locBuf[100]; unsigned int id; char hash[sizeofHash]; /* parse chunkfile*/ if((fp = fopen(chunkfile, "r")) == NULL){ fprintf(stderr,"Error opening %s",chunkfile); exit(1); } list->length = 0; list->finished = 0; while(fgets(locBuf, sizeof(locBuf),fp)){ /* read each line in chunkfile */ if(sscanf(locBuf,"%d %40c",&id,hash) < 2){ fprintf(stderr,"Malformed chunkfile %s\n",chunkfile); } /* add new chunk entry in list */ chunkEle* ele = initChunkEle(id,hash); node* newNode = initNode(ele); addList(newNode, list); } fclose(fp); }
/* Adds element to the front of the list. */ void addFront(node *head, char *elem) { node *newNode; newNode = initNode(elem); newNode->next = head->next; head->next = newNode; }
int main(int argc, const char** argv) { try { if (argc < 3) { std::cerr << "Usage:\n\t" << argv[0] << " <node-id> <can-iface-name-1> [can-iface-name-N...]" << std::endl; return 1; } const int self_node_id = std::stoi(argv[1]); std::vector<std::string> iface_names; for (int i = 2; i < argc; i++) { iface_names.emplace_back(argv[i]); } uavcan_linux::NodePtr node = initNode(iface_names, self_node_id, "org.uavcan.linux_test_node"); std::cout << "Node initialized successfully" << std::endl; runForever(node); return 0; } catch (const std::exception& ex) { std::cerr << "Exception: " << ex.what() << std::endl; return 1; } }
int llMain() { node_t *ptr = NULL; int entry = 0; while (1) { printf("\nMake selection:\n1)Add node\n2)Delete Node\n3)Print Node List\n4)Insert node\n5)Return to main menu\n"); entry = getInput(); switch (entry) { case 1: printf("Enter int:"); ptr = initNode(getInput()); addNode(&head, &tail, ptr); break; case 2: deleteNode(&head, &tail); break; case 3: printList(&head); break; case 4: insertNode(&head, &tail); break; case 5: system("cls"); return 0; break; } } return 0; }
struct TrieNode *add(struct TrieNode *pNode, char c){ struct TrieNode **pNext; if (pNode == NULL) return NULL; /* Find child node */ switch (c) { case 'A': pNext = &(pNode->a); break; case 'C': pNext = &(pNode->c); break; case 'G': pNext = &(pNode->g); break; case 'T': pNext = &(pNode->t); break; default: /* Error, nothing found */ return NULL; } /* Check for untouched path in trie, create new one if not found */ if (*pNext == NULL) *pNext = initNode(); return *pNext; }
void insertLastNode(listNode_h *N, const char *dev_path)//node add Head and Tail { listNode *newNode = (listNode *)malloc(sizeof(listNode)); // if(strcmp(dev_path, "/dev/ttyUSB0") == 0) // LOGI(" 7. insertNode /dev/ttyUSB0"); // else // LOGI(" 7. insertNode /dev/ttyUSB????"); initNode(newNode,dev_path); if(N->head == NULL) { //node not exist //set head N->head = newNode; N->tail = newNode; printf(">> Insert Node: %s\n", N->head->dev_path); return; } else { //node exist N->tail->next = newNode; N->tail = newNode; printf(">> Insert Node: %s\n", N->tail->dev_path); return; } printf("---Insert Node Fail!!!!!"); }
/*Initialize a linked list*/ struct node * addNodeInList(struct node *ptr, int n) { struct node *current; if(ptr == NULL) ptr = initNode(n); else { current = ptr; while(current->next != NULL) current = current->next; current->next = initNode(n); } return ptr; }
AudioBufferNode::AudioBufferNode(int sample_rate) : Node(nullptr, NType::INVALID, "Null Audio Buffer Node", "Unspecified"), BufferNode<c_time, AudioVelData>(1, AudioVelData(AUDIO_CHUNK_SIZE, 1, 0)), sampleRate(sample_rate) { initNode(); }
void addNode(Graph* graph, char name) { addNodeCapacityIfFull(graph); Node node; initNode(&node); node.name = name; graph->nodes[graph->nodeCount++] = node; }
void addFront(Node * head, int nodeValue) { Node * newNode; newNode = initNode(nodeValue); newNode -> next = head -> next; head -> next = newNode; }