okay so heres my code
import java.util.Iterator; import java.util.NoSuchElementException; public class LinkedList<Item> implements Iterable<Item> { private Node head; private Node last; private int 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. */ private class Node { private Item item = null; private Node next = null; } public LinkedList() { head = null; last = null; size = 0; } //Add to the end of the list public boolean 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 list if (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 list return true; } public int size() { return size; } public boolean remove(Item item) { boolean found = false; if (head != null) { //if head = null, then the list is empty - return false if (head.item.equals(item)) {// 1st Node contains the item head = head.next; //remove the 1st Node size--; //revise the size of the list if (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 list if (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() { return new LinkedListIterator(); } private class LinkedListIterator implements Iterator<Item> { private Node current = head; public boolean hasNext() { return current != null; } public Item next() { if (!hasNext()) { throw new NoSuchElementException(); } Item item = current.item; //get current key data current = current.next; return item; } public void remove() { } } //hw part NEED HELP BELOW HERE! public boolean 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; } public static void 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 !