1 package example;
2
3 import java.util.ArrayDeque;
4 import java.util.Deque;
5
6 public class IntegerStack {
7 private Deque<Integer> data = new ArrayDeque<Integer>();
8
9 public void push(Integer element) {
10 data.addFirst(element);
11 }
12
13 public Integer pop() {
14 return data.removeFirst();
15 }
16
17 public Integer peek() {
18 return data.peekFirst();
19 }
20
21 public String toString() {
22 return data.toString();
23 }
24
25 public static void main(String[] args) {
26 IntegerStack stack = new IntegerStack();
27 for (int i = 0; i < 5; i++) {
28 stack.push(i);
29 }
30 System.out.println("elements: " + stack);
31
32 int m = stack.pop();
33 System.out.println("Popped :" + m);
34
35 System.out.println("elements: " + stack);
36
37 System.out.println("Peeked :" + stack.peek());
38 System.out.println("elements: " + stack);
39 System.out.println("first element: " + stack.data.getFirst());
40 System.out.println("last element: " + stack.data.getLast());
41 System.out.println("remove last element: " + stack.data.removeLast());
42 System.out.println("elements: " + stack);
43 }
44 }
posted on 2007-09-30 15:23
交口称赞 阅读(1386)
评论(9) 编辑 收藏 所属分类:
Java6 、
java相关