d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Interating Through Linkedlist To Delete An Element
Add Reply New Topic New Poll
Member
Posts: 29,350
Joined: Mar 27 2008
Gold: 504.69
Oct 23 2015 07:30am
Code
public void listRemove(LinkedList newList)
{
String input;
Scanner scan = new Scanner(System.in);
ListIterator myIterator = newList.listIterator();

displayQueue(newList);
System.out.printf("Type what you would like to remove from the list:");
input = scan.nextLine();

if((myIterator.next()).equals(input))
{
myIterator.remove();
}

while(myIterator.hasNext())
{
System.out.print( " "+myIterator.next()+" " );
}

}


.remove is taking out the last element .next returned which always seems to be the first element in the list. I need to be able to input what element I want removed and it remove all of them from the list. Any help appreciated.

/edit

I meant List Iterator, not linked list.

This post was edited by ROM on Oct 23 2015 07:36am
Member
Posts: 29,350
Joined: Mar 27 2008
Gold: 504.69
Oct 23 2015 08:02am
Nevermind. I was making my work harder then it needed to be. Reread the description and it was too delete from a linked list which want that hard.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll