package com.test.common;
public class LinkedStack {
private class Node{
U item;
Node next;
Node(){item=null;next=null;}
Node(U item,Node next)
{
this.item=item;
this.next=next;
}
boolean end()
{
return item==null && next==null;
}
}
private Node top=new Node();
public void push(T item)
{
top=new Node(item,top);
}
public T pop()
{
T result=top.item;
if(!top.end())
{
top=top.next;
}
return result;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
LinkedStack stack=new LinkedStack();
stack.push("jianghuiwen");
stack.push("wangjun");
System.out.println(stack.pop());
}
}
代码如上所示,创建了一个泛型的栈,并且提供pop和push两种操作方法。输出结果如图