|
******************************************
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 |
![]() |
| Sponsored links | |
|
|
|
|
CE - Apprentice
![]() ![]()
I'm a Crazy Computer Engineer
Join Date: 4th May 2006
Posts: 48
|
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;
}
|
|
|
|
|
CE - Newbie
![]()
I'm a Crazy Computer Science and egineering Engineer
Join Date: 17th November 2008
Posts: 2
|
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> |
|
|
|
|
CE - Apprentice
![]() ![]()
I'm a Crazy Computer Engineer
Join Date: 4th May 2006
Posts: 48
|
Quote:
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 |
|
|
|
|
| Sponsored links | |
|
|
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
| Contact Us - Home - Impressum | Impressum - |