473,387 Members | 1,925 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Basic Linked List Problem

I don't understand the following code which is copied from a text. It
is the text's solution to the problem of determining if a linked list
is cyclic.

int DetermineTermination(node *head)
{
node *fast, *slow;
fast = slow = head;
while (1) {
if (!fast || !fast -next)
return 0;
else if (fast == slow || fast -next == slow)
return 1;
else {
slow = slow -next;
fast = fast -next -next;
}
}
}

As I see it, while (1) just means while (true) and the while construct
isn't really needed.

However, the fast pointer is initially assigned to point to the same
value as the slow pointer.

So (as I see it), fast == slow will be true at the beginning, and the
value 1 will be returned.

So (as I see it), this function will simply return 1 almost always
unless the head pointer is null.

What am I missing?

In particular, I know that the text wants to implement the final else
statements -- slow = slow->next; etc. in order to update the slow and
fast pointers.

However, the final else only comes into play if fast == slow is false.
Why should fast == slow be false when fast is assigned to slow at the
beginning?
Thank you very much for your help.

Paul Epstein

Aug 3 '06 #1
3 2808

pa**********@att.net wrote:
I don't understand the following code which is copied from a text. It
is the text's solution to the problem of determining if a linked list
is cyclic.

int DetermineTermination(node *head)
{
node *fast, *slow;
fast = slow = head;
while (1) {
if (!fast || !fast -next)
return 0;
else if (fast == slow || fast -next == slow)
return 1;
else {
slow = slow -next;
fast = fast -next -next;
}
}
}

As I see it, while (1) just means while (true) and the while construct
isn't really needed.

However, the fast pointer is initially assigned to point to the same
value as the slow pointer.

So (as I see it), fast == slow will be true at the beginning, and the
value 1 will be returned.

So (as I see it), this function will simply return 1 almost always
unless the head pointer is null.

What am I missing?

In particular, I know that the text wants to implement the final else
statements -- slow = slow->next; etc. in order to update the slow and
fast pointers.

However, the final else only comes into play if fast == slow is false.
Why should fast == slow be false when fast is assigned to slow at the
beginning?
Thank you very much for your help.

Paul Epstein

You are correct! There was in fact an error in the book (Programming
Interviews Exposed, it appears). You can see the corrected version of
the code on the Errata page at:
http://www.wiley.com/legacy/compbook...ew/errata.html

Siam

Aug 3 '06 #2

<pa**********@att.netwrote in message
news:11*********************@h48g2000cwc.googlegro ups.com...
>I don't understand the following code which is copied from a text. It
is the text's solution to the problem of determining if a linked list
is cyclic.

int DetermineTermination(node *head)
{
node *fast, *slow;
fast = slow = head;
while (1) {
if (!fast || !fast -next)
return 0;
else if (fast == slow || fast -next == slow)
return 1;
else {
slow = slow -next;
fast = fast -next -next;
}
}
}

As I see it, while (1) just means while (true) and the while construct
isn't really needed.
That construct is often used for a loop which doesn't have an easily
predetermined condition for ending (or has multiple such conditions). It
forces the loop to continue until a break (or in this case, return)
statement. Without the while construct, it would only execute once.
However, the fast pointer is initially assigned to point to the same
value as the slow pointer.

So (as I see it), fast == slow will be true at the beginning, and the
value 1 will be returned.
Yep.
So (as I see it), this function will simply return 1 almost always
unless the head pointer is null.
Yep.
What am I missing?
Nothing. (Except perhaps a better example? :-))

(But is that really the entire code? From the indentation, it looks like
there might have been other code there, which was stripped. Just
checking...)
In particular, I know that the text wants to implement the final else
statements -- slow = slow->next; etc. in order to update the slow and
fast pointers.

However, the final else only comes into play if fast == slow is false.
Why should fast == slow be false when fast is assigned to slow at the
beginning?
You're correct. The code,as written, is in error. (I'm not going to try to
figure out the correction for the code, though. Maybe that would be a good
problem for you? :-))

-Howard
Aug 3 '06 #3
pa**********@att.net wrote:
I don't understand the following code which is copied from a text. It
is the text's solution to the problem of determining if a linked list
is cyclic.

int DetermineTermination(node *head)
{
node *fast, *slow;
fast = slow = head;
while (1) {
if (!fast || !fast -next)
return 0;
else if (fast == slow || fast -next == slow)
return 1;
else {
slow = slow -next;
fast = fast -next -next;
}
}
}

As I see it, while (1) just means while (true) and the while construct
isn't really needed.

However, the fast pointer is initially assigned to point to the same
value as the slow pointer.

So (as I see it), fast == slow will be true at the beginning, and the
value 1 will be returned.
Right... Did the text claim the function worked? Or was it saying that
the function doesn't work, and you're supposed to fix it?
So (as I see it), this function will simply return 1 almost always
unless the head pointer is null.

What am I missing?
Nothing, AFAICT.
In particular, I know that the text wants to implement the final else
statements -- slow = slow->next; etc. in order to update the slow and
fast pointers.

However, the final else only comes into play if fast == slow is false.
Right.
Why should fast == slow be false when fast is assigned to slow at the
beginning?
I am not sure what you're asking here.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 3 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: RAJASEKHAR KONDABALA | last post by:
Hi, Does anybody know what the fastest way is to "search for a value in a singly-linked list from its tail" as oposed to its head? I am talking about a non-circular singly-linked list, i.e.,...
5
by: John N. | last post by:
Hi All, Here I have a linked list each containing a char and is double linked. Then I have a pointer to an item in that list which is the current insertion point. In this funtion, the user...
7
by: Kieran Simkin | last post by:
Hi all, I'm having some trouble with a linked list function and was wondering if anyone could shed any light on it. Basically I have a singly-linked list which stores pid numbers of a process's...
10
by: Ben | last post by:
Hi, I am a newbie with C and am trying to get a simple linked list working for my program. The structure of each linked list stores the char *data and *next referencing to the next link. The...
3
by: Little | last post by:
Could someone tell me what I am doing wrong here about declaring mutiple double linked lists. This is what the information is for the project and the code wil be below that. Thank your soo much for...
11
by: bofh1234 | last post by:
Hello, I am having a problem with linked lists. My program is based on a client server model. The client sends some packets of data to the server. The server reads those packets and is...
22
by: joshc | last post by:
In an interview for an embedded software position recently I was asked to write code, in C, for printing the contents of a linked list backwards. After a few minutes I came up with the recursive...
6
Atli
by: Atli | last post by:
This is an easy to digest 12 step guide on basics of using MySQL. It's a great refresher for those who need it and it work's great for first time MySQL users. Anyone should be able to get...
7
by: QiongZ | last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.