📄 linkedliststack.java
字号:
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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -