0

trying to get these 2 methods to work, but I don't have much experience with generics and the concept is confusing a lot.

Anything getFirst(): Returns the value stored in the first node in the list. It should print an error message and return null if the list is empty.
Anything getLast(): Returns the value stored in the last node in the list. It should print an error message and return null if the list is empty.

Here's my code: (The methods above appear at the bottom)

public class Node<Anything>{

private Anything data;
private Node next;

Node(Anything a, Node n)
{
    data = a;
    next = n;
}

public Anything getData()
{ 
    return this.data;
}

public Anything setData(Anything newData)
{
    Anything oldData = this.data;
    this.data = newData;
    return oldData;
}

public void setNext(Node newNext)
{
    this.next = newNext;
}

public Node getNext()
{
    return this.next;
}
  }

-----------------------------------------------
 
public class CS2LinkedList<Anything>{  

private Node first;
private Node last;

public CS2LinkedList()
{
    first = null;
}

public boolean isEmpty()
{
    return (first == null);
}

public void addFirst(Anything d)
{
     Node temp = first;
     first = new Node(d,temp);
}


public void clear()
{
    first = null;
}

public boolean contains(Anything value)
{
    for (Node curr = first; curr != null; curr = curr.getNext())
    {
        if (value.equals(curr.getData())){
            return true;
        }
    }
    return false;
}


public String toString()
{
    StringBuilder result = new StringBuilder();  //String result = "";
    for (Node curr = first; curr != null; curr = curr.getNext())
        result.append(curr.getData() + "->");  //result = result + curr.data + "->";
    result.append("[null]");
    return result.toString();   //return result + "[null]";
}

public int size()
{   
    int size = 0;
    for (Node curr = first; curr != null; curr = curr.getNext()){
         size++;
         if (first==null){
                 size = 0;
            }
        }
        return size;
         }
    
// ------------------------ Question begins here ------------------------

public Anything getFirst()
{   
    if (first != null){
        // What should I return here? I tried returning first, (Anything) first, but none of them seems to work.
    }
    else{
        return null;
    }
}

public Anything getLast()
{
    if (first != null){
        // Same here
    }
    else{
        return null;
}
 }
enzo
  • 9,861
  • 3
  • 15
  • 38

1 Answers1

1

The class Node has a type parameter, but in your class CS2LinkedList you are using it without the type parameter. You're using it as a raw type. Raw types only exist in Java for backward compatibility with very old Java versions, which didn't have generics. You shouldn't use raw types (unless absolutely necessary because you have to work with very old code).

Everywhere where you write Node in your class CS2LinkedList, write Node<Anything> instead. For example, declare the member variables like this:

private Node<Anything> first;
private Node<Anything> last;

Write your addFirst method like this:

public void addFirst(Anything d)
{
    Node<Anything> temp = first;
    first = new Node<>(d,temp);
}

Etcetera.

Then, in you can write your getFirst() method like this:

public Anything getFirst()
{   
    if (first != null){
        return first.getData();
    }
    else{
        return null;
    }
}

And similar for the getLast() method.

You also need to modify some of the code in class Node. The constructor parameter and the parameter of the setNext method should also have a type argument:

Node(Anything a, Node<Anything> n)
{
    data = a;
    next = n;
}

public void setNext(Node<Anything> newNext)
{
    this.next = newNext;
}

As well as the return type of the getNext method:

public Node<Anything> getNext()
{
    return this.next;
}
Jesper
  • 202,709
  • 46
  • 318
  • 350