java堆栈类源程序import java.util.LinkedList;import java.io.*;import java.util.NoSuchElementException;/** * * @version 1.00 06/12/19 */public class MStack { LinkedList list; public MStack() { list=new LinkedList(); } public static void main(String[] args) { MStack MStack=new MStack(); MStack.push("ok"); MStack.push("i am ok"); System.out.println(MStack.pop()); System.out.println(MStack.pop()); System.out.println(MStack.pop()); } //弹出元素 public Object pop() { try { Object o=list.getFirst(); list.removeFirst(); return o; } catch(NoSuchElementException e) { //System.out.println(e); return null; } } //压入元素 public void push(Object o) { list.addFirst(o); } //得到栈顶元素 public Object getTop() { if(list.size()!=0) { return list.getFirst(); } else { return null; } } }Junit测试程序import junit.framework.*;public class TestMStack extends TestCase{ MStack s=new MStack(); public TestMStack(String name) { super(name); } public void setUp() { } public void testCase1() { MStack MStack=new MStack(); s.push("ok"); MStack.push("ok"); Assert.assertEquals(s.getTop(),MStack.getTop()); } public static void main(String[] args) { junit.textui.TestRunner.run(TestMStack.class); } }// http://blog.csdn.net/wfisone/archive/2009/05/09/4163778.aspx