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.
10 Replies - 2552 Views - Last Post: 07 May 2011 - 06:52 PM
#1
java linked list: removing largest element using iterator
Posted 07 May 2011 - 02:35 PM
Replies To: java linked list: removing largest element using iterator
#2
Re: java linked list: removing largest element using iterator
Posted 07 May 2011 - 03:03 PM
Use the Collections.sort method
That will sort it into a natural order. Now, your largest element will be in the last position, so you can use
No need for an iterator for what you need to do.
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.
#3
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.
#4
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 ?
nick2price, on 07 May 2011 - 03:03 PM, said:
Use the Collections.sort method
That will sort it into a natural order. Now, your largest element will be in the last position, so you can use
No need for an iterator for what you need to do.
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.
#5
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.
#6
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;
}
}
darek9576, 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.
#7
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.
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.
#8
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);
darek9576, 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.
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.
#9
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.
#10
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?
Yes, I'm dealing with java.util.LinkedList and also using iterator(). Now how can I delete the largest element?
macosxnerd101, 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.
#11
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.
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote








|