Software Design
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreSoftware Design

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 16th, 2003, 02:16 AM
monty monty is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: Chennai
Posts: 4 monty User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Linked list.

Hello,
In a linked list, I have the last node pointing somewhere to the
middle of the linked list. How should I find out where the circular link appears?


Monty

Reply With Quote
  #2  
Old June 16th, 2003, 01:22 PM
epl epl is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2001
Location: Dublin
Posts: 413 epl User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 18 m 18 sec
Reputation Power: 8
i'm guessing you should walk through it and load each item into a new list - if it doesn't already exist in the new list. the first item you get pointing to an item already in the new list is the then last item.

Reply With Quote
  #3  
Old June 25th, 2003, 07:06 AM
me_no_xpert me_no_xpert is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: India
Posts: 19 me_no_xpert User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
to chk whether is a link is circular, use 2 pointers, and traverse ,, say p and q
for p , increment p = p->next
and q q=q->next->next
(take care of error checks )
eventually they'll have to meet.. if its circular
Alternatively u can have an additional flag for every list item: visited or not ... once the flag is set true.. i.e visited and u try visiting it again, u know its circular...
enna purinchida?

Reply With Quote
  #4  
Old June 25th, 2003, 07:26 AM
monty monty is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: Chennai
Posts: 4 monty User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hello no expert thanks...
but my friend had already found the logic in the book "art of programming".

Thanks for taking a effort to mail me.

you seem to be tamilian right?

Reply With Quote
  #5  
Old June 25th, 2003, 07:31 AM
me_no_xpert me_no_xpert is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: India
Posts: 19 me_no_xpert User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
what is the solution that ur friend has got ?
Cud u plz post that also for the benefits of "non experts" in this forum..
oh yup, naan oru tamizacchi

Reply With Quote
  #6  
Old June 25th, 2003, 07:34 AM
monty monty is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: Chennai
Posts: 4 monty User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hello no expert,

it's the same logic use 2 iterators and at one point they would meet.
I think your flag idea is innovative.

thanks tamilachi

Reply With Quote
  #7  
Old June 25th, 2003, 07:39 AM
me_no_xpert me_no_xpert is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: India
Posts: 19 me_no_xpert User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
FYI monty, the flag idea is one of the "bad ideas" to solve the pblm..
to find a solution , one of the last things that you must resort to
is change the data structure itself... u may be asking why?
say suppose u have an additional flag, it cud be a char/int/bit field
dat wud mean increasing the space complexity. The last thing u must do is to increase the space complexity..
the flag idea is O(n) (i hope u are comfy with such notations) but requires more space,
the iterating way using 2 pointers is the best way to do it. acc 2 me..

Reply With Quote
  #8  
Old June 25th, 2003, 07:47 AM
monty monty is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: Chennai
Posts: 4 monty User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hello no expert,
I agree with you about the flag idea.
But instead of thinking logically,and in lateral context i think it's a good idea.

you seem to be intelligent tamilachi!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreSoftware Design > Linked list.


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT