void removeLoop(struct node *start) { struct node *slow=start,*fast=start,*tmp=start; while(tmp->link!=NULL) { tmp=tmp->link; } tmp->link=start->link->link; if(hasLoop(start)) printf("List ha loop\n"); else printf("Doesn't contain loop\n"); while(slow&&fast&&fast->link) { slow=slow->link; fast=fast->link->link; if(slow==fast) { slow=start; fast=fast->link; while(slow->link!=fast) { slow=slow->link; fast=fast->link; } fast->link=NULL; } } if(hasLoop(start)) printf("List ha loop\n"); else printf("Doesn't contain loop\n"); }
node* unloop(node *root) { node *loopingNode(hasLoop(root)); if (loopingNode != NULL) { // We found a loop // Split the list into two parts // one: root to loopingNode // second: loopingNode->next to loopingNode // count nodes in first list (root) int rc(root->count(loopingNode)); node *preVNode(loopingNode); loopingNode = loopingNode->nextNode(); // count second list int lc(loopingNode->count(preVNode)); // Skip first if it is greater SKIP_NNODES(root, rc - lc); // skip second if it is greater and keep track of previous node for (int c(lc - rc); c > 0; c--) { preVNode = loopingNode; loopingNode = loopingNode->nextNode(); } // traverse both first and second list till they point to same node while (loopingNode != root) { root = root->nextNode(); preVNode = loopingNode; loopingNode = loopingNode->nextNode(); } // previous node in the second list is the actual last pointer in the list preVNode->next = NULL; loopingNode = preVNode; } return loopingNode; }
bool hasLoop(map<int, vector<int> > &table, const int &begin, vector<int> &path){ if (table.count(begin) == 0){ return false; } while (!table[begin].empty()){ int temp = table[begin].back(); if (find(path.begin(), path.end(), temp) != path.end()){ return true; } path.push_back(temp); if (hasLoop(table, temp, path)){ return true; } path.pop_back(); table[begin].pop_back(); } table.erase(begin); return false; }
int main(int argc, char *argv[]) { if (argc<2) return 0; //FILE *f = fopen(argv[1],"r"); //if (f==NULL) return 0; char* x = argv[1]; char* tmp ; tmp = strtok(x," "); struct List *l; newList(&l); struct Entry* node; struct Entry* n1 ; newNode(&n1); struct Entry* n2 ; newNode(&n2); struct Entry* n3 ; newNode(&n3); struct Entry* n4 ; newNode(&n4); int status = 0; struct Entry* e = l->head; while (tmp != (void*)0) { if (tmp[0] == '"') continue; if (strcmp(tmp,"N1")==0) node = n1; if (strcmp(tmp,"N2")==0) node = n2; if (strcmp(tmp,"N3")==0) node = n3; if (strcmp(tmp,"N4")==0) node = n4; if (strcmp(tmp,"H")==0) node = l->head; e->next = node; e = e->next; tmp = strtok((void*)0," "); } printf(" %d",hasLoop(l)); return 0; }
int main(int argc, char *argv[]) { if (argc<2) return 0; FILE *f = fopen(argv[1],"r"); if (f==NULL) return 0; struct List *l; newList(&l); char x[20]; struct Entry* node; struct Entry* n1 ; newNode(&n1); struct Entry* n2 ; newNode(&n2); struct Entry* n3 ; newNode(&n3); struct Entry* n4 ; newNode(&n4); int status = 0; struct Entry* e = l->head; while (fscanf(f,"%s",x)==1) { if (x[0] == '"') continue; if (strcmp(x,"N1")==0) node = n1; if (strcmp(x,"N2")==0) node = n2; if (strcmp(x,"N3")==0) node = n3; if (strcmp(x,"N4")==0) node = n4; if (strcmp(x,"H")==0) node = l->head; e->next = node; e = e->next; } fclose(f); printf(" %d",hasLoop(l)); return 0; }
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) { if (numCourses == 0 || numCourses == 1){ return true; } if (prerequisites.size() == 0){ return true; } map<int, vector<int> > m; for (auto &i : prerequisites){ m[i[0]].push_back(i[1]); } vector<int> path; while (!m.empty()){ auto it = m.begin(); path.push_back(it->first); if (hasLoop(m, it->first, path)){ return false; } path.pop_back(); } return true; }