KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > util > SimpleStack


1 package jodd.util;
2
3 import java.util.LinkedList;
4 import java.util.NoSuchElementException;
5
6 /**
7  * Simple Stack (LIFO) class.
8  */

9 public class SimpleStack {
10
11     private LinkedList list = new LinkedList();
12
13     /**
14      * Stack push.
15      *
16      * @param o
17      */

18     public void push(Object o) {
19         list.addLast(o);
20     }
21
22     /**
23      * Stack pop.
24      *
25      * @return poped object from stack
26      */

27     public Object pop() {
28         Object result = null;
29         try {
30             result = list.removeLast();
31         } catch (NoSuchElementException nsee) {
32         }
33         return result;
34     }
35
36
37     public Object[] popAll() {
38         Object[] res = list.toArray();
39         list.clear();
40         return res;
41     }
42
43     /**
44      * Peek element from stack.
45      *
46      * @return peeked object
47      */

48     public Object peek() {
49         return list.getLast();
50     }
51
52
53     /**
54      * Is stack empty?
55      *
56      * @return true if stack is empty
57      */

58     public boolean isEmpty() {
59         return list.isEmpty();
60     }
61
62 }
63
Popular Tags