/* down() - It checks if sem-> > 0 , If true then decreases the count and gives the semaphore else adds the thread to the waiting queue and suspends it. */ int sthread_sem_down(sthread_sem_t *sem) { lock(); int ret; if (sem->count > 0) { sem->count--; ret = 0; unlock(); } else { insertNode(sem, sthread_self()); ret = -1; unlock(); sthread_suspend(); } return ret; }
RBTree * loadTree(char *filename){ RBTree *tree = malloc(sizeof(RBTree)); initTree(tree); FILE *fp; fp = fopen(filename, "r"); if(!fp){ deleteTree(tree); free(tree); return NULL; } int sizeDb, numNodes = NULL; fread( &(sizeDb), sizeof(int), 1, fp ); fread( &(numNodes), sizeof(int), 1, fp ); if(numNodes == 0){ fclose(fp); deleteTree(tree); free(tree); return NULL; } int i; for(i=0; i< numNodes; i++){ RBData *data = malloc(sizeof(RBData)); int length = NULL; fread(&(length), sizeof(int), 1, fp); data->primary_key = malloc(sizeof(char) * (length+1) ); fread(data->primary_key, sizeof(char), length, fp); data->primary_key[length] = '\0'; fread(&(data->numFiles), sizeof(int), 1, fp); data->numTimes = malloc(sizeof(int) * sizeDb); fread(data->numTimes, sizeof(int), sizeDb, fp); insertNode(tree, data); } fclose(fp); return tree; }
Boolean writeSymbolAddress(CodeSection *codeSection, char *symbol, SourceLinePtr sourceLine) { Word symbolAddress; Word symbolLocation; SymbolPtr symbolPtr; MemoryType memoryType; ListNodeDataPtr dataPtr; if (symbol == NULL) { logError("Symbol address to write was NULL."); return False; } symbolPtr = findSymbol(codeSection->symbolTable, symbol); if (symbolPtr == NULL) { logErrorInLine(sourceLine, "Unable to find symbol '%s' in symbol table.\n", symbol); return False; } symbolAddress = symbolPtr->value; memoryType = MemoryType_Relocatable; if (symbolAddress == EXTERN_SYMBOL_VALUE) { symbolAddress = DEFAULT_EXTERNAL_SYMBOL_ADDRESS; memoryType = MemoryType_External; dataPtr = (ListNodeDataPtr)malloc(sizeof(ListNodeData)); symbolLocation = getAbsoluteInstructionCounter(codeSection); dataPtr->symbolLocation = initSymbolLocation(symbol, symbolLocation); insertNode(&codeSection->externalSymbols, dataPtr, NodeType_SymbolLocation); } setCurrentMemoryLocationType(codeSection, memoryType); writeWord(codeSection->memory, symbolAddress); return True; }
void insertOptimalNode(GRADIENT* g) { int i, next_gap; int pos = 0; int gap = g->ratios[0]; for (i = 0; i < g->num - 1; i++) { next_gap = g->ratios[i + 1] - g->ratios[i]; if (next_gap > gap) { gap = next_gap; pos = i + 1; } } next_gap = 255 - g->ratios[g->num -1]; if (next_gap > gap) pos = g->num; insertNode(g, pos); }
int main() { //test(); system("clear"); int num, choice, c; char name[NAMELEN]; char sex; Llist L; L = createList(); while(1){ choice = option(); if(choice == 1){ system("clear"); printf("please enter job number: "); scanf("%d",&num); printf("\nplease enter name: "); while ((c=getchar()) != '\n' && c != EOF); //fflush() can't use in gcc fgets(name, NAMELEN, stdin); printf("\nplease enter sex: "); scanf("%c",&sex); insertNode(L,num,name,sex); continue; } if(choice == 2){ system("clear"); printf("please enter delete number: "); scanf("%d",&num); deleteNode(L,num); continue; } if(choice == 3){ system("clear"); printList(L); continue; } if(choice == 0){ return 0; } } return 0; }
int TransRecord::readin() { char line[MAX_LINE_LENGTH + 1] = {'\0'}; int num = 0; int res = 0; /* * here, we first getline, then judge whether the eof is reached. * because the eof will work only after some op has been done on * the file. So, if we put the getline func inside the loop, after * we read the last line, the eof is not triggered, only after the * next read operation, the eof will work. That is, after we get the * last line, it still goes into the loop. So, we get one line first, * and put the getline at the end of the loop. */ _input.getline(line, MAX_LINE_LENGTH); while (!_input.eof()) { num++; if (_input.fail()) { cerr << "error in input file, line " << num << endl; return FALSE; } TransNode * node = NULL; res = readLine(line, &node); if (res == FALSE) { cerr << "wrong format in file, line " << num << endl; return FALSE; } res = insertNode(node); if (res == FALSE) { cerr << "error when process input file, line " << num << endl; return FALSE; } _input.getline(line, MAX_LINE_LENGTH); } return TRUE; }
struct node * insertAtEveryKthNode(struct node *head, int K) { if (head==NULL||K<1) return NULL; int count; struct node *temp; temp = head; count = K; while (temp != NULL){ if (count == 1){ temp->next=insertNode(temp->next, K); count = K; temp = temp->next->next; } else{ temp = temp->next; count -= 1; } } return head; }
void DynamicArrayList::add(const Element& e) { Node* after = head->next; Node* last = tail->previous; Node* first = head->next; if (isEmpty() || e <= first->elements) { // Check if empty or first one. add(0, e); } else if (e >= last->elements) { // Check if last one. add(size(), e); } else { // Find first node with a value > than e's value. while (e >= after->elements) { after = after->next; } // Insert the new node into the list. insertNode(after->previous, e, after); } }
// stores a String as a Linked List node *stringToList( char *str ) { node *head = NULL; int length, i; if(str == NULL || str == "") return NULL; // count the length of the string length = 0; while(*(str+length) != '\0') length++; // strip each letter of the string in reverse order to be able to // insert each new node at the front of the LinkedList for(i = 0; i<length; i++) head = insertNode( head, str[length-i-1] ); return head; }
int main(){ Node *R = NULL; R = insertNode(R,0); R = insertNode(R,1); R = insertNode(R,5); R = insertNode(R,2); R = insertNode(R,7); R = insertNode(R,8); R = insertNode(R,-1); R = insertNode(R,99); printf("\n\n"); printTree(R); R = removeNode(R,5); printf("\n\n"); printTree(R); R = removeNode(R,4); printf("\n\n"); printTree(R); return 0; }
void * tree_Insert(pwr_tStatus *sts, tree_sTable *tp, void *key) { tree_sNode *np; tree_sNode *op; np = findNode(tp, key); if (np != tp->null) pwr_Return((void *)np, sts, TREE__FOUND); np = allocNode(tp, key); if (np == NULL) return NULL; op = insertNode(tp, np); if (np == op) { pwr_Return((void *)np, sts, TREE__INSERTED); } else { freeNode(tp, np); pwr_Return(NULL, sts, TREE__ERROR); } }
static void traverse( TreeNode * t,int sign) { if (t != NULL) { insertNode(t,sign); if(t -> nodekind == StmtK && t -> kind.stmt == VarK) { token = t -> attr.op; for(int i=0;i<MAXCHILDREN;i++) traverse(t->child[i],1); } else { //if(t -> nodekind == Stmt) for(int i=0;i<MAXCHILDREN;i++) traverse(t->child[i],sign); } traverse(t->sibing,sign); } }
/* Finds InDels in a BAM or CRAM file, adding them to the linked list * * fp Input BAM/CRAM file * hdr The header for the BAM/CRAM file * k The K-mer size * * discussion The linked list will need to be destroyed with destroyNodes() */ void findInDels(htsFile *fp, bam_hdr_t *hdr, int minMAPQ, int k) { bam1_t *b = bam_init1(); int i, op; InDel *node; uint32_t *cigar; while(sam_read1(fp, hdr, b) > 0) { if(b->core.qual < minMAPQ) continue; cigar = bam_get_cigar(b); for(i=0; i<b->core.n_cigar; i++) { op = bam_cigar_op(cigar[i]); if(op == 1 || op == 2) { node = makeNode(b, i); if(node == NULL) goto quit; insertNode(node, k); while(++i < b->core.n_cigar) { //Skip adjacent D/I operations op = bam_cigar_op(cigar[i]); if(op != 1 && op != 2) break; continue; } } } } //Ensure that all ROIs are at least k apart lastTargetNode = firstTargetNode->next; while(lastTargetNode->next) { i = TargetNodeCmp(lastTargetNode,lastTargetNode->next, k); assert(i<=0); if(i==0) { lastTargetNode->end = lastTargetNode->next->end; lastTargetNode->count += (lastTargetNode->count+lastTargetNode->next->count > lastTargetNode->count)?lastTargetNode->next->count:0xFFFFFFFF; removeNode(lastTargetNode->next); } else { lastTargetNode = lastTargetNode->next; } } quit: bam_destroy1(b); }
//main () int main (int argC, char *argV[]) { //Call syntax check if (argC != 2) { printf ("Usage: %s Input_filename\n", argV[0]); exit (1); } //Main variables FILE *inFile = NULL, *outFile = NULL; node *entry = NULL, *firstNode = NULL; int count = 0; char in; //File creation and checks printf ("Opening files...\n"); createFile (&inFile, argV[1], 'r'); createOutputFile (&outFile, argV[1]); //Load entries into DLL printf ("Files opened. Loading entries...\n"); while (1) { //Stop conditions if (((ferror (inFile)) || (feof (inFile)))) { break; } entry = malloc (sizeof (node)); initializeNode (entry); loadEntry (entry, inFile); firstNode = insertNode (entry, firstNode); entry = NULL; //A counter so the user has some idea of how long it will take if (++count % 100000 == 0) { printf ("%d entries ordered...\n", count); } } fclose (inFile); //Write ordered list to file printf ("%d entries ordered. Writing to file...\n", count); printNodeList (firstNode, outFile); //Close everything and free memory printf ("Writen. Closing files and freeing memory...\n"); fclose (outFile); return 0; }
Node insertTree(Node t, char* id) { // Default return value is 0 or failed to insert this node Node rval = tnull; Node n = createNode(id); do { if ( t->root == tnull ) { // We do not have anything in our tree yet t->root = n; t->root->color = BLACK; rval = n; break; } n = insertNode( t->root, n ); if ( n != tnull ) { t->root = fix(t->root,n); } } while ( 0 ); return rval; }
void main() { int n,x; int num_nodes = 7; x=0; while (x<num_nodes) { printf("Enter new node of BST "); scanf("%d",&n); insertNode(root,n); x++; } printf("\nInorder Traversal is:\n"); inOrderTrav(root); printf("\nPreorder Traversal is:\n"); preOrderTrav(root); printf("\nPostorder Traversal is:\n"); postOrderTrav(root); }
int main() { int i, option; Tree* BinarySearchTree = createEmptyTree(); TreeAVL* AVLTree = createEmptyAVLTree(); printf("Count and plot data\nBinary Search Tree vs. AVL Tree\n\n"); printf("To continue, type 1\nTo exit, type ant other key\n\n"); printf("Your option: "); scanf("%d", &option); if(option == 1) { for(i = 0; i < 10000; i++) { BinarySearchTree = insertNode(BinarySearchTree, i); AVLTree = insertNodeAVL(AVLTree, i); } printf("\nNumber of comparisons to find %d in the AVL Tree: %d\n", i - 1, binarySearchAVL(AVLTree, i - 1, 0)); printf("Number of comparisons to find %d in the Binary Search Tree: %d\n", i - 1, binarySearch(BinarySearchTree, i - 1, 0)); } return 0; }
synLink *createSynlink(){ synLink *s = (struct synLink *)malloc(sizeof(struct synLink)); synLink *head = s; s->port = NULL; s->ipaddr = NULL; s->next = NULL; char addrs[] = "127.0.0.1:9000#10.103.123.22:9000#"; char *serverAddr; serverAddr = strtok(addrs,"#"); // printf("serverAddr %s\n",serverAddr); while(serverAddr != NULL){ printf("in while serverAddr %s\n",serverAddr); s = insertNode(s,serverAddr); //s->next = temp; printf("========%s\n",s->ipaddr); serverAddr = strtok(NULL,"#"); } printf("head->next->ip %s \n",head->next->ipaddr); return head; }
int main(){ NODE* ptr; TREE* tree; int i=0; tree = create_tree(); for(i=0; i<13; i++){ insertNode(tree, i); } printf("PreOrder: "); preOrderPrint(tree->root); printf("\nInOrder: "); inOrderPrint(tree->root); printf("\npostOrder: "); postOrderPrint(tree->root); printf("\nPrint list:"); linklistprint(tree->root); printf("\ntree count: %d\n", tree->count); return 0; }
void test_insert_should_insert_children_to_any_child(){ Iterator* iterator; Tree tree = createTree(compareInt); int result; result = insertNode(&tree, NULL, &data[0]); result = insertNode(&tree, &data[0] ,&data[1]); result = insertNode(&tree, &data[1] ,&data[2]); result = insertNode(&tree,&data[2],&data[3]); result = insertNode(&tree,&data[3],&data[4]); result = insertNode(&tree,&data[4],&data[5]); iterator = getChildren(&tree, &data[3]); ASSERT(&data[4] == iterator->next(iterator)); iterator = getChildren(&tree, &data[4]); ASSERT(&data[5] == iterator->next(iterator)); ASSERT(result == 1); };
int main(int argc, char* argv[]) { if (argc<2) return 0; FILE *f = fopen(argv[1],"r"); if (f==NULL) return 0; struct Tree* t; newTree(&t); char x[20]; int res = 0; while (fscanf(f,"%s",x)==1) { if (x[0]== '"') continue; res = res + insertNode(&t, atoi(x)); } fclose(f); printf ("%d ",res); printf ("%d ",t->size); printTree(t->root); return 0; }
Graph * CreateComments(char * file, char * file2, Graph * PostGraph) //finding comments and creators of comments { FILE* fp = fopen(file,"r"); //file = comment_replyOf_post Node * n; int *creator; Graph * CommentsGraph = createGraph(M,C); if (fp == NULL) { perror ("Error opening file"); exit(-1); } char buffer[200],*s; fgets(buffer,200,fp); while(fgets(buffer,200,fp)!= NULL) { s = strtok (buffer,"|"); if((n = lookupNode(atoi(strtok(NULL,"\n")), PostGraph)) != NULL) { n = malloc(sizeof(Node)); n->ID = atoi(s); n->obj = NULL; insertNode(n,CommentsGraph); } } fclose(fp); fp = fopen(file2, "r"); //file2 = comment_hasCreator_person fgets(buffer,200,fp); while(fgets(buffer,200,fp)!= NULL) { if((n = lookupNode(atoi(strtok (buffer,"|")), CommentsGraph)) != NULL) { creator = malloc(4); *creator = atoi(strtok (NULL, "\n")); n->obj = creator; } } fclose(fp); return CommentsGraph; }
pool_tRef ptree_Insert(pwr_tStatus* sts, ptree_sTable* tp, void* key) { pool_tRef nr; pool_tRef or ; nr = findNode(tp, key); if (nr != tp->g->null) pwr_Return(nr, sts, TREE__FOUND); nr = allocNode(tp, key); if (nr == pool_cNRef) pwr_Return(pool_cNRef, sts, TREE__ERROR); or = insertNode(tp, nr); if (nr == or) { pwr_Return(nr, sts, TREE__INSERTED); } else { freeNode(tp, nr); pwr_Return(pool_cNRef, sts, TREE__ERROR); } }
void kalman::updatelist(int x, int y) { node * p = nl->next; bool existud = false; while (p) { if ((p->x - x)*(p->x - x) <= 3000 && (p->y - y)*(p->y - y) <= 3750) { p->dy = p->dy + p->y - y; p->x = x; p->y = y; p->done = true; existud = true; break; } p = p->next; } if (!existud && x != 0 && y != 0) { insertNode(nl, x, y); } }
int main() { int i; Node* root = NULL; int arr[] = {10,4,16,8,1,12,15,5,23,17}; int size = sizeof(arr)/sizeof(*arr); for(i=0;i<size;i++) { root = insertNode(root, arr[i]); } midTraverse(root); printf("\n"); Node* pNode = getNode(root,12); deleteNode(root, pNode); midTraverse(root); getchar(); getchar(); return 0; }
BtStatus insertOp(bt_addr_struct *bd_addr, PROFILE_enum id, ProfileOpType op_type) { profile_op_queue_t *ptr = NULL; dev_op *devOp = getDeviceOpList(bd_addr); if(devOp == NULL) { return BT_STATUS_FAILED; } ALOGI("%s addr:[0x%lX:0x%X:0x%X], profile[%d], op[%s]!\n", __FUNCTION__, bd_addr->lap, bd_addr->uap, bd_addr->nap, id, op_type?"Disconnect":"Connect"); ptr = (profile_op_queue_t *)calloc(1, sizeof(profile_op_queue_t)); if(NULL == ptr) { ALOGE("%s No Memory!", __FUNCTION__); return BT_STATUS_FAILED; } ptr->node.profile_id = id; ptr->node.op = op_type; insertNode((btmtk_list_header_struct **)&devOp->op_list, (btmtk_list_header_struct *)ptr); return BT_STATUS_SUCCESS; }
Graph * createPostForum(char * file,char * file2, int forumID) { FILE * fp = fopen(file,"r"); if (fp == NULL) perror ("Error opening file"); char buffer[200]; int i = 0, *creator,ID; Node * postNode; fgets(buffer,200,fp); Graph * postGraph = createGraph(M,C); while(fgets(buffer,200,fp)!= NULL) { ID = atoi(strtok (buffer,"|")); if(ID != forumID) continue; // add to post forum postNode = malloc(sizeof(Node)); postNode ->ID = atoi(strtok (NULL,"\n")); postNode->obj = NULL; insertNode(postNode,postGraph); i++; } fclose(fp); //finding posts creators fp = fopen(file2, "r"); //file2 = post_hasCreator_person if (fp == NULL) perror ("Error opening file"); fgets(buffer,200,fp); while(fgets(buffer,200,fp)!= NULL) { if((postNode = lookupNode(atoi(strtok (buffer,"|")), postGraph)) != NULL) { creator = malloc(sizeof(int)); *creator = atoi(strtok (NULL, "\n")); postNode->obj = creator; } } fclose(fp); return postGraph; }
void main() { node* root = NULL; root = insertNode(root,20); root = insertNode(root, 8); root = insertNode(root, 22); root = insertNode(root, 4); root = insertNode(root, 12); root= insertNode(root, 10); root = insertNode(root, 14); printTreePreOrder(root); printf("\n"); node* tmp = NULL; int currentK = 0; int k = 7; tmp = printKSmallest(root, ¤tK, &k); if (tmp!=NULL) { printf("%d \n", tmp->data); } }
node * create() { node * h; int ch; float c; int e; h = NULL;//important initialization do { printf("\n Enter coeff : "); scanf("%f", &c); printf("\n Enter exponent : "); scanf("%d", &e); h = insertNode(h, c, e); printf("\n Add more nodes (1 is yes) "); scanf("%d", &ch); }while(ch == 1); return h; }
void AABBTree::addBody(AbstractWorkBody *body, PairContainer *alternativePairContainer) { BodyNodeData *nodeData = new BodyNodeData(body, alternativePairContainer); AABBNode *nodeToInsert = new AABBNode(nodeData); if (rootNode!=nullptr) { nodeToInsert->updateAABBox(fatMargin); insertNode(nodeToInsert, rootNode); }else { rootNode = nodeToInsert; rootNode->updateAABBox(fatMargin); } bodiesNode[body] = nodeToInsert; computeOverlappingPairsFor(nodeToInsert, rootNode); #ifdef _DEBUG //printTree(rootNode, 0); #endif }