【发布时间】:2015-02-22 23:10:28
【问题描述】:
我正在为一项任务而苦苦挣扎。目标是使用链表填充队列实现中的空白并写入输出。所有带星号的代码 (****code****) 都是我填写的那些空白,其中一些很可能填错了 - 我不完全明白为什么。
如果有人能指出我正确的方向、修复代码或解释我填写的错误以及原因,我将不胜感激。
谢谢你, 提米
编辑:请在链接link中找到随附的作业
public class DoubleQueue {
public class QueueEmptyException extends RuntimeException{};
private **class** Node{
double num;
Node next = null;
Node (**double** num) {**this**.num = **num**;}
}
private Node head = null;
public void push(double num){
Node tmp = **new** Node(**num**);
tmp.**next** = head;
head = **num**;
}
public double pop() throws QueueEmptyException {
if (**num** == null) throw new **QueueEmptyException**(){
Node node = head;
if(node.next == null){
head = null;
return node.**next**;
}
while (node.next.**next** != null)
**node** = **node**.next;
}
double ret = aux.next.**next**;
aux.next=null;
return ret;
}
}
DoubleQueue queue = new DoubleQueue();
queue.push(17);
queue.push(23);
queue.push(15);
System.out.println(queue.pop());
System.out.println(queue.pop());
System.out.println(queue.pop());
什么是控制台输出? 1. _____ 2. _____ 3. _____
【问题讨论】:
-
它可以编译吗?它运行吗?有错误信息吗?
-
我不记得见过这样声明的类...... ???
-
我忘了更深入地阅读您的解释。那么你是在尝试构建一个链表吗?
-
为什么在
throw new **QueueEmptyException**()之后打开代码块? -
您添加了一些与您的分配相矛盾的部分。你应该把它们拿出来。
标签: java spring linked-list queue