Linked list help simple little problem i need help with
okay so heres my code
Code java:
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 !
Re: Linked list help simple little problem i need help with
Quote:
how do i get the code to remove it backwords starting with L then J
Can you explain what L and J are? I don't see them in the code. All the letters I see are lowercase.
Please edit your post and wrap your code with
[code=java]
<YOUR CODE HERE>
[/code]
to get highlighting and preserve formatting.
Re: Linked list help simple little problem i need help with
sorry about that norm im new here and a begginer programmer! and my bad i meant to say lower case l , k, and j !
thanks for the heads up on the tags
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.
Re: Linked list help simple little problem i need help with
Quote:
There is no real problem it doesn't do anything incorrectly
I thought there was a problem with the code.
Quote:
where do i need to modifiy my code to remove it starting from l, k, j
Is that what the removeLast() method should do?
Or are you talking about a new method that you are trying to write?
It's confusing the way you have worded it.
Re: Linked list help simple little problem i need help with
Im sorry its I 'm terrible at wording what i want to say . But yes the removeLast() should remove the last letter but instead i got it removing the top letter starting with b
Re: Linked list help simple little problem i need help with
What would the method need to do to remove the last node in the list?