1+ /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others.
2+ 3+ * Creation Date : 2015-01-10 06:15:46
4+ * Last modification : 2006-05-31
5+ by : Narasimha Karumanchi
6+ * File Name : DynamicArrayStack.java
7+ * Book Title : Data Structures And Algorithms Made In Java
8+ * Warranty : This software is provided "as is" without any
9+ * warranty; without even the implied warranty of
10+ * merchantability or fitness for a particular purpose.
11+ *
12+ */
13+
14+
15+ package chapter4stacks ;
16+
17+ public class DynamicArrayStack {
18+ // Length of the array used to implement the stack.
19+ protected int capacity ;
20+
21+ // Default array capacity.
22+ public static final int CAPACITY = 16 ; // power of 2
23+
24+ public static int MINCAPACITY =1 <<15 ; // power of 2
25+
26+ // Array used to implement the stack.
27+ protected int [] stackRep ;
28+
29+ // Index of the top element of the stack in the array.
30+ protected int top = -1 ;
31+
32+ // Initializes the stack to use an array of default length.
33+ public DynamicArrayStack () {
34+ this (CAPACITY ); // default capacity
35+ }
36+
37+ // Initializes the stack to use an array of given length.
38+ public DynamicArrayStack (int cap ) {
39+ capacity = cap ;
40+ stackRep = new int [capacity ]; // compiler may give warning, but this
41+ // is ok
42+ }
43+
44+ // Returns the number of elements in the stack. This method runs in O(1) time.
45+ public int size () {
46+ return (top + 1 );
47+ }
48+
49+ // Testes whether the stack is empty. This method runs in O(1) time.
50+ public boolean isEmpty () {
51+ return (top < 0 );
52+ }
53+
54+ // Inserts an element at the top of the stack. This method runs in O(1) time.
55+ public void push (int data ) throws Exception {
56+ if (size () == capacity )
57+ expand ();
58+ stackRep [++top ] = data ;
59+ }
60+
61+ private void expand () {
62+ int length = size ();
63+ int [] newstack =new int [length <<1 ];
64+ System .arraycopy (stackRep ,0 ,newstack ,0 ,length );
65+ stackRep =newstack ;
66+ }
67+
68+ // dynamic array operation: shrinks to 1/2 if more than than 3/4 empty
69+ @ SuppressWarnings ("unused" )
70+ private void shrink () {
71+ int length = top + 1 ;
72+ if (length <=MINCAPACITY || top <<2 >= length )
73+ return ;
74+ length =length + (top <<1 ); // still means shrink to at 1/2 or less of the heap
75+ if (top <MINCAPACITY ) length = MINCAPACITY ;
76+ int [] newstack =new int [length ];
77+ System .arraycopy (stackRep ,0 ,newstack ,0 ,top +1 );
78+ stackRep =newstack ;
79+ }
80+
81+ // Inspects the element at the top of the stack. This method runs in O(1) time.
82+ public int top () throws Exception {
83+ if (isEmpty ())
84+ throw new Exception ("Stack is empty." );
85+ return stackRep [top ];
86+ }
87+
88+ // Removes the top element from the stack. This method runs in O(1) time.
89+ public int pop () throws Exception {
90+ int data ;
91+ if (isEmpty ())
92+ throw new Exception ("Stack is empty." );
93+ data = stackRep [top ];
94+ stackRep [top --] = Integer .MIN_VALUE ; // dereference S[top] for garbage collection.
95+ return data ;
96+ }
97+
98+ // Returns a string representation of the stack as a list of elements, with
99+ // the top element at the end: [ ... , prev, top ]. This method runs in O(n)
100+ // time, where n is the size of the stack.
101+ public String toString () {
102+ String s ;
103+ s = "[" ;
104+ if (size () > 0 )
105+ s += stackRep [0 ];
106+ if (size () > 1 )
107+ for (int i = 1 ; i <= size () - 1 ; i ++) {
108+ s += ", " + stackRep [i ];
109+ }
110+ return s + "]" ;
111+ }
112+ }
0 commit comments