packageorg.container;//栈importjava.util.*;publicclassMyStack{LinkedListlinkedlist=newLinkedList();publicvoidpush(Objectobj){linkedlist.addFirst(obj);}publicObjectpop(){r...
package org.container;
//栈
import java.util.*;
public class MyStack {
LinkedList linkedlist = new LinkedList();
public void push(Object obj){
linkedlist.addFirst(obj);
}
public Object pop(){
return linkedlist.removeFirst();
}
public Object peek(){
return linkedlist.getFirst();
}
public boolean empty(){
return linkedlist.isEmpty();
}
public String toString(){
return linkedlist.toString();}
public static void main(String[] args){
MyStack ms = new MyStack();
ms.push("Spring");
ms.push("Summer");
ms.push("Autumn");
ms.push("Winter");
System.out.print(ms.pop()+" ");
System.out.print(ms.peek()+" ");
System.out.print(ms.pop()+" ");
System.out.print(ms.empty()+" ");
System.out.print(ms);
}
}
去掉pop()或peek()就不能调用
但是我看到在队列中直接就能调用 不用自己写这些函数。
展开