I'm working on learning Java and I'm trying out single linked lists. I am implementing an interface I've made for it too, but I keep getting an error saying that my main class LinkedList "is not abstract and therefore does not override method search(java.lang.Object) in LinkedListInterface". What am I doing wrong? I seem to be implementing all my methods but I'm still getting an error.

Here is my source code: Node.java

public class Node<E> {

private E element;
private Node<E> next;

public Node() {
    this(null, null);
}

public Node(E element, Node<E> next) {
    this.element = element;
    this.next = next;
}

public E getElement() {
    return element;
}

public Node<E> getNext() {
    return next;
}

public void setElement(E element) {
    this.element = element;
}

public void setNext(Node<E> next) {
    this.next = next;
}
} // End Node

LinkedListInterface.java

public interface LinkedListInterface<E> {

/**
*  Adds the specified element to the head of the linked list.
*  @param element element to be added to the head of the list.
*/
public void addFirst(E element);

/**
*  Removes and return the element from the head of the linked list.
*  @return the element removed from the head of the linked list.
*/
public E removeFirst();

/**
*  Adds the specified element to the end of the linked list.
*  @param element element to be added to the end of the list.
*/
public void addLast(E element);

/**
*  Removes and return the element from the end of the linked list.
*  @return the element removed from the end of the linked list.
*/
public E removeLast(); //EXTRA CREDIT

/**
*  Returns a string representation of the linked list.
*  @return a string representation of the linked list.
*/
public String traverse();

/**
*  Returns a boolean of true if the searched term is within the list.
*  @return a boolean.
*/
public boolean search(E element);

} // End LinkedListInterface 

LinkedList.java:

public class LinkedList<E> extends Node<E> implements LinkedListInterface {

//variables
private Node<E> head;
private Node<E> tail;
private int size;

//Constructor
public LinkedList() {
  head = null;
    tail = null;
  size = 0;
}

// Check that list is empty
public boolean isEmpty() { 
  if (head == null)
     return true;
  else
     return false;
}

// Return the size of the list
public int size() { 
  return size;
}

// Add element to the first position on the list
public void addFirst(E element) { 
  Node<E> temp = new Node<E>(element, null);
  if (isEmpty()) {
     head = temp; //tail should also be updated...
  } else {
     temp.setNext(head); //points temps next to head
     head = temp; //move head to temp
  }
  size++;

}// End addFirst

// Remove element at the first position on the list
public E removeFirst() {
  if (isEmpty())
     throw new EmptyListException("error");

  Node<E> temp = head; //point to the head
  E result = head.getElement(); 
  head = head.getNext(); //set the new head to the next element
  temp.setNext(null); //set temp next to null which removes it
  size--; //decrease list size

  return result;

}// End removeFirst

// Add element to the last position on the list
public void addLast(E element) { 
  Node<E> temp = new Node<E>(element, null);
  if (isEmpty())
     tail = temp; //tail should also be updated...
  else {
     temp.setNext(tail); //points temps next to head
     tail = temp; //move head to temp
  }
  size++;

}// End addLast

// Remove element at the last position on the list
public E removeLast() {
  if (isEmpty())
     throw new EmptyListException("error");

  Node<E> temp = tail; //point to the head
  E result = tail.getElement(); //
  tail = tail.getNext(); //set the new head to the next element
  temp.setNext(null); //set temp next to null which removes it
  size--; //decrease list size

  return result;
}// End removeLast

// Returns a string representation of the list
public String traverse() {
  if (isEmpty())
     throw new EmptyListException("error");

  Node<E> temp = head;

  String result = "Head-->";
  int i = size();
  while (i > 0){
     result += temp.getElement() + "-->";
     temp = temp.getNext();
     i--;
  }  
  return result;
}// End traverse

// Returns true if searched item exists within the list
public boolean search(E element) { 
  if (isEmpty())
     throw new EmptyListException("error");

  Node<E> itr = head.getNext();

  while (!itr.getElement().equals(element)) {
     itr = itr.getNext();
  }
    return true;
}// End search

public static void main(String [] args) {

  LinkedList<String> myList = new LinkedList<String>();

  myList.addFirst("MSP");
  myList.addFirst("RST");
  myList.addFirst("STL");

  System.out.println(myList.traverse());

  myList.removeFirst();

  System.out.println(myList.traverse());

}// End main

}// End LinkedList class
有帮助吗?

解决方案

You are implementing the raw interface LinkedListInterface, where E becomes object. Implement the generic version instead, by adding <E>:

//                                                                       vvv
public class LinkedList<E> extends Node<E> implements LinkedListInterface<E> {
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top