# A question in searching a link in a circular list

## Main Question or Discussion Point

i was told to build a method for which if
i have built a method that supposedly does it
the problem is that i was told that this code is wrong because
we need to check the "this" link seperatly from the FOR loop
why is that???
Code:
public static  Link find(Object x)
{
if (p.data.equals(x)){
return t;

}
return null;
}

Last edited:

Related Programming and Computer Science News on Phys.org
D H
Staff Emeritus
That isn't valid c++ code.
- 'this' is doesn't exist in a static member function.
- There is no left operand for the && operator in the for loop.

its JAVA not C++

D H
Staff Emeritus
The same comments apples to Java. From http://www.owlnet.rice.edu/~tchjava/2002/notes/current/node53.html [Broken],
owlnet.rice.edu said:
Similarly, the code in a static method cannot refer to this or to the fields of this because there is no class instance to serve as the receiver for such an access.
"&&" is a binary infix conditional operator in Java. Binary: "&&" needs two operands on which to operate. Infix: the two operands go to the left and right of the operator. Conditional: If the left operand is false so is the expression.

Last edited by a moderator:
i removed &&

i changed the post

can you answer my original post??

D H
Staff Emeritus
The link talks about using the parameter this in a static method. You cannot do that. Why did you use the word "static" doing at the start of the code? You cannot use this in a static method. You have other errors as well, such as "p=Link". "Link" is a class; I suspect you meant "p=this" instead. Of course, if you do start with "p=this", the code will never enter the loop as written.

jambaugh
Gold Member
One problem that I see is:

If your first link happens to be the one where you find the value then the t-pointer to the previous link will not yet be assigned.

ok i ment p=this
and suppose that its not static
so what know
i dont understand this comment
"if you do start with "p=this", the code will never enter the loop as written"
its starts with this then in the for loop i say that p=p.next
so it goes further
and checks "p" again which represents the next link
i was told to start the for loop from p=this.next
and to check "this" object seperatly
i dont know why???

D H
Staff Emeritus
for (p=this; p != this; p= p.next) {
}