基于单链表的FIFO队列结构

huangapple go评论60阅读模式
英文:

FIFO queue structure based on a singly linked list

问题

在我目前正在学习的Java课程中,我需要基于单向链表实现一个FIFO队列结构。我必须实现接口并覆盖3个方法:add、poll和peek。

我在poll和peek方面遇到了问题,无法在返回语句中获得一个对象。或者可能还有其他方法可以实现。对于任何帮助,我将非常感激。

add - 使用add()方法将元素添加到队列中
poll - 获取并移除队列头部的元素
peek - 类似poll,但不会移除元素

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;
        T value;

        public Node(T value) {
            this.value = value;
        }

        public T getObject() {
            return value;
        }
    }
}
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());
}
英文:

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&lt;T&gt; implements Queue2&lt;T&gt; {
Node&lt;T&gt; head;
Node&lt;T&gt; tail;
int size;
@Override
public boolean add(T e) {
Node&lt;T&gt; 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&lt;T&gt; {
Node&lt;T&gt; next;
Node&lt;T&gt; value;
public &lt;T&gt; Node(T value) {
}
public T getObject() {
**return null;** // what should be returned here?
}
}
}
 public static void main(String[] args) {
Queue&lt;String&gt; queue1 = new Queue&lt;&gt;();
queue1.add(&quot;finns&quot;);
queue1.add(&quot;bella&quot;);
queue1.add(&quot;ssara&quot;);
queue1.add(&quot;nanna&quot;);
queue1.add(&quot;anna&quot;);
System.out.println(queue1.peek());
System.out.println(queue1.poll());

答案1

得分: 2

class Node {
Node 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.

英文:
class Node&lt;T&gt; {
Node&lt;T&gt; 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&lt;T&gt;.

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

huangapple
  • 本文由 发表于 2020年10月13日 21:01:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/64335840.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定