The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

Linked list help simple little problem i need help with

okay so heres my code

importjava.util.Iterator;importjava.util.NoSuchElementException;publicclass LinkedList<Item>implements Iterable<Item>{private Node head;private Node last;privateint size;//number of Nodes/*
* head refers to the first node; head.previous is always null. last refers
* to the last node; last.next is always null.
*/privateclass Node {private Item item =null;private Node next =null;}publicLinkedList(){
head =null;
last =null;
size =0;}//Add to the end of the listpublicboolean add(Item item){
Node n =new Node();//create a new Node object
n.item= item;// store the data in this object
n.next=null;// this node will be placed at the end of the linked listif(last ==null){//Adding to an empty LinkedList
head = last = n;}else{//Adding to the end of a LinkedList with at least one element
last.next= n;//the current last node points to our new node
last = n;//last now points to the node we added}
size++;//increase the size of the listreturntrue;}publicint size(){return size;}publicboolean remove(Item item){boolean found =false;if(head !=null){//if head = null, then the list is empty - return falseif(head.item.equals(item)){// 1st Node contains the item
head = head.next;//remove the 1st Node
size--;//revise the size of the listif(size ==0)//Did we remove the only item in the list?{
last =null;// then set last to null}
found =true;//we found the item in the LinkedList}else{
Node current = head.next;
Node previous = head;while(current !=null&&!found){// current == null means we're at the end of the listif(current.item.equals(item)){// we found a match
previous.next= current.next;//remove the current node
size--;
found =true;if(current == last)// we're removing the last item{
last = previous;//set last to previous}}else{//we didn't find the item, so go to the next Node in the LinkedList
previous = current;
current = current.next;}}//end while}//end else}return found;}public Iterator<Item> iterator(){returnnew LinkedListIterator();}privateclass LinkedListIterator implements Iterator<Item>{private Node current = head;publicboolean hasNext(){return current !=null;}public Item next(){if(!hasNext()){thrownewNoSuchElementException();}
Item item = current.item;//get current key data
current = current.next;return item;}publicvoid remove(){}}//hw part
NEED HELP BELOW HERE!publicboolean removeLast(){boolean found =false;if(size!=0){
Node current = head.next;
Node previous = head;while(current !=null&&!found){// current == null means we're at the end of the list
previous.next= current.next;
size--;
found =true;if(current == last)// we're removing the last item{
last = previous;//set last to previous}}
previous = current;
current = current.next;}return found;}publicstaticvoid main(String[] args){
LinkedList<String> ll =new LinkedList<String>();
ll.add("a");
ll.add("b");
ll.add("c");
ll.add("d");
ll.add("e");
ll.add("f");
ll.add("g");
ll.add("h");
ll.add("i");
ll.add("j");
ll.add("k");
ll.add("l");System.out.println();System.out.println("size = "+ ll.size());for(String e : ll){System.out.println(e);}System.out.println();if(ll.removeLast()){for(String e : ll){System.out.println(e);}}System.out.println();if(ll.removeLast()){for(String e : ll){System.out.println(e);}}System.out.println();if(ll.removeLast()){for(String e : ll){System.out.println(e);}}}}

and heres the out put i get

run:

size = 12
a
b
c
d
e
f
g
h
i
j
k
l

a
c
d
e
f
g
h
i
j
k
l

a
d
e
f
g
h
i
j
k
l

a
e
f
g
h
i
j
k
l

My problem is how do i get the code to remove it backwords starting with l then k then j and so forth please help thank you !

Re: Linked list help simple little problem i need help with

Can you explain what the problem is with the existing code? What does it do incorrectly? Show its output and add some comments to say what the output should look like.
For testing you should only need to call the method once or twice to see if it is working correctly.

The code needs formatting. All the statements should NOT start in the first column.
There should be indentations for nested statements.

Re: Linked list help simple little problem i need help with

There is no real problem it doesn't do anything incorrectly , i got my code backwards ! The output show one letter being removed b c then d , the question is where do i need to modifiy my code to remove it starting from l, k, j

thanks for the tips ill include all the formatting in my code right away.