Download | Plain Text | No Line Numbers


  1. /*
  2.  * Copyright (c) 2008, Manuel Mausz <manuel at mausz.at>
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions are met:
  7.  * * Redistributions of source code must retain the above copyright
  8.  * notice, this list of conditions and the following disclaimer.
  9.  * * Redistributions in binary form must reproduce the above copyright
  10.  * notice, this list of conditions and the following disclaimer in the
  11.  * documentation and/or other materials provided with the distribution.
  12.  * * Neither the name of the copyright holders nor the
  13.  * names of its contributors may be used to endorse or promote products
  14.  * derived from this software without specific prior written permission.
  15.  *
  16.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  19.  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  20.  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  21.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  22.  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  23.  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  25.  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  26.  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  27.  * DAMAGE.
  28.  */
  29.  
  30. /**
  31.  * Implements a stack using an array
  32.  *
  33.  * @version 1.0
  34.  * @author Manuel Mausz (manuel at mausz.at)
  35.  * @author http://manuel.mausz.at/
  36.  */
  37. class Stack
  38. {
  39. private int[] stack;
  40. private int i = 0;
  41.  
  42. /**
  43.   * constructor
  44.   *
  45.   * @param size initialize stack size
  46.   */
  47. Stack(int size)
  48. {
  49. stack = new int[size];
  50. }
  51.  
  52. public void resize()
  53. {
  54. resize(2);
  55. }
  56.  
  57. /**
  58.   * resizes the internal stack
  59.   *
  60.   * @param factor resize factor. default is 2
  61.   */
  62. public void resize(int factor)
  63. {
  64. if (factor <= 1)
  65. return;
  66. int b[] = stack.clone();
  67. stack = new int[stack.length * factor];
  68. System.arraycopy(b, 0, stack, 0, i);
  69. }
  70.  
  71. /**
  72.   * push item onto stack
  73.   *
  74.   * @param n item
  75.   */
  76. public void push(int n)
  77. {
  78. if (i >= stack.length)
  79. resize();
  80. stack[i++] = n;
  81. }
  82.  
  83. /**
  84.   * pop item from stack
  85.   *
  86.   * @return item
  87.   */
  88. public int pop()
  89. {
  90. if (i == 0)
  91. return 0;
  92. return stack[--i];
  93. }
  94.  
  95. /**
  96.   * check if stack has items
  97.   *
  98.   * @return boolean
  99.   */
  100. public boolean hasNext()
  101. {
  102. return (i > 0);
  103. }
  104.  
  105. /**
  106.   * converts stack to string
  107.   *
  108.   * @return string
  109.   */
  110. public String toString()
  111. {
  112. String str = "";
  113. for (int j = 0; j < i; j++)
  114. str += stack[j] + "\n";
  115. return str;
  116. }
  117. }
  118.