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());
}

}

你可能感兴趣的:(stack)