Welcome to the Java Programming Forums


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.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

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

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Linked list help simple little problem i need help with

    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 !


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

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

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default 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

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default 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.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

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

    There is no real problem it doesn't do anything incorrectly
    I thought there was a problem with the code.

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Feb 2013
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default 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

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Linked list help simple little problem i need help with
    By perezbrandon92 in forum Algorithms & Recursion
    Replies: 1
    Last Post: February 5th, 2013, 06:39 PM
  2. Help with linked list problem
    By babe20042004 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 30th, 2013, 11:45 AM
  3. Please Help: Simple Double Linked List (remove, addBefore, )
    By yeohv in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 30th, 2012, 12:58 PM
  4. Simple linked list problem
    By babe20042004 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 5th, 2011, 10:40 AM
  5. Simple linked list
    By Koren3 in forum Collections and Generics
    Replies: 10
    Last Post: November 2nd, 2009, 03:33 AM