/** * [intersectionPoint Returns the point of intersection of two lists] * @param head1 [ head of list 1 ] * @param head2 [ head of list 2 ] * @return [ Intersecting node, if lists intersect, else nullptr] */ Node * intersectionPoint( Node * head1, Node * head2 ) { int len1 = listLen(head1); int len2 = listLen(head2); //figure out the bigger list ( and smaller ) //ptr points to bigger list, let us move the difference //between the two. Node * ptr1 = ( len1 > len2 ) ? head1 : head2; Node * ptr2 = ( len1 > len2 ) ? head2 : head1; int i = 0; while ( i < std::abs(len1 - len2) && ptr1 ) { ptr1 = ptr1->next; ++i; } //Now we have equal nodes to travel on both the nodes // traversing and comparing the pointers. while( ptr1 && ptr2 ) { if ( ptr1 == ptr2 ) { return ptr1; } ptr1 = ptr1->next; ptr2 = ptr2->next; } return nullptr; }
/*! \fn checkForStateEvent * * \param [ref] [data] * \param [ref] [eventList] * * This function checks for events in interval=[oldTime, timeValue] * If a zero crossing function cause a sign change, root finding * process will start */ int checkForStateEvent(DATA* data, LIST *eventList) { TRACE_PUSH long i=0; debugStreamPrint(LOG_EVENTS, 1, "check state-event zerocrossing at time %g", data->localData[0]->timeValue); for(i=0; i<data->modelData.nZeroCrossings; i++) { int *eq_indexes; const char *exp_str = data->callback->zeroCrossingDescription(i,&eq_indexes); debugStreamPrintWithEquationIndexes(LOG_EVENTS, 1, eq_indexes, "%s", exp_str); if(sign(data->simulationInfo.zeroCrossings[i]) != sign(data->simulationInfo.zeroCrossingsPre[i])) { debugStreamPrint(LOG_EVENTS, 0, "changed: %s", (data->simulationInfo.zeroCrossingsPre[i] > 0) ? "TRUE -> FALSE" : "FALSE -> TRUE"); listPushFront(eventList, &(data->simulationInfo.zeroCrossingIndex[i])); } else { debugStreamPrint(LOG_EVENTS, 0, "unchanged: %s", (data->simulationInfo.zeroCrossingsPre[i] > 0) ? "TRUE -- TRUE" : "FALSE -- FALSE"); } if (DEBUG_STREAM(LOG_EVENTS)) messageClose(LOG_EVENTS); } if (DEBUG_STREAM(LOG_EVENTS)) messageClose(LOG_EVENTS); if(listLen(eventList) > 0) { TRACE_POP return 1; }
node* bubbleSort( node *head){ if ( NULL == head || NULL == head->next) return head; node *m, *n; int flag, len = listLen(head); int j,i = len -1; while( i > 0 ){ printf("bs- "); listPrint( head ); m = head; n = head->next; flag = 0; for( j= 0 ; j < i; ++j ) { if ( n->data < m->data) { int tmp = n->data; n->data = m->data; m->data = tmp; flag = 1; } m = m->next; n = n->next; } if( 0 == flag ) break; --i; } return head; }
ListNode* rotateRight(ListNode* head, int k) { if (head == NULL) { return head; } int len = listLen(head); k = k % len; if (k == 0) { return head; } ListNode* node2 = head; while (k-- && node2) { node2 = node2->next; } ListNode* node1 = head; while (node2 && node2->next) { node1 = node1->next; node2 = node2->next; } ListNode* newHead = NULL; if (node2 == NULL || node2 == head) { newHead = head; } else { newHead = node1->next; node2->next = head; node1->next = NULL; } return newHead; }
/*! \fn updateInitialGuessDB * * This function writes new values to solution list. * * \param [in] [nonlinsys] * \param [in] [time] time for extrapolation * \param [in] [context] current context of evaluation * */ int updateInitialGuessDB(NONLINEAR_SYSTEM_DATA *nonlinsys, double time, int context) { /* write solution to oldValue list for extrapolation */ if (nonlinsys->solved == 1) { /* do not use solution of jacobian for next extrapolation */ if (context < 4) { addListElement((VALUES_LIST*)nonlinsys->oldValueList, createValueElement(nonlinsys->size, time, nonlinsys->nlsx)); } } else if (nonlinsys->solved == 2) { if (listLen(((VALUES_LIST*)nonlinsys->oldValueList)->valueList)>0) { cleanValueList((VALUES_LIST*)nonlinsys->oldValueList, NULL); } /* do not use solution of jacobian for next extrapolation */ if (context < 4) { addListElement((VALUES_LIST*)nonlinsys->oldValueList, createValueElement(nonlinsys->size, time, nonlinsys->nlsx)); } } messageClose(LOG_NLS_EXTRAPOLATE); return 0; }
ListNode* removeNthFromEnd(ListNode* head, int n) { int p = listLen(head) + 1 - n; if (p == 1) return head -> next; ListNode* remove = head; for (int i = 0; i < p - 2; i++) remove = remove -> next; remove -> next = remove -> next -> next; return head; }
void alarm_handler() { if (listLen(&list) == 0) { alarm(100); return; } decrementaTtl(&list); alarm(100); return; }
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { int len1 = listLen(l1); int len2 = listLen(l2); int maxLen = len1 > len2 ? len1 : len2; int minLen = len1 > len2 ? len2 : len1; ListNode* longList = len1 > len2 ? l1 : l2; ListNode* shortList = len1 > len2 ? l2 : l1; ListNode* resList = new ListNode(0); ListNode* p = resList; int val1(0), val2(0), add(0), n(0); for (int i = 0; i < maxLen; i++) { val1 = longList->val; val2 = i < minLen ? shortList->val : 0; n = val1 + val2 + add; if (n > 9) { add = 1; n -= 10; } else add = 0; p->next = new ListNode(n); p = p->next; longList = longList->next; if (i < minLen) shortList = shortList->next; } if (add == 1) p->next = new ListNode(1); return resList->next; }
ListNode* sortList(ListNode* head) { int len = listLen(head); for (int subRange = 1; subRange < len; subRange *= 2) { ListNode* remain = head; ListNode* newHead = NULL; ListNode** newTail = &newHead; while (remain != NULL) { ListNode* L1 = takeN(remain, subRange); ListNode* L2 = takeN(remain, subRange); mergeToTail(L1, L2, newTail); } head = newHead; } return head; }
int main() { //new a new list node *p,*head; int d,deld,insertd; p = head = NULL; while( scanf("%d",&d) ) { if( 0 == d ) break; node *n = (node*) malloc(sizeof(node)); n->data = d; n->next = NULL; if (NULL == head) p = head = n; p->next = n; p = n; } listLen( head ); listPrint( head ); printf("del?"); scanf("%d",&deld); head = delByData(head, deld); listPrint( head ); printf("insert?"); scanf("%d",&insertd); head = insert(head, insertd); listPrint( head ); printf("reverse\n"); head = reverse (head ); listPrint( head ); printf("sort\n"); //head = selSort( head ); head = bubbleSort( head ); listPrint( head ); listFree( head ); return 0; }
/*! \fn getInitialGuess * * This function writes initial guess to nonlinsys->nlsx and nonlinsys->nlsOld. * * \param [in] [nonlinsys] * \param [in] [time] time for extrapolation * */ int getInitialGuess(NONLINEAR_SYSTEM_DATA *nonlinsys, double time) { /* value extrapolation */ printValuesListTimes((VALUES_LIST*)nonlinsys->oldValueList); /* if list is empty use current start values */ if (listLen(((VALUES_LIST*)nonlinsys->oldValueList)->valueList)==0) { /* use old value if no values are stored in the list */ memcpy(nonlinsys->nlsx, nonlinsys->nlsxOld, nonlinsys->size*(sizeof(double))); } else { /* get extrapolated values */ getValues((VALUES_LIST*)nonlinsys->oldValueList, time, nonlinsys->nlsxExtrapolation, nonlinsys->nlsxOld); memcpy(nonlinsys->nlsx, nonlinsys->nlsxOld, nonlinsys->size*(sizeof(double))); } return 0; }
/*! \fn solve non-linear systems * * \param [in] [data] * \param [in] [sysNumber] index of corresponding non-linear system * * \author wbraun */ int solve_nonlinear_system(DATA *data, threadData_t *threadData, int sysNumber) { void *dataAndThreadData[2] = {data, threadData}; int success = 0, saveJumpState; NONLINEAR_SYSTEM_DATA* nonlinsys = &(data->simulationInfo->nonlinearSystemData[sysNumber]); struct dataNewtonAndHybrid *mixedSolverData; data->simulationInfo->currentNonlinearSystemIndex = sysNumber; /* enable to avoid division by zero */ data->simulationInfo->noThrowDivZero = 1; ((DATA*)data)->simulationInfo->solveContinuous = 1; rt_ext_tp_tick(&nonlinsys->totalTimeClock); /* value extrapolation */ infoStreamPrint(LOG_NLS_EXTRAPOLATE, 1, "############ Start new iteration for system %d at time at %g ############", sysNumber, data->localData[0]->timeValue); printValuesListTimes((VALUES_LIST*)nonlinsys->oldValueList); /* if list is empty use current start values */ if (listLen(((VALUES_LIST*)nonlinsys->oldValueList)->valueList)==0) { //memcpy(nonlinsys->nlsxOld, nonlinsys->nlsx, nonlinsys->size*(sizeof(double))); //memcpy(nonlinsys->nlsxExtrapolation, nonlinsys->nlsx, nonlinsys->size*(sizeof(double))); memcpy(nonlinsys->nlsx, nonlinsys->nlsxOld, nonlinsys->size*(sizeof(double))); } else { /* get extrapolated values */ getValues((VALUES_LIST*)nonlinsys->oldValueList, data->localData[0]->timeValue, nonlinsys->nlsxExtrapolation, nonlinsys->nlsxOld); memcpy(nonlinsys->nlsx, nonlinsys->nlsxOld, nonlinsys->size*(sizeof(double))); } if(data->simulationInfo->discreteCall) { double *fvec = malloc(sizeof(double)*nonlinsys->size); int success = 0; #ifndef OMC_EMCC /* try */ MMC_TRY_INTERNAL(simulationJumpBuffer) #endif ((DATA*)data)->simulationInfo->solveContinuous = 0; nonlinsys->residualFunc((void*) dataAndThreadData, nonlinsys->nlsx, fvec, (int*)&nonlinsys->size); ((DATA*)data)->simulationInfo->solveContinuous = 1; success = 1; memcpy(nonlinsys->nlsxExtrapolation, nonlinsys->nlsx, nonlinsys->size*(sizeof(double))); #ifndef OMC_EMCC /*catch */ MMC_CATCH_INTERNAL(simulationJumpBuffer) #endif if (!success) { warningStreamPrint(LOG_STDOUT, 0, "Non-Linear Solver try to handle a problem with a called assert."); } free(fvec); }