0

In the java course I'm following right now, I'm required to implement a FIFO queue structure based on a singly linked list. I have to implement interface and override 3 methods: add, poll and peek.

I'm stuck with poll and peek and can not get an Object in return statement. Or may be there is another way to do it. For any help would be very grateful.

add -Use add() method to add elements into the Queue
poll - Fetching and removing the element at the head of the queue
peek- as pool without removing

public class Queue<T> implements Queue2<T> {

    Node<T> head;
    Node<T> tail;
    int size;

    @Override
    public boolean add(T e) {

        Node<T> node = new Node(e);

        if (head == null) {
            head = node;
            tail = node;
        } else {

            tail.next = node;
            tail = node;
            node.next = null;
        }
        size++;
        return true;
    }


    @Override
    public T poll() {
        if (size == 0)
            return null;
        else {
            T obj = head.getObject();
            head = head.next;
            if (head == null) {
                tail = null;
            }
            size--;

            return obj;
        }

    }

    @Override

    public T peek() {
        if (head == null)
            return null;
        else {

            return head.getObject();

        }
    }


    class Node<T> {
        Node<T> next;

        Node<T> value;

        public <T> Node(T value) {


        }

        public T getObject() {
            **return null;** // what should be returned here?
        }
    }
}
 public static void main(String[] args) {
        Queue<String> queue1 = new Queue<>();
        queue1.add("finns");
        queue1.add("bella");
        queue1.add("ssara");
        queue1.add("nanna");
        queue1.add("anna");
       System.out.println(queue1.peek());
       System.out.println(queue1.poll());

2
  • "can not get an Object in return statement": Please explain. Commented Oct 13, 2020 at 13:04
  • 1
    My guess is that getObject should return the value that the node is initialized with, ie in your example strings such as "finns", "bella" etc. Also, you might want to reconsider whether the value field should actually be a Node or whether it should be the generic type T. Commented Oct 13, 2020 at 13:04

1 Answer 1

2
class Node<T> {
    Node<T> next;
    T value;
    public Node(T value) {
        this.value=value;
    }

    public T getObject() {
        return value;
    }
}

The value in Node is your object so should be of type T , not Node<T>.

Remove the <T> from the constructor otherwise you are defining a new T and the assignment this.value=value; will not work.

Sign up to request clarification or add additional context in comments.

1 Comment

ahhhhh, those mistakes) Thanks!

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.