Code.GeekInterview.com
 
Code Samples C++
 

Finding loop in a link list


Code ResourceAuthor: vipin gupta  

Difficulty Level:

Published: 6th Nov 2006   Read: 6335 times  

Filed in: C++
Add Comment


 

 

Sponsored Links


 

 

The program facilitates user to create & traverse a link list. It also allows user to create a loop in the link list, and then, find out weither there is a loop in the list or not. The logic behind finding loop is: Take two pointers, one will traverse the list twice as fast as other. if at any point of traversal, the two pointers are equal, then we say, we have found the loop.

just compile & run the a.out file. The program will go in a loop asking which operation user wants to perform. As per users choice, the program operates on link list & display lists contents after operation is performed.

 


Sample Code
  1.  
  2. #include<iostream.h>
  3. #include<stdio.h>
  4.  
  5. struct list
  6. {
  7.         int info;
  8.         struct list *next;
  9. };
  10.  
  11. class listOp
  12. {
  13.         struct list *fPtr,*tPtr;
  14.         struct list *A,*B,*C,*T;
  15.         public:
  16.         listOp()
  17.         {
  18.                 fPtr = NULL;
  19.                 tPtr = NULL;
  20.         }
  21.         bool allocateNode(int); // allocate a new node & put int as its info field
  22.         struct list *traverseList(int); // travesr list & return its elements pointer
  23.         void  createLoop(int element1,int element2); // create a loop between element1 & element2
  24.         bool findLoop(); // finds out whether there is loop or not
  25. };
  26.  
  27. bool listOp::allocateNode(int info)
  28. {
  29.         tPtr = new struct list;
  30.         if(tPtr == NULL)return false;
  31.         tPtr->info = info;
  32.         tPtr->next = NULL;
  33.         if(fPtr == NULL)
  34.                 fPtr=tPtr;
  35.         else
  36.         {
  37.                 struct list *t1Ptr = traverseList(-1);
  38.                 t1Ptr->next = tPtr;
  39.         }
  40.         return true;
  41. }
  42. struct list* listOp::traverseList(int element)
  43. {
  44.         struct list *t1Ptr = fPtr;
  45.         cout << "nThe Elements found in traversal are: n";
  46.         while(t1Ptr->next !=NULL && t1Ptr->info != element)
  47.         {
  48.                 cout << t1Ptr->info << "n";
  49.                 t1Ptr = t1Ptr->next;
  50.         }
  51.         cout << t1Ptr->info << "n";
  52.         return t1Ptr;
  53. }
  54. void listOp::createLoop(int element1,int element2)
  55. {
  56.         struct list *t1Ptr = traverseList(element1);
  57.         struct list *t2Ptr = traverseList(element2);
  58.         if(t1Ptr > t2Ptr)
  59.                 t1Ptr->next = t2Ptr;
  60.         else
  61.                 t2Ptr->next = t1Ptr;
  62. }
  63. bool listOp::findLoop()
  64. {
  65.         struct list *t1Ptr = fPtr;
  66.         struct list *t2Ptr = fPtr;
  67.         while(t1Ptr->next !=NULL)
  68.         {
  69.                 t1Ptr=t1Ptr->next;
  70.                 if(t2Ptr->next!=NULL)t2Ptr=t2Ptr->next;
  71.                 if(t2Ptr->next!=NULL)t2Ptr=t2Ptr->next;
  72.                 if(t1Ptr == t2Ptr && t2Ptr->next != NULL)
  73.                         return true;
  74.         }
  75.         return false;
  76. }
  77.  
  78.  
  79. int main()
  80. {
  81.         listOp LIST;
  82.         int choice;
  83.         int infor,infor1,infor2;
  84.         while(1)
  85.         {
  86.                 cout << "nWhat operation you want to Perform: n
  87.                                1. Add Node to the list. n
  88.                                2. Traverse list. n
  89.                                3. Create loop in the list. n
  90.                                4. Find Loop. n
  91.                                5. Exit. n";
  92.                 cin >> choice;
  93.                 switch(choice)
  94.                 {
  95.                         case 1: cout << "nEnter the information field of the node: ";
  96.                                 cin >> infor;
  97.                                 if(LIST.allocateNode(infor))
  98.                                         cout << "nNode allocated successfully";
  99.                                 else
  100.                                         cout << "nOperation Failed";
  101.                                 break;
  102.                         case 2: cout << "nEnter the information field to which to travese: ";
  103.                                 cin >> infor;
  104.                                 LIST.traverseList(infor);
  105.                                 break;
  106.                         case 3: cout << "nEnter element1: ";
  107.                                 cin >> infor1;
  108.                                 cout << "nEnter element2: ";
  109.                                 cin >> infor2;
  110.                                 LIST.createLoop(infor1,infor2);
  111.                                 break;
  112.                         case 4: if(LIST.findLoop())
  113.                                         cout << "nLoop Found in list.";
  114.                                 else
  115.                                         cout << "nLoop not found.";
  116.                                 break;
  117.                         default: exit(0);
  118.                     }
  119.         }
  120.         return 0;
  121. }
  122.  
Copyright GeekInterview.com


Next: Using Stream Iterators with Containers


 

Latest Code Samples

 

Popular Code Samples

 

Related Code Samples

 

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    



Popular Coders

# Coder NameHits
1. Priya38159
2. chowsys15750
3. bipin_vaylu14163
4. raghu.mohindru9083
5. Subashini7757
6. rachittyagi7544
7. vimal.fire7413
8. venki_madesh6541
9. prabha6526
10. vipin gupta6336

Active Coders

Refined Tags