A simple integer based stack. : Stack « Collections Data Structure « Java






A simple integer based stack.

/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *//** * A simple integer based stack. * * moved to org.apache.xerces.util by neilg to support the * XPathMatcher. * @author Andy Clark, IBM * * @version $Id: IntStack.java 447241 2006-09-18 05:12:57Z mrglavas $ */publicfinalclass IntStack { // // Data // /** Stack depth. */privateint fDepth; /** Stack data. */privateint[] fData; // // Public methods // /** Returns the size of the stack. */publicint size() { return fDepth; } /** Pushes a value onto the stack. */publicvoid push(int value) { ensureCapacity(fDepth + 1); fData[fDepth++] = value; } /** Peeks at the top of the stack. */publicint peek() { return fData[fDepth - 1]; } /** Returns the element at the specified depth in the stack. */publicint elementAt(int depth) { return fData[depth]; } /** Pops a value off of the stack. */publicint pop() { return fData[--fDepth]; } /** Clears the stack. */publicvoid clear() { fDepth = 0; } // debugging /** Prints the stack. */publicvoid print() { System.out.print('('); System.out.print(fDepth); System.out.print(") {"); for (int i = 0; i < fDepth; i++) { if (i == 3) { System.out.print(" ..."); break; } System.out.print(' '); System.out.print(fData[i]); if (i < fDepth - 1) { System.out.print(','); } } System.out.print(" }"); System.out.println(); } // // Private methods // /** Ensures capacity. */privatevoid ensureCapacity(int size) { if (fData == null) { fData = newint[32]; } elseif (fData.length <= size) { int[] newdata = newint[fData.length * 2]; System.arraycopy(fData, 0, newdata, 0, fData.length); fData = newdata; } } } // class IntStack 








Related examples in the same category

1.Demonstration of Stack ClassDemonstration of Stack Class
2.Stack in java.utilStack in java.util
3.Stack data structureStack data structure
4.Bracket Checker
5.String Reverser Through Stack String Reverser Through Stack
6.Link stackLink stack
7.Triangular numbersTriangular numbers
8.Triangular numbers with stack replaces recursionTriangular numbers with stack replaces recursion
9.Show String ReversalsShow String Reversals
10.Generic stack demo with annotation
11.Character Stack
12.A faster, smaller stack implementation.
13.Growable Object stack with type specific access methods
14.Growable int stack with type specific access methods
15.Stack for boolean values
16.extends ArrayList to create Stack
17.Growable String stack with type specific access methods.
18.A very simple unsynchronized stack. This one is faster than the java.util-Version.
19.Pop an empty stack ntry times and catch the resulting exceptionPop an empty stack ntry times and catch the resulting exception
20.Object Stack
21.Fast stack
close