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 2 of 2

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 J then j and so forth please help thank you !

  2. The Following User Says Thank You to perezbrandon92 For This Useful Post:

    thomas10 (June 7th, 2013)


  3. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

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

    double post. closing

  4. The Following User Says Thank You to pbrockway2 For This Useful Post:

    thomas10 (June 7th, 2013)

Similar Threads

  1. 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
  2. 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
  3. Simple linked list problem
    By babe20042004 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 5th, 2011, 10:40 AM
  4. Linked List problem, please help.
    By Axeander in forum What's Wrong With My Code?
    Replies: 10
    Last Post: December 21st, 2010, 05:01 PM
  5. Simple linked list
    By Koren3 in forum Collections and Generics
    Replies: 10
    Last Post: November 2nd, 2009, 03:33 AM