1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java Removing Element from List

  1. Aug 6, 2011 #1
    i am trying to remove an element from the back of the list (i.e. the node right before the tail). i am using a dummy head and tail node. the code compiles correctly, but when i try to test it, there is no execution. below is the segment of code that i am referring to:


    public Object removeBack() {
    if (_size == 0) throw new NoSuchElementException();
    Node node = new Node();
    Node cursor = _tail._prev;
    cursor._next._prev = cursor._prev;
    cursor._prev._next = cursor._next; // this is the line of code giving me error
    _size--;
    return node._data;
    }

    i am not sure what is wrong with my code. it works properly when trying to removeFront, and the only change from that segment of the code is the cursor.
     
  2. jcsd
  3. Aug 7, 2011 #2

    I like Serena

    User Avatar
    Homework Helper

    Hi magnifik! :smile:

    Your code appears to correctly remove the last element in the list, so what do you mean that there is no execution?

    Btw, you do create an unnecessary empty "node", of which you try to return the _data field, which will probably be empty.
    Depending on how defined your Node class, this could even result in an exception.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Java Removing Element from List
  1. Java - list program (Replies: 3)

Loading...