【发布时间】:2018-04-27 01:45:32
【问题描述】:
我需要使用链表填充堆栈。我有一个名为 GenericStack 的通用类,带有常规方法。我有一个 Evaluator 类,它有我的主要方法,我必须读取后缀表达式的输入文件。我有一个 Node 类来构建链表。要使用 6 5 2 3 + 8 * + 3 + * 之类的后缀表达式读取文件,我不知道如何用文件填充链表或如何读取它。
public class GenericStack {
private Node top;
public GenericStack(){
top = null;
}
public boolean isEmpty(){
return (top == null);
}
public void push (Object newItem){
top = new Node(newItem,top);
}
public Objectpop(){
if(isEmpty()){
System.out.println("Trying to pop when stack is empty.");
return null;
}
else{
Node temp = top;
top = top.next;
return temp.info;
}
}
void popAll(){
top = null;
}
public Object peek(){
if(isEmpty()){
System.out.println("Trying to peek when stack is empty.");
return null;
}
else{
return top.info;
}
}
}
public class Evaluator {
public static void main(String[] args){
GenericStack myStack = new GenericStack();
}
}
public class Node {
Object info;
Node next;
Node(Object info, Node next){
this.info = info;
this.next = next;
}
}
【问题讨论】:
标签: java linked-list stack