CrazyEngineers Forum

******************************************
Welcome To CrazyEngineers (CE) – an online community of engineers from all over the world! With the younger CEan at 84 and the youngest at 16, CE boasts of professional engineers, students, professors, entrepreneurs, CEOs, geeks & nerds. We exchange innovative ideas, share knowledge, help each other and expand our worldwide network of engineers! You need not have a formal degree in engineering to be a part of CrazyEngineers! Need we say more?
Join CE! | Be a CE Ambassador! | Forgot password? | Sponsor CE | Contact Us
Navigation
Go Back   CrazyEngineers Forum > CE : Technical Discussions > Computer Science & IT Engineering
Reply

  #1 (permalink)
Old 2nd March 2008, 12:11 PM
CE - Apprentice
 
pradeep_agrawal's Avatar
 
I'm a Crazy Computer Engineer
Join Date: 4th May 2006
Posts: 48
Default Data Structure/Algorithm Challenger II

We have a doubly-linked-list with number of nodes unknown.

Sometimes, while working with the list, it somehow gets corrupted. The corrupt node is pointing to some previous node instead of next node. So a circle is created in the list.

Test for the existence of circle (i.e., check if the list is corrupt).

Note: The list is doubly and not singly like Data Structure/Algorithm Challenger I, so a solution other then one specified in Data Structure/Algorithm Challenger I is expected.
pradeep_agrawal is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored links
  #2 (permalink)
Old 16th March 2008, 11:52 PM
CE - Apprentice
 
I'm a Crazy computer science Engineer
Join Date: 25th September 2006
Location: chennai
Posts: 36
Default Re: Data Structure/Algorithm Challenger II

some hint please. isnt the one by single linked list considered to be the most optimal?i jus hav a vague idea.may be we can have two pointers that go in opposite directions and if they dont meet means there is a cycle.i dont know where my second pointer ie which is to travel back wards has to point initially because i am not given the number of nodes.
sahana is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)
Old 17th March 2008, 12:32 PM
CE - Apprentice
 
pradeep_agrawal's Avatar
 
I'm a Crazy Computer Engineer
Join Date: 4th May 2006
Posts: 48
Default Re: Data Structure/Algorithm Challenger II

Quote:
Originally Posted by sahana View Post
isnt the one by single linked list considered to be the most optimal?
The solution given for singly linked list may be optimal for it, but a more optimized solution may exist for doubly linked list.

Quote:
Originally Posted by sahana View Post
may be we can have two pointers that go in opposite directions and if they dont meet means there is a cycle.i dont know where my second pointer ie which is to travel back wards has to point initially because i am not given the number of nodes.
We can't have second pointer pointing to the last node of list because we have only head and if the list is corrupt we never reach the last node. Due to same reason we canit count the number of nodes in the list.

Quote:
Originally Posted by sahana View Post
some hint please.
The basic property of doubly linked list is that every node has two pointers, one pointing to next node and other pointing to previous node in the list. Having an extra pointer in each node surely increase the space complexity, but that will help in giving more optimized solution to the problem in terms of space complexity.

-Pradeep

Last edited by pradeep_agrawal : 9th April 2008 at 06:05 PM.
pradeep_agrawal is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)
Old 10th November 2008, 12:13 PM
CE - Apprentice
 
pradeep_agrawal's Avatar
 
I'm a Crazy Computer Engineer
Join Date: 4th May 2006
Posts: 48
Default Re: Data Structure/Algorithm Challenger II

No solution from such a long time

Here is a sample code for the given problem statement. Solution is based on fact that in a doubly linked list, previous of next of a node is the same node. The solution has complexity of O(n).

Code:
       node *tmp1 = head;
      int result = 0;        //Specify no circular path exist
  
      while(tmp1 != NULL) {
          if(tmp1->next != null) {
              if(tmp1->next->prev != tmp1) {
                  result = 1;        //specify existence of circular path
                  break;
              }
              tmp1 = tmp1->next;
          } else {
              break;
          }
      }
  
      return result;
  }
-Pradeep
pradeep_agrawal is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)
Old 17th November 2008, 12:34 PM
CE - Newbie
 
I'm a Crazy Computer Science and egineering Engineer
Join Date: 17th November 2008
Posts: 2
Default Re: Data Structure/Algorithm Challenger II

hey the above code checks only between the two nodes but instead if we can modify the data node then include a flag.........
1>> initialize all flags to false......
2>> Goto each node serially and in each node check if its flag is already true is so then it means that the node has been already visited once and thus there exits a loopbut if the flag is false set it to true and iterate further........
<thus any size loop can be detected>
sumitjami is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)
Old 17th November 2008, 12:59 PM
CE - Apprentice
 
pradeep_agrawal's Avatar
 
I'm a Crazy Computer Engineer
Join Date: 4th May 2006
Posts: 48
Default Re: Data Structure/Algorithm Challenger II

Quote:
Originally Posted by sumitjami View Post
hey the above code checks only between the two nodes but instead if we can modify the data node then include a flag
Yes this can be a solution but:
1. It will need modification of your base data structure, which you may not always want to do.
2. Keeping an extra flag in each node will consume more memory.

-Pradeep
pradeep_agrawal is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored links
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +5.5. The time now is 10:21 PM.
Powered by vBulletin® Version 3.6.7
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0
Member comments are owned by the poster. Copyright © 2005-2008 CrazyEngineers.com. All rights reserved.Ad Management by RedTyger