JAVA_模拟stack
package com.kk.Collection;
import java.util.LinkedList;
/**
* 模拟一个栈
* 后进先出(LIFO)
*/
public class MyStack {
private LinkedList ll=new LinkedList();
//有底的用push,没底的用put
public void push(Object o){
ll.addFirst(o);
}
public Object pop(){
return ll.removeFirst();
}
public Object peek(){
return ll.getFirst();
}
public boolean empty(){
return ll.isEmpty();
}
public static void main(String[] args) {
MyStack stack=new MyStack();
stack.push("a");
stack.push("b");
stack.push("c");
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.empty());
}
}
版权声明:本文为BigIdiot原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。