P
p7371464
I have a LinkedList which stores some integer key.
For each key, I can use a computing function f() to get the
corresponding value.
In order to founding the key which has smallest value, I must work
through all key in the list.
Once found the key, I will remove the key from the list.
So I wrote the code as follow:
....
LinkedList<Integer> list = new LinkedList<Integer>();
....
....add some integer key to the list
.....
ListIterator<Integer> it = list.listIterator();
ListIterator<Integer> it_save;
Integer min=Integer.MAX_VALUE;
//walk through all key in the list to found the key have minimum value
while(it.hasNext()){
int key = it.next();
int value = f(i);
if(value < min){
it.previous();
It_save = it;
it.next();
}
}
//remove the key which has smallest value
it_save.remove();
.....
But the above code can not work correctly, have any method to store the
ListIterator variable while
scan the list ?
Thanks in advance.
For each key, I can use a computing function f() to get the
corresponding value.
In order to founding the key which has smallest value, I must work
through all key in the list.
Once found the key, I will remove the key from the list.
So I wrote the code as follow:
....
LinkedList<Integer> list = new LinkedList<Integer>();
....
....add some integer key to the list
.....
ListIterator<Integer> it = list.listIterator();
ListIterator<Integer> it_save;
Integer min=Integer.MAX_VALUE;
//walk through all key in the list to found the key have minimum value
while(it.hasNext()){
int key = it.next();
int value = f(i);
if(value < min){
it.previous();
It_save = it;
it.next();
}
}
//remove the key which has smallest value
it_save.remove();
.....
But the above code can not work correctly, have any method to store the
ListIterator variable while
scan the list ?
Thanks in advance.