public static LinkedList<Double> list(Stack stack) {
//create doubly linked list object
LinkedList<Double> list = new LinkedList<>();
// add double element to linked list as a test
list.add(22.5);
list.add(8.5);
list.add(3.5);
list.add(4.5);
// check if stack is not empty then add doubly linked list elements to the Stack
// then pritn stack elements and return this list
if (!stack.isEmpty()) {
System.out.println("Stack is not Empty");
} else {
stack.addAll(list);
for (Object stack1 : stack) {
System.out.println(stack1);
}
}
return list;
}
编写以堆栈作为参数的java方法,并在双向链表中填充其元素并返回此列表。
答案 0 :(得分:2)
最简单的解决方案是
public static <V> List<V> list(<Stack<V> stack) {
return new LinkedList<>(stack);
}
但是,我怀疑你在作业中缺少一些要求。例如他们是否希望你实现自己的双向链表和/或堆栈?