linkedliststack.java
来自「java 完全探索的随书源码」· Java 代码 · 共 55 行
JAVA
55 行
import java.util.LinkedList;import java.util.EmptyStackException;public class LinkedListStack { private LinkedList listDelegate = new LinkedList(); public boolean empty() { return listDelegate.isEmpty(); } // return the element on top of the stack without removing it public Object peek() { if ( !this.empty() ) { return listDelegate.getFirst(); } throw new EmptyStackException(); } // return the element on top of the stack and remove it public Object pop() { if ( !this.empty() ) { return listDelegate.removeFirst(); } throw new EmptyStackException(); } // place an object on top of the stack public Object push(Object item) { listDelegate.addFirst(item); return item; } // look for an object in the stack public int search(Object item) { return listDelegate.indexOf(item); } public static void main( String args[] ) { LinkedListStack stack = new LinkedListStack(); // push 5 elements onto the stack for ( int i=0; i<5; i++ ) { stack.push( new Integer(i) ); } System.out.println("Peek at the top of the stack: " + stack.peek() ); // empty the stack while ( !stack.empty() ) { System.out.println("Pop: " + stack.pop() ); } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?