通用堆栈
在计算机程序开发领域,堆栈是一个不容忽视的概念,堆栈是一种数据结构。堆栈都是一种数据项按序排列的数据结构,只能在一端(称为栈顶)对数据项进行插入和删除。
//来 自 时 代 J a v a - N o w J a v a . c o m
public class GenericStack{
private Nodefirst;
public Item pop() {
if (first == null) {
throw new java.lang.IndexOutOfBoundsException();
}
Item firstval = first.item;
first = first.next;
return firstval;
}
public void push(Item s) {
Nodenewfirst = new Node();
newfirst.item = s;
newfirst.next = first;
first = newfirst;/*来自 n o w j a v a . c o m*/
}
public static void main(String[] args) {
GenericStack<String> stack = new GenericStack<String>();
while (!StdIn.isEmpty()) {
String s = StdIn.readString();
//StdOut.print(s);
if (s.equals("-"))
System.out.println(stack.pop());
&nbs