A question in searching a link in a circular list

Click For Summary

Discussion Overview

The discussion revolves around a coding problem related to implementing a method for searching a value in a circular linked list and returning the previous link. Participants explore issues with the provided code and clarify the implications of using static methods in Java.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant presents a method intended to find a value in a circular linked list and return the previous link, but questions arise about the correctness of the code.
  • Another participant points out that the code is invalid in C++ and raises issues regarding the use of 'this' in a static method, which is also applicable to Java.
  • There is a clarification that the code cannot refer to 'this' in a static context, and the use of the '&&' operator in the for loop is incorrect due to missing operands.
  • A participant suggests that if the first link contains the searched value, the previous link pointer will not be assigned, indicating a potential flaw in the logic.
  • Further discussion reveals confusion about starting the loop with 'p=this' and the implications of that choice on loop execution.
  • Another participant explains that starting the loop with 'p=this' causes the loop to terminate immediately, prompting a suggestion to start with 'p=this.next' instead.
  • One participant expresses gratitude for the clarification, indicating some understanding of the issues raised.

Areas of Agreement / Disagreement

Participants express differing views on the correctness of the initial code and the implications of using 'this' in a static context. There is no consensus on the best approach to implement the method, and multiple competing views remain regarding the handling of the circular linked list.

Contextual Notes

Limitations include the potential misunderstanding of static methods in Java, the handling of the first link in the circular list, and the implications of the loop structure on code execution. These aspects remain unresolved.

transgalactic
Messages
1,386
Reaction score
0
i was told to build a method for which if
we find the asked value in one of the links we
return the previous link
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)
Link p;//current object
Link t;//previous object
for (p=Link;(p!=this);p=p.next, t=p)
{
if (p.data.equals(x)){
 return t;
 
}
return null;
}
 
Last edited:
Technology news on Phys.org
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++
 
The same comments apples to Java. From http://www.owlnet.rice.edu/~tchjava/2002/notes/current/node53.html ,
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

the link doesn't talk about linked listcan you answer my original post??
 
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.
 
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 don't 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 don't know why?
 
It is a good idea to trace through code by hand. Doing that with this chunk of code,
Code:
for (p=this; p != this; p= p.next) {
  < loop body >
}
The variable p is initially set to this. The termination condition is then checked. p is equal to this, so the body of the loop is never entered. Starting with p.next initially prevents this problem, but creates a new one. The body of the loop is never performed with this itself. That is easily rectified in this case as the body of this particular loop is a one-liner.
 
  • #10
ahhhhhhhhhh ok thanks
 

Similar threads

  • · Replies 6 ·
Replies
6
Views
13K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
Replies
9
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 3 ·
Replies
3
Views
4K