LinkedList实现一个stack,实现其中的push(),top和pop方法

2014-11-24 09:53:38 · 作者: · 浏览: 1
[java]
package test;

import java.util.LinkedList;

public class test2 {
static LinkedList link =null;
public static void main(String args[])throws Exception {
link = new LinkedList();
push(1);
int d =top();
System.out.println(d);
pop();
pop();


}
static boolean push(int i){
link.addFirst(i);
return true;
}
static int top(){
int get = 0;
try{
get = link.getFirst();
}catch(Exception e){System.out.println("没有数据");}
return get;
}
static boolean pop(){
try{
link.removeFirst();
}catch(Exception e){
System.out.println("没有数据");
return false;
}
return true;
}
}