コード例 #1
0
void test_detectCycle()
{
    {
        int a[] = { 1, 2 };
        ListNode *p1 = createList(a, sizeof(a) / sizeof(a[0]));

        ListNode *s = p1;
        ListNode *e = p1->next;
        e->next = s;

        ListNode *inters = detectCycle(p1);
        if (inters)
            printf("get intersect %d\n", inters->val);
    }
    {
        int a[] = { 1, 2, 3, 4, 5 };
        ListNode *p1 = createList(a, sizeof(a) / sizeof(a[0]));

        ListNode *s = p1->next->next;
        ListNode *e = p1->next->next->next->next;
        e->next = s;

        ListNode *inters = detectCycle(p1);
        if (inters)
            printf("get intersect %d\n", inters->val);
    }
    {
        int a[] = { 1, 2 };
        ListNode *p1 = createList(a, sizeof(a) / sizeof(a[0]));

        ListNode *s = p1->next;
        ListNode *e = s;
        e->next = s;

        ListNode *inters = detectCycle(p1);
        if (inters)
            printf("get intersect %d\n", inters->val);
    }

    if (1)
    {
        int a[] = { 2, 2, 3, 4 };
        ListNode *p1 = createList(a, sizeof(a) / sizeof(a[0]));

        ListNode *inters = detectCycle(p1);
        if (inters)
            printf("get intersect %d\n", inters->val);
    }
}
コード例 #2
0
ファイル: graph.c プロジェクト: nttputus/blensor
static int detectCycle(BNode *node, BArc *src_arc)
{
	int value = 0;
	
	if (node->flag == 0) {
		int i;

		/* mark node as visited */
		node->flag = 1;

		for (i = 0; i < node->degree && value == 0; i++) {
			BArc *arc = node->arcs[i];
			
			/* don't go back on the source arc */
			if (arc != src_arc) {
				value = detectCycle(BLI_otherNode(arc, node), arc);
			}
		}
	}
	else {
		value = 1;
	}
	
	return value;
}
コード例 #3
0
ファイル: iterative.c プロジェクト: vodkaice/quiz
int main()
{
    struct timespec start, end;
    double cpu_time;
    LinkNode *head = CreateNode();
    assert((head->next != NULL && head->next->next !=NULL) && "no cycle");
    clock_gettime(CLOCK_REALTIME, &start);
    LinkNode *res = detectCycle(head);
    clock_gettime(CLOCK_REALTIME, &end);
    cpu_time = diff_in_second(start, end);
    //use print to check the list 
    /*printf("%d->%d->%d->%d->%d->%d->%d\n",head->data,
				    head->next->data,
				    head->next->next->data,
				    head->next->next->next->data,
				    head->next->next->next->next->data,
				    head->next->next->next->next->next->data,
				    head->next->next->next->next->next->next->data);*/
    if (res != NULL)
	//printf("result: n%d\n",res->data);
	printf("execution time: %.10lf sec\n", cpu_time);
    else
	printf("NULL\n");
    return 0;
}
コード例 #4
0
void c_AsyncFunctionWaitHandle::prepareChild(c_WaitableWaitHandle* child) {
  assert(!child->isFinished());

  // import child into the current context, throw on cross-context cycles
  asio::enter_context(child, getContextIdx());

  // detect cycles
  detectCycle(child);
}
コード例 #5
0
int main() {
	struct ListNode *nil = NULL;
	assert(NULL == detectCycle(nil));

	struct ListNode n = { 0, &n };
	assert(&n == detectCycle(&n));

	struct ListNode n3 = { 0, NULL };
	struct ListNode n2 = { 0, &n3 };
	struct ListNode n1 = { 0, &n2 };
	n3.next = &n2;
	assert(&n2 == detectCycle(&n1));

	n.next = NULL;
	assert(NULL == detectCycle(&n));

	return 0;
}
コード例 #6
0
ファイル: solve.c プロジェクト: beyondlte/leetcode-3
int main(int argc, char **argv)
{
	struct ListNode *head;
	int a[] = {1,2,3,4,5};
	mk_list(&head, a, 5);
	//head->next->next = head;
	printf("%d\n", hasCycle(head));
	printf("%d\n", detectCycle(head)->val);
	return 0;
}
コード例 #7
0
ファイル: recursive.c プロジェクト: GaliTW/quiz
ListNode *detectCycle(ListNode *head)
{
    if (head == NULL)
        return NULL;
    if (head->pNext == (ListNode *)detectCycle)
        return head;

    ListNode *temp = head->pNext;
    head->pNext = (ListNode *)detectCycle;

    return detectCycle(temp);
}
コード例 #8
0
ファイル: 142.c プロジェクト: wuhao4u/InterviewPractice
int main(int argc, char const *argv[])
{
    struct ListNode* head = BuildByLength(2);
    struct ListNode* tail = head;
    while(tail->next != NULL) {
        tail = tail->next;
    }
    tail->next = head->next;

    if(detectCycle(head) == NULL)
        printf("no cycle found!\n");
    else
        printf("%d\n", (detectCycle(head))->val);

    head = BuildByLength(2);
    if(detectCycle(head) == NULL)
        printf("no cycle found!\n");
    else
        printf("%d\n", (detectCycle(head))->val);

    return 0;
}
コード例 #9
0
ファイル: recursive.c プロジェクト: Joouis/quiz
int main()
{
    ListNode *root = (ListNode *)malloc(sizeof(ListNode));
    root->val = 1;
    creat_tree(root);
    ListNode *cycle_begin = (ListNode *)detectCycle(root);
    if ( cycle_begin  ) {
        printf( "Yes the cycle begins at No.%d node!\n", cycle_begin->val );
    } else {
        printf( "There is no cycle!\n" );
    }
    return 0;
}
コード例 #10
0
ファイル: recursive.c プロジェクト: Joouis/quiz
ListNode *detectCycle(ListNode *node)
{
    if ( !node || !node->next) {
        return NULL;
    }
    if ( find(node)  ) {
        return node;
    } else {
        return detectCycle(node->next);
    }

    return NULL;
}
コード例 #11
0
ファイル: gen_map_wait_handle.cpp プロジェクト: brucezy/hhvm
void c_GenMapWaitHandle::onUnblocked() {
  assert(getState() == STATE_BLOCKED);

  for (;
       m_deps->iter_valid(m_iterPos);
       m_iterPos = m_deps->iter_next(m_iterPos)) {

    auto* current = tvAssertCell(m_deps->iter_value(m_iterPos));
    assert(current->m_type == KindOfObject);
    assert(current->m_data.pobj->instanceof(c_WaitHandle::classof()));
    auto child = static_cast<c_WaitHandle*>(current->m_data.pobj);

    if (child->isSucceeded()) {
      auto k = m_deps->iter_key(m_iterPos);
      m_deps->set(k.asCell(), &child->getResult());
    } else if (child->isFailed()) {
      putException(m_exception, child->getException());
    } else {
      assert(child->instanceof(c_WaitHandle::classof()));
      auto child_wh = static_cast<c_WaitableWaitHandle*>(child);

      try {
        if (isInContext()) {
          child_wh->enterContext(getContextIdx());
        }
        detectCycle(child_wh);
        blockOn(child_wh);
        return;
      } catch (const Object& cycle_exception) {
        putException(m_exception, cycle_exception.get());
      }
    }
  }

  auto const parentChain = getFirstParent();
  if (m_exception.isNull()) {
    setState(STATE_SUCCEEDED);
    tvWriteObject(m_deps.get(), &m_resultOrException);
  } else {
    setState(STATE_FAILED);
    tvWriteObject(m_exception.get(), &m_resultOrException);
    m_exception = nullptr;
  }

  m_deps = nullptr;
  UnblockChain(parentChain);
  decRefObj(this);
}
コード例 #12
0
void c_AwaitAllWaitHandle::blockOnCurrent() {
  auto child = m_children[m_cur];
  assert(!child->isFinished());

  if (checkCycle) {
    try {
      detectCycle(child);
    } catch (const Object& cycle_exception) {
      markAsFailed(cycle_exception);
      return;
    }
  }

  child->getParentChain()
    .addParent(m_blockable, AsioBlockable::Kind::AwaitAllWaitHandle);
}
コード例 #13
0
ファイル: main.cpp プロジェクト: qiannn/leetcode
int main(int argc, const char * argv[]) {
    // insert code here...
    std::cout << "Hello, World!\n";
    ListNode* node1 = new ListNode(1);
    ListNode* node2 = new ListNode(2);
    ListNode* node3 = new ListNode(3);
    ListNode* node4 = new ListNode(4);
    node1->next = NULL;
    node2->next = node3;
    node3->next = node4;
    node4->next = node2;
    ListNode *p = detectCycle(node1);
    if (p) std::cout << p->val << std::endl;
    else std::cout << "ops" << std::endl;
    return 0;
}
コード例 #14
0
ファイル: await_all_wait_handle.cpp プロジェクト: bjori/hhvm
void c_AwaitAllWaitHandle::blockOnCurrent() {
  auto child = m_children[m_cur];
  assert(!child->isFinished());

  try {
    if (isInContext()) {
      child->enterContext(getContextIdx());
    }
    if (checkCycle) {
      detectCycle(child);
    }
  } catch (const Object& cycle_exception) {
    markAsFailed(cycle_exception);
    return;
  }

  blockOn(child);
}
コード例 #15
0
void c_GenVectorWaitHandle::onUnblocked() {
  assert(getState() == STATE_BLOCKED);

  for (; m_iterPos < m_deps->size(); ++m_iterPos) {

    Cell* current = tvAssertCell(m_deps->at(m_iterPos));
    assert(current->m_type == KindOfObject);
    assert(current->m_data.pobj->instanceof(c_WaitHandle::classof()));
    auto child = static_cast<c_WaitHandle*>(current->m_data.pobj);

    if (child->isSucceeded()) {
      auto result = child->getResult();
      m_deps->set(m_iterPos, &result);
    } else if (child->isFailed()) {
      putException(m_exception, child->getException());
    } else {
      assert(child->instanceof(c_WaitableWaitHandle::classof()));
      auto child_wh = static_cast<c_WaitableWaitHandle*>(child);

      try {
        detectCycle(child_wh);
        child_wh->getParentChain()
          .addParent(m_blockable, AsioBlockable::Kind::GenVectorWaitHandle);
        return;
      } catch (const Object& cycle_exception) {
        putException(m_exception, cycle_exception.get());
      }
    }
  }

  auto parentChain = getParentChain();
  if (m_exception.isNull()) {
    setState(STATE_SUCCEEDED);
    tvWriteObject(m_deps.get(), &m_resultOrException);
  } else {
    setState(STATE_FAILED);
    tvWriteObject(m_exception.get(), &m_resultOrException);
    m_exception = nullptr;
  }

  m_deps = nullptr;
  parentChain.unblock();
  decRefObj(this);
}
コード例 #16
0
ファイル: recursive.c プロジェクト: vodkaice/quiz
int main()
{
    LinkNode *head = CreateNode();
    _head = head;
    LinkNode *res = detectCycle(head);
    //use print to check the list
    /*printf("%d->%d->%d->%d->%d->%d->%d\n",head->data,
    			    head->next->data,
    			    head->next->next->data,
    			    head->next->next->next->data,
    			    head->next->next->next->next->data,
    			    head->next->next->next->next->next->data,
    			    head->next->next->next->next->next->next->data);*/
    if (res != NULL)
        printf("result: n%d\n",res->data);
    else
        printf("NULL\n");
    return 0;
}
コード例 #17
0
ファイル: recursive.c プロジェクト: vodkaice/quiz
LinkNode *detectCycle(LinkNode *head) {
    assert((head->next != NULL && head->next->next !=NULL) && "no cycle");
    LinkNode *now = head;
    LinkNode *temp = now->next;
    if(temp->next != _head && temp->next != NULL) {
        now->next = _head;
        now = temp;
        temp = temp->next;
        return detectCycle(now);
    }
    else if(temp->next == NULL) {
        printf("no cycle\n");
        return NULL;
    }
    else {
        printf("cycle detected!\n");
        return temp;
    }
}
コード例 #18
0
void c_GenVectorWaitHandle::onUnblocked() {
  assert(getState() == STATE_BLOCKED);

  for (; m_iterPos < m_deps->size(); ++m_iterPos) {

    Cell* current = tvAssertCell(m_deps->at(m_iterPos));
    assert(current->m_type == KindOfObject);
    assert(current->m_data.pobj->instanceof(c_WaitHandle::classof()));
    auto child = static_cast<c_WaitHandle*>(current->m_data.pobj);

    if (child->isSucceeded()) {
      cellSet(child->getResult(), *current);
    } else if (child->isFailed()) {
      putException(m_exception, child->getException());
    } else {
      assert(child->instanceof(c_WaitableWaitHandle::classof()));
      auto child_wh = static_cast<c_WaitableWaitHandle*>(child);

      try {
        if (isInContext()) {
          child_wh->enterContext(getContextIdx());
        }
        detectCycle(child_wh);
        blockOn(child_wh);
        return;
      } catch (const Object& cycle_exception) {
        putException(m_exception, cycle_exception.get());
      }
    }
  }

  if (m_exception.isNull()) {
    setState(STATE_SUCCEEDED);
    tvWriteObject(m_deps.get(), &m_resultOrException);
  } else {
    setState(STATE_FAILED);
    tvWriteObject(m_exception.get(), &m_resultOrException);
    m_exception = nullptr;
  }

  m_deps = nullptr;
  done();
}
コード例 #19
0
ファイル: recursive.c プロジェクト: extraclean/quiz
int main()
{
    ListNode *input = (ListNode *)malloc(sizeof(ListNode));
    ListNode *input2 = (ListNode *)malloc(sizeof(ListNode));
    ListNode *input3 = (ListNode *)malloc(sizeof(ListNode));
    ListNode *input4 = (ListNode *)malloc(sizeof(ListNode));
    ListNode *input5 = (ListNode *)malloc(sizeof(ListNode));
    ListNode *input6 = (ListNode *)malloc(sizeof(ListNode));

    input->val=1;
    input2->val=2;
    input3->val=3;
    input4->val=4;
    input5->val=5;
    input6->val=6;

    input->next=input2;
    input2->next=input3;
    input3->next=input4;
    input4->next=input5;
    input5->next=input6;
    input6->next=input2;

    printf("input: ");
    printf("%d",input->val);//1
    printf("->");
    printf("%d",input->next->val);//2
    printf("->");
    printf("%d",input->next->next->val);//3
    printf("->");
    printf("%d",input->next->next->next->val);//4
    printf("->");
    printf("%d",input->next->next->next->next->val);//5
    printf("->");
    printf("%d",input->next->next->next->next->next->val);//6
    printf("->");
    printf("%d\n",input->next->val);//2

    printf("result: ");
    printf("%d\n",detectCycle(input,input,input));
    return 0;
}
コード例 #20
0
ファイル: graph.c プロジェクト: nttputus/blensor
int	BLI_isGraphCyclic(BGraph *graph)
{
	BNode *node;
	int value = 0;
	
	/* NEED TO CHECK IF ADJACENCY LIST EXIST */
	
	/* Mark all nodes as not visited */
	BLI_flagNodes(graph, 0);

	/* detectCycles in subgraphs */	
	for (node = graph->nodes.first; node && value == 0; node = node->next) {
		/* only for nodes in subgraphs that haven't been visited yet */
		if (node->flag == 0) {
			value = value || detectCycle(node, NULL);
		}		
	}
	
	return value;
}
コード例 #21
0
ファイル: DetectLoop.cpp プロジェクト: sakshi-chauhan/DSCodes
int main(){
	
	node *head = NULL;
	insert( &head , 12 );
	insert( &head , 2 );
	insert( &head , 26 );
	insert( &head , 32 );
	insert( &head , 50 );
	printlist( head );
	
	head -> next -> next -> next -> next -> next = head -> next;
	
	if( detectCycle( head ) )
		std::cout<<"There's a loop in the list";
	
	else
		std::cout<<"No LOOP";
	
	//printlist( head );
}
コード例 #22
0
void c_GenMapWaitHandle::onUnblocked() {
  for (;
       m_deps->iter_valid(m_iterPos);
       m_iterPos = m_deps->iter_next(m_iterPos)) {

    Cell* current = tvAssertCell(m_deps->iter_value(m_iterPos));
    assert(current->m_type == KindOfObject);
    assert(current->m_data.pobj->instanceof(c_WaitHandle::classof()));
    auto child = static_cast<c_WaitHandle*>(current->m_data.pobj);

    if (child->isSucceeded()) {
      cellSet(child->getResult(), *current);
    } else if (child->isFailed()) {
      putException(m_exception, child->getException());
    } else {
      assert(child->instanceof(c_WaitHandle::classof()));
      auto child_wh = static_cast<c_WaitableWaitHandle*>(child);

      try {
        if (isInContext()) {
          child_wh->enterContext(getContextIdx());
        }
        detectCycle(child_wh);
        blockOn(child_wh);
        return;
      } catch (const Object& cycle_exception) {
        putException(m_exception, cycle_exception.get());
      }
    }
  }

  if (m_exception.isNull()) {
    setResult(make_tv<KindOfObject>(m_deps.get()));
    m_deps = nullptr;
  } else {
    setException(m_exception.get());
    m_exception = nullptr;
    m_deps = nullptr;
  }
}
コード例 #23
0
ファイル: recursive.c プロジェクト: extraclean/quiz
int detectCycle(ListNode *head,ListNode *p,ListNode *q) {
    if(!head)
        return 0;

    if(p->next!=NULL)
        p=p->next;
    else
        return 0;
    if(q->next!=NULL && q->next->next!=NULL)
        q=q->next->next;
    else
        return 0;
    if (p==q) {
        q=head;
        while (p!=q) {
            p=p->next;
            q=q->next;
        }
        return p->val;
    }
    detectCycle(head,p,q);
}
コード例 #24
0
void c_AwaitAllWaitHandle::onUnblocked(uint32_t idx) {
  assert(idx <= m_unfinished);
  assert(getState() == STATE_BLOCKED);

  if (idx == m_unfinished) {
    for (uint32_t next = idx - 1; next < idx; --next) {
      auto const child = m_children[next].m_child;
      if (!child->isFinished()) {
        // Found the next unfinished child.
        m_unfinished = next;

        // Make sure there's no cyclic dependencies.
        try {
          detectCycle(child);
        } catch (const Object& cycle_exception) {
          markAsFailed(cycle_exception);
        }
        return;
      }
    }
    // All children finished.
    markAsFinished();
  }
}