10 Replies - 3581 Views - Last Post: 07 May 2011 - 06:52 PM Rate Topic: -----

#1 helloworld123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 07-May 11

java linked list: removing largest element using iterator

Posted 07 May 2011 - 02:35 PM

I have got stuck in a problem. I have a linkedlist. how can I remove the largest element from the list? I know i can use get() function to retrieve the elements. But i want to make it efficient. I want to use iterator? Do you have any idea how i can do it? Any help is appreciated.
Is This A Good Question/Topic? 0
  • +

Replies To: java linked list: removing largest element using iterator

#2 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: java linked list: removing largest element using iterator

Posted 07 May 2011 - 03:03 PM

Use the Collections.sort method
Collections.sort(yourList);


That will sort it into a natural order. Now, your largest element will be in the last position, so you can use
yourList.removeLast() 


No need for an iterator for what you need to do.
Was This Post Helpful? 0
  • +
  • -

#3 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,691
  • Joined: 13-March 10

Re: java linked list: removing largest element using iterator

Posted 07 May 2011 - 03:06 PM

If you use Collections.sort() make sure that the objects being sorted implement Comparable interface.
Was This Post Helpful? 0
  • +
  • -

#4 helloworld123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 07-May 11

Re: java linked list: removing largest element using iterator

Posted 07 May 2011 - 05:36 PM

Thanks for your reply. But I dont want to sort my linkedlsit. Is there any other way ? I want to do a sequential search using iterator. But how can i do that ?




View Postnick2price, on 07 May 2011 - 03:03 PM, said:

Use the Collections.sort method
Collections.sort(yourList);


That will sort it into a natural order. Now, your largest element will be in the last position, so you can use
yourList.removeLast() 


No need for an iterator for what you need to do.

Was This Post Helpful? 0
  • +
  • -

#5 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,691
  • Joined: 13-March 10

Re: java linked list: removing largest element using iterator

Posted 07 May 2011 - 05:40 PM

create a pointer to the head of the list. Iterate through the list comparing values at each node with the value the pointer points to. If bigger, point to new node. Keep going until you reach the tail.
Was This Post Helpful? 0
  • +
  • -

#6 helloworld123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 07-May 11

Re: java linked list: removing largest element using iterator

Posted 07 May 2011 - 05:54 PM

Thanks again! Here is my code. How can I change the pointer (or iterator) so that it will point to a new node. I know how to do it C++ but dont know in java.

int maxNumber = -1;
LinkedList <Cube>cubeList = new LinkedList<Cube>();
Cube maxCube = null;

for(Iterator <Cube> it = cubeList.iterator(); it.hasNext(); )
{
     Cube cube = it.next();
     if ( maxNumber < cube.number)
     {
          maxNumber = cube.number;
          maxCube = cube;
     }
}





View Postdarek9576, on 07 May 2011 - 05:40 PM, said:

create a pointer to the head of the list. Iterate through the list comparing values at each node with the value the pointer points to. If bigger, point to new node. Keep going until you reach the tail.

Was This Post Helpful? 0
  • +
  • -

#7 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,691
  • Joined: 13-March 10

Re: java linked list: removing largest element using iterator

Posted 07 May 2011 - 06:08 PM

Your code seems correct but why do you have

maxNumber = cube.number;

I think thats not necessary since you want to point to the Cube object with largest value so your if-statement will do the job. But you are on the right line.
Was This Post Helpful? 1
  • +
  • -

#8 helloworld123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 07-May 11

Re: java linked list: removing largest element using iterator

Posted 07 May 2011 - 06:39 PM

But I want remove the maxCube element. How can I do that ? I can add the following line at end of for loop. But I dont think it is efficient. Because, It will search the maxCube element again in my whole linkedlist. Just imagine that I have 1 million elements in my list. Any efficient idea ?

cubeList.remove(maxCube);


View Postdarek9576, on 07 May 2011 - 06:08 PM, said:

Your code seems correct but why do you have

maxNumber = cube.number;

I think thats not necessary since you want to point to the Cube object with largest value so your if-statement will do the job. But you are on the right line.

Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,259
  • Joined: 27-December 08

Re: java linked list: removing largest element using iterator

Posted 07 May 2011 - 06:42 PM

If you are dealing with the java.util.LinkedList, then you can determine if the element occurs in the first or second half, and use either the iterator() or descendingIterator() method respectively. That will cut the number of steps in half. If you are writing your own LinkedList class, then you can deal at the Node-based level, which is more efficient.
Was This Post Helpful? 0
  • +
  • -

#10 helloworld123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 07-May 11

Re: java linked list: removing largest element using iterator

Posted 07 May 2011 - 06:50 PM

Thanks for your reply!
Yes, I'm dealing with java.util.LinkedList and also using iterator(). Now how can I delete the largest element?


View Postmacosxnerd101, on 07 May 2011 - 06:42 PM, said:

If you are dealing with the java.util.LinkedList, then you can determine if the element occurs in the first or second half, and use either the iterator() or descendingIterator() method respectively. That will cut the number of steps in half. If you are writing your own LinkedList class, then you can deal at the Node-based level, which is more efficient.

Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,259
  • Joined: 27-December 08

Re: java linked list: removing largest element using iterator

Posted 07 May 2011 - 06:52 PM

Iterators have remove() methods. Check the documentation. You will have to traverse at most half of your List again to remove the element, as I described above.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1