Start New Discussion within our Software Development Community

This is the code that I have so far but does not work. When called, the metod returns a pointer whose first and last points to null. The "given" linked list a linked list that we have to make using the add method from the inherited Linked List class.

public class ExtLinkedList<E> extends LinkedList<E>
{
    public ExtLinkedList<E> oddItemsList () 
    {
        ExtLinkedList<E> oddList = new ExtLinkedList<E>();
        ListIterator<E> itr = oddList.listIterator();
        for(int i = 0; itr.hasNext(); i++)
        {
            //E element = itr.next();
            if(i % 2 == 1)
            {
                //System.out.print(element);
                itr.remove();
                itr.next();
            }
            i++;
        }
        return oddList;
    }
}


// Here is the node class with constructors

public class Entry <E>
{
  E element;
  Entry<E> prev;
  Entry<E> next;

  public Entry() {
      element=null;
      prev=null;
      next=null;
    }

    public Entry(E info) {
        element =info;
        prev=null;
        next=null;
    }

    public Entry (E info, Entry <E> n){
        element=info;
        prev=null;
        next=n;
    }
    public Entry (E info, Entry<E> n, Entry <E> p) {
        element=info;
        prev=p;
        next=n;
    }


}

Your logic is a problem. You create new empty list then interate through its contents (since there are none, this does nothing) then you return the list. It's empty, so of course the first & last pointers are null.

It needs to be more like (pseudocode)

create new empty list
iterate through the *current* list
   if(i % 2 == 1)
      add a copy of the current node from current list to the new list
return the new list
This article has been dead for over six months. Start a new discussion instead.