英文:
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<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());
答案1
得分: 2
class Node
Node
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<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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论