Welcome, %1$s. Please login or register.

October 24, 2017, 04:06:28 AM
: 1
: How do you find out if a linked-list has an end or not in a single traversal?  ( 3863 )
« : May 15, 2007, 08:22:21 PM From Hari»



How do you find out if a linked-list has an end or not in a single traversal?


How do you find out if a linked-list has an end or not in a single traversal?

 
Liked It? Share it!

              


« #1 : May 15, 2007, 08:27:00 PM From mohank»

« #2 : May 31, 2007, 05:09:36 PM From iammilind»

check for the NULL.
i.e.

struct Node
{
  // ... data
  Node *pNext;
};

void End_or_Not(Node *ptr)
{
   Node *pTrav = ptr;

   while(pTrav = pTrav->pNext)
      if(pTrav == ptr)
      {   printf("\nCircular"); return; }

   printf("\nSingly");
}
« #3 : November 10, 2010, 10:44:36 AM From prayas»

You can find out by using 2 pointers. One of them goes 2 nodes each time. The second one goes at 1 nodes each time. If there is a cycle, the one that goes 2 nodes each time will eventually meet the one that goes slower. If that is the case, then you will know the linked-list is a cycle.


Read more at http://study.tryandfun.com/
: 1
« previous next »

 

Best RatedList All>>



Latest
Random



SMF 2.0.10 | SMF © 2015, Simple Machines | Contact Webmaster | OnlineFunDb.com © 2009/10 | Legal Disclaimer