1

I think I set up my class correct to be generic but when i try to call methods i cant seem to set up my other methods correct. Im I supposed to cast my variables to be generic? or do I cast my methods to variables?

public class LinkedList<E>
{
    // reference to the head node.
    private E head;
    private int listCount;


    public boolean delete(E string)
    // post: removes the element at the specified position in this list.
    {       
        Node current = head.getNext();

        while(true){
            if(current == null){
                return false;
            }else if(current.getNext().getData().equals(string)){
                if(current.getNext() == null){
                    current.setNext(null);
                }else{
                    current.setNext(current.getNext().getNext());
                }
                listCount--; // decrement the number of elements variable
                return true;
            }else{
                current = current.getNext();
            }   
        }
    }

  private class Node<E extends Comparable<E>>
    {
        // reference to the next node in the chain,
        E next;
        // data carried by this node.
        // could be of any type you need.
        E data;


        // Node constructor
        public Node(E _data)
        {
            next = null;
            data = _data;
        }

        // another Node constructor if we want to
        // specify the node to point to.
        public Node(E _data, E _next)
        {
            next = _next;
            data = _data;
        }

        // these methods should be self-explanatory
        public E getData()
        {
            return data;
        }

        public void setData(E _data)
        {
            data = _data;
        }

        public E getNext()
        {
            return next;
        }

        public void setNext(E _next)
        {
            next = _next;
        }
    }


}
6
  • 1
    What exactly is the problem? Commented Oct 13, 2014 at 15:04
  • 2
    Shouldn't head and next be of type Node<E> instead of E? Commented Oct 13, 2014 at 15:05
  • Your code seems okayish (though @JonK above me is absolutely right), could you edit your question to include an example of what you want to do, but fails? Commented Oct 13, 2014 at 15:05
  • Side notes: 1: LinkedList is already taken, you really should find another name. 2: Use another parameter name for your Node class, such as T for example. 3: We usually expect a class named AnythingList to implements the interface List. Commented Oct 13, 2014 at 15:06
  • 1
    I really hope that variable naming in delete(E string) is some weird artifact from refactoring process Commented Oct 13, 2014 at 15:06

1 Answer 1

3

The types of your variables were a bit messed up.

  • Node.next needs to be a Node
  • LinkedList.head needs to be Node
  • Node does not need to be generic. (The E type parameter is in scope for the inner class.)

Here's a version that compiles:

class LinkedList<E> {
    // reference to the head node.
    private Node head;
    private int listCount;

    public boolean delete(E string)
    // post: removes the element at the specified position in this list.
    {
        Node current = head;

        while (true) {
            if (current == null) {
                return false;
            } else if (current.getData().equals(string)) {
                if (current.getNext() == null) {
                    current.setNext(null);
                } else {
                    current.setNext(current.getNext().getNext());
                }
                listCount--; // decrement the number of elements variable
                return true;
            } else {
                current = current.getNext();
            }
        }
    }

    private class Node {
        // reference to the next node in the chain,
        Node next;
        // data carried by this node.
        // could be of any type you need.
        E data;

        // Node constructor
        public Node(E _data) {
            next = null;
            data = _data;
        }

        // another Node constructor if we want to
        // specify the node to point to.
        public Node(E _data, Node _next) {
            next = _next;
            data = _data;
        }

        // these methods should be self-explanatory
        public E getData() {
            return data;
        }

        public void setData(E _data) {
            data = _data;
        }

        public Node getNext() {
            return next;
        }

        public void setNext(Node _next) {
            next = _next;
        }
    }

}

Looking at your delete method, I think it's a bit buggy though. When you arrive at a node where the data equals string, you change the next-pointer of that node while you should be changing the next-pointer of the previous node.

I would try something like this:

    Node current = head, prev = null;
    while (current != null) {
        if (current.getData().equals(string)) {
            // Remove current from list
            if (current == head) {
                head = current.getNext();
            } else {
                prev.setNext(current.getNext());
            }

            listCount--; // decrement the number of elements variable
            return true;
        }
        prev = current;
        current = current.getNext();
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. I was able to get everything to compile. I have one more question however. I have to have class Node as private class Node<E extends comparable<E>> I tired having LInkedList as public class LinkedList<E extends Comparable<E>> but that just left me with this warning from eclipse "LinkedList.Node is a raw type. References to generic type LinkedList<E>.Node<E> should be parameterized"
Hmm. Interesting. I can't tell for sure what this is due to. Post another question and we'll see if someone else knows...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.