A question in searching a link in a circular list

In summary, the conversation discusses building a method to find a specific value in a linked list and return the previous link. There is confusion about using the parameter "this" in a static method and how to properly use the "for" loop to check for the value. Suggestions are made to fix the code, such as starting the loop with p=this.next to prevent the body of the loop from never being performed with "this" itself.
  • #1
transgalactic
1,395
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
  • #2
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.
 
  • #3
its JAVA not C++
 
  • #4
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:
  • #5
i removed &&i changed the post

the link doesn't talk about linked listcan you answer my original post??
 
  • #6
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.
 
  • #7
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.
 
  • #8
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?
 
  • #9
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
 

1. What is a circular list?

A circular list is a data structure in which the last element is connected to the first element, creating a loop. This allows for efficient traversal and access of the elements in the list.

2. How do you search for a link in a circular list?

To search for a link in a circular list, you can start at any element and traverse through the list until you find the desired link. Alternatively, you can use a binary search approach by first finding the middle element and then determining if the desired link is in the first or second half of the list.

3. What is the time complexity for searching a link in a circular list?

The time complexity for searching a link in a circular list is O(n) in the worst case, where n is the number of elements in the list. However, if the list is sorted, a binary search approach can be used which has a time complexity of O(log n).

4. Can a circular list contain duplicate links?

Yes, a circular list can contain duplicate links just like any other data structure. However, this may affect the efficiency of searching for a specific link since there may be multiple instances of the same link in the list.

5. How do you insert a new link into a circular list?

To insert a new link into a circular list, you first need to find the appropriate location in the list. This can be done by traversing through the list or using a binary search. Once the location is found, the new link can be inserted by adjusting the pointers of the surrounding elements to point to the new link.

Similar threads

  • Programming and Computer Science
Replies
9
Views
2K
Replies
9
Views
943
  • Programming and Computer Science
Replies
9
Views
3K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
6
Views
12K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
7
Views
2K
  • Programming and Computer Science
Replies
4
Views
790
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Programming and Computer Science
Replies
3
Views
3K
Back
Top