01: /*
02: * @(#)Stack.java 1.2 04/12/06
03: *
04: * Copyright (c) 1997-2003 Sun Microsystems, Inc. All Rights Reserved.
05: *
06: * See the file "LICENSE.txt" for information on usage and redistribution
07: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
08: */
09: package org.pnuts.util;
10:
11: /**
12: * Stack implemented by a linked list
13: */
14: public class Stack {
15: Cell cell;
16:
17: int count = 0;
18:
19: public void push(Object object) {
20: Cell old = cell;
21: cell = new Cell();
22: cell.object = object;
23: cell.next = old;
24: count++;
25: }
26:
27: public Object pop() {
28: Cell c = cell;
29: cell = cell.next;
30: count--;
31: return c.object;
32: }
33:
34: public int size() {
35: return count;
36: }
37:
38: public Object peek() {
39: return cell.object;
40: }
41:
42: public void removeAllElements() {
43: cell = null;
44: count = 0;
45: }
46:
47: public void copyInto(Object[] array) {
48: Cell c = cell;
49: for (int i = 0; i < count; i++) {
50: array[count - 1 - i] = c.object;
51: c = c.next;
52: }
53: }
54: }
|