int main(int argc, char* argv[]) { printf("Let us formulate the problem statement to understand the deletion process. Given a key, delete the first occurrence of this key in linked list.\nTo delete a node from linked list, we need to do following steps.\n1) Find previous node of the node to be deleted.\n2) Changed next of previous node.\n3) Free memory for the node to be deleted.\nSince every node of linked list is dynamically allocated using malloc() in C, we need to call free() for freeing memory allocated for the node to be deleted.\n"); //struct node *head = NULL; //head = (struct node*)malloc(sizeof(_node)); _node *head = NULL; insert_element(&head,1); insert_element(&head,2); display_list(head); // printf(" number of nodes = %d"); insert_element(&head,7); insert_element(&head,8); insert_element(&head,9); insert_element(&head,12); insert_element(&head,111); display_list(head); //delete_node(&head,111); display_list(head); insert_element(&head,13); insert_element(&head,15); //head->next->next->next->next->next->next = head->next->next; int give_answer = detect_loop(head); //delete_node(&head,1); //display_list(head); //delete_node(&head,12); //display_list(head); //delete_node(&head,8); //display_list(head); return 0; }
/* * Detects if the needle can be reached from either the haystack's * next or children. */ static int detect_loop(Container *haystack, Container *needle) { if (!haystack || !needle) return 0; if (haystack == needle) return 1; if (haystack->next && detect_loop(haystack->next, needle)) return 1; if (haystack->child && detect_loop(haystack->child, needle)) return 1; return 0; }
void main() { head=0; tail=0; add(1); add(2); add(3); add(4); add(5); add(6); add(6);add(7);add(8); display(); head->next->next->next->next = head->next->next; detect_loop(); printf("\t...%d",__LINE__); display(); }
static int detect_road_segment (int point_id, const RoadMapGpsPosition *gps_position, TrackNewSegment *new_segment, int max, int point_type) { int count; int road_type = TRACK_ROAD_REG; if (point_type & POINT_GAP) { road_type = TRACK_ROAD_CONNECTION; } count = detect_loop (point_id, gps_position, new_segment, max, road_type); if (count) return count; count = detect_turn (point_id, gps_position, new_segment, max, road_type); return count; }
int main() { node_t *tmp = NULL; node_t *root = get_node(10); root->next = get_node(20); root->next->next = get_node(30); root->next->next->next = get_node(40); root->next->next->next->next = get_node(50); root->next->next->next->next->next = get_node(60); root->next->next->next->next->next->next = root->next->next; //print_list(root); tmp = detect_loop(root); if(tmp) printf("Loop detectd at [%d]\n", tmp->data); else printf("NO LOOP DETECTED\n"); return 0; }
/* Name : LoopOpt_optimize_loop Description : A driver function for the loop optimization. Maintainer : Jinpyo Park <*****@*****.**> Pre-condition: Post-condition: This procedure returns (1) the first instruction in the sequence of moved instructions (first_moved_instr) and (2) the (possibly) changed loop instruction (new_loop_entry_instr). Notes: A successful loop optimization may create additional regions resulting from loop peeling and moved instructions at loop entry and exits. It may screw up the region informations and is so annoying problem to the register allocator which iterates register allocation region by region. Take a look at the trick used in register_allocator.c (if you are interested). */ int LoopOpt_optimize_loop(CFG* cfg, InstrNode* instr, InstrNode** first_moved_instr, InstrNode** new_loop_entry_instr, InstrNode*** additional_region_headers) { InstrNode* old_region_header; void* h_of_old_region_header; int i; bool is_region_dummy_necessary; /* loop detection */ if (!detect_loop(cfg, instr)) return -1; DBG({ extern int num; InlineGraph* IG_root = CFG_get_IG_root(cfg); Method* method = IG_GetMethod(IG_root); printf("Loop is found in %s:%s, (size=#%d) (num=%d)\n", Class_GetName(Method_GetDefiningClass(method))->data, Method_GetName(method)->data, loop_instr_array_size, num); });
/* * Calculates the threading relationships for a list of messages */ Container *calculate_threads(apr_pool_t *p, MBOX_LIST *l) { apr_hash_t *h, *rootSet, *subjectSet; apr_hash_index_t *hashIndex; MBOX_LIST *current = l; const apr_array_header_t *refHdr; apr_table_entry_t *refEnt; Message *m; Container *c, *subjectPair, *realParent, *curParent, *tmp; void *hashKey, *hashVal, *subjectVal; char *subject; int msgIDLen, refLen, i; apr_ssize_t hashLen, subjectLen; /* FIXME: Use APR_HASH_KEY_STRING instead? Maybe slower. */ h = apr_hash_make(p); while (current != NULL) { m = (Message *) current->value; msgIDLen = strlen(m->msgID); c = (Container *) apr_hash_get(h, m->msgID, msgIDLen); if (c) { c->message = m; } else { c = (Container *) apr_pcalloc(p, sizeof(Container)); c->message = m; c->parent = NULL; c->child = NULL; c->next = NULL; apr_hash_set(h, m->msgID, msgIDLen, c); } realParent = NULL; if (m->references) { refHdr = apr_table_elts(m->references); refEnt = (apr_table_entry_t *) refHdr->elts; for (i = 0; i < refHdr->nelts; i++) { refLen = strlen(refEnt[i].key); curParent = (Container *) apr_hash_get(h, refEnt[i].key, refLen); /* Create a dummy node to store the message we haven't * yet seen. */ if (!curParent) { curParent = (Container *) apr_pcalloc(p, sizeof(Container)); apr_hash_set(h, refEnt[i].key, refLen, curParent); } /* Check to make sure we are not going to create a loop * by adding this parent to our list. */ if (realParent && !detect_loop(curParent, realParent) && !detect_loop(realParent, curParent)) { /* Update the parent */ if (curParent->parent) unlink_parent(curParent); curParent->parent = realParent; curParent->next = realParent->child; realParent->child = curParent; } /* We now have a new parent */ realParent = curParent; } } /* The last parent we saw is our parent UNLESS it causes a loop. */ if (realParent && !detect_loop(c, realParent) && !detect_loop(realParent, c)) { /* We need to unlink our parent's link to us. */ if (c->parent) unlink_parent(c); c->parent = realParent; c->next = realParent->child; realParent->child = c; } current = current->next; } /* Find the root set */ rootSet = apr_hash_make(p); for (hashIndex = apr_hash_first(p, h); hashIndex; hashIndex = apr_hash_next(hashIndex)) { apr_hash_this(hashIndex, (void *) &hashKey, &hashLen, &hashVal); c = (Container *) hashVal; if (!c->parent) apr_hash_set(rootSet, hashKey, hashLen, c); } /* Prune empty containers */ for (hashIndex = apr_hash_first(p, rootSet); hashIndex; hashIndex = apr_hash_next(hashIndex)) { apr_hash_this(hashIndex, (void *) &hashKey, &hashLen, &hashVal); c = (Container *) hashVal; prune_container(c); if (!c->message && !c->child) apr_hash_set(rootSet, hashKey, hashLen, NULL); } /* Merge root set by subjects */ subjectSet = apr_hash_make(p); for (hashIndex = apr_hash_first(p, rootSet); hashIndex; hashIndex = apr_hash_next(hashIndex)) { apr_hash_this(hashIndex, (void *) &hashKey, &hashLen, &hashVal); c = (Container *) hashVal; /* If we don't have a message, our child will. */ if (!c->message) c = c->child; subject = strip_subject(p, c->message); subjectLen = strlen(subject); /* FIXME: Match what JWZ says */ subjectVal = apr_hash_get(subjectSet, subject, subjectLen); if (subjectVal) { if (!c->message) apr_hash_set(subjectSet, subject, strlen(subject), hashVal); else { subjectPair = (Container *) subjectVal; if (!is_reply(c->message) && is_reply(subjectPair->message)) apr_hash_set(subjectSet, subject, strlen(subject), hashVal); } } else apr_hash_set(subjectSet, subject, strlen(subject), hashVal); } /* Subject table now populated */ for (hashIndex = apr_hash_first(p, rootSet); hashIndex; hashIndex = apr_hash_next(hashIndex)) { apr_hash_this(hashIndex, (void *) &hashKey, &hashLen, &hashVal); c = (Container *) hashVal; /* If we don't have a message, our child will. */ if (c->message) subject = strip_subject(p, c->message); else subject = strip_subject(p, c->child->message); subjectLen = strlen(subject); subjectVal = apr_hash_get(subjectSet, subject, subjectLen); subjectPair = (Container *) subjectVal; /* If we need to merge the tables */ if (subjectPair && subjectPair != c) { if (!c->message || !subjectPair->message) { /* One is dummy */ if (!c->message && !subjectPair->message) join_container(subjectPair, c); else if (c->message && !subjectPair->message) { /* It's possible that we're already a child! */ if (c->parent != subjectPair) append_container(subjectPair, c); } else { /* (!c->message && subjectPair->message) */ append_container(c, subjectPair); apr_hash_set(subjectSet, subject, subjectLen, c); delete_from_hash(p, rootSet, subjectPair); } } else { /* Both aren't dummies */ /* We are Reply */ if (is_reply(c->message) && !is_reply(subjectPair->message)) append_container(subjectPair, c); else if (!is_reply(c->message) && is_reply(subjectPair->message)) { append_container(c, subjectPair); apr_hash_set(subjectSet, subject, subjectLen, c); delete_from_hash(p, rootSet, subjectPair); } else { /* We are both replies. */ c = merge_container(p, c, subjectPair); apr_hash_set(subjectSet, subject, subjectLen, c); delete_from_hash(p, rootSet, subjectPair); } } } } /* Now, we are done threading. We want to return a sorted container * back to our caller. All children of the root set need to be in * order and then we need to issue an ordering to the root set. */ tmp = NULL; /* Sort siblings */ for (hashIndex = apr_hash_first(p, subjectSet); hashIndex; hashIndex = apr_hash_next(hashIndex)) { apr_hash_this(hashIndex, (void *) &hashKey, &hashLen, &hashVal); c = (Container *) hashVal; sort_siblings(c); if (tmp) c->next = tmp; tmp = c; } return (Container *) mbox_sort_linked_list(tmp, 3, compare_siblings, NULL, NULL); }