void checkfrom(mycell *obj) { int k; freeze = 1; /* suspend counting while checking graph */ checkobjcount = 0; checkloop(obj, 1); comment("checkfrom: %li live objects checked", checkobjcount); k = checkcomments; checkcomments = 0; checkloop(obj, 0); checkcomments = k; comment("checkfrom: graph ok from ID: %li.", obj->data.id); freeze = 0; /* resume counting */ }
static void checkloop(mycell *obj, int dir) { mycell *toj; int tid; int i; asserts(obj->tag == MCdata, "checkfrom: non data object in graph at %p.", obj); if (obj->data.checkedflag != dir) { commentif(checkcomments, "checking %p = %li", obj, obj->data.id); checkobjcount += 1; obj->data.checkedflag = dir; for (i=0; i<(obj->data.numrefs); i+=1) { if (obj->data.ref[i].addr != NULL) { toj = (obj->data.ref[i].addr); tid = (obj->data.ref[i].id); asserts(toj->data.id == tid, "checkfrom: corrupt graph at %p, %d.", obj, i); checkloop(toj, dir); } } } }
int main(void) { int in,o,d; char c,c1[2]; List ll = initialize(); do{ fflush(stdin); printf("Insert(i),Update(u),Delete(d),Print(p),CheckLoop(l)\n"); scanf("%c",&c); switch (c) { case 'i': { printf("Enter element\n"); scanf("%d",&in); ll = insert(ll,in); break; } case 'u': { printf("Original and update element\n"); scanf("%d%d",&o,&in); ll = update(ll,o,in); break; } case 'd': { printf("Element to delete\n"); scanf("%d",&d); ll = deleten(ll,d); break; } case 'p': { LINK curr = ll.Head; while(curr!=NULL) { printf("%d # ",curr->e); curr = curr->next; } printf("\n"); break; } case 'l': { checkloop(ll); } } printf("Continue(y/n)\n"); }while(scanf("%s",c1)&&c1[0]=='y'); return 0; }
//cat link void linkcat(link *pselect_link) { link *cur; int i; if(checkloop(pselect_link)!=0) { printf("loop detected!!\n"); return; } for(cur=pselect_link,i=0;cur!=NULL;cur=cur->next,i++) { printf("idx[%d]=%d \n",i,cur->data); } }