/Users/lyon/j4p/src/collections/linkedlist/Stack.java
|
1 /*
2 * Stack.java
3 *
4 * Created on December 4, 2002
5 */
6
7 package collections.linkedlist;
8
9 import java.util.LinkedList;
10
11 /**
12 * Shows how to use a LinkedList as a Stack.
13 * @author Thomas Rowland
14 */
15 public class Stack {
16
17 private static LinkedList list = new LinkedList();
18
19 public static void main(String[] args) {
20 Stack stack = new Stack();
21 stack.push("good");
22 stack.push("bad");
23 stack.push("ugly");
24
25 System.out.println(stack.peek());
26 System.out.println(stack.pop());
27 System.out.println(stack.pop());
28 System.out.println(stack.pop());
29 }
30
31 /**
32 * push an object onto the top of the stack
33 */
34 public void push(Object o) {
35 list.addFirst(o);
36 }
37
38 /**
39 * pop an object off the top of the stack
40 */
41 public Object pop() {
42 return list.removeFirst();
43 }
44
45 /**
46 * peek at an object on the top of the stack
47 */
48 public Object peek() {
49 return list.getFirst();
50 }
51 }
52