CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2004
    Posts
    12

    Checking the circularity of a singly linked list

    Can anyone tell me the logic for:
    Checking the circularity of a singly linked list? The logic should not depend on the length of the list. if the list is partially circular (eg last node pointing to 4th node), the logic should not fail and logic should have a complexity of constant.

    Thanks,
    buddy

  2. #2
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815
    Quote Originally Posted by AnotherBuddy
    Checking the circularity of a singly linked list? The logic should not depend on the length of the list. if the list is partially circular (eg last node pointing to 4th node), the logic should not fail and logic should have a complexity of constant.
    It should be obvious that an existing linked list can't be checked in constant time. However, you could maintain a flag in each node which indicates whether the node is already referenced, this would allow to check for circular references upon insertion. But anyway, this is a rather academic discussion (which is not what this forum is for). For any practical purpose, use std::list or CList: Since the linking logic is an implementation detail of those containers, the interface won't even allow you to create circular references. So I wonder why you want to reinvent the wheel and implement a linked list yourself, instead of using the existing container classes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured