Home / Data structures and Algorithms by Java Examples / Stacks / Dynamic Array Stack Implementation using JAVA Example
Dynamic Array Stack Implementation using JAVA Example
6717 views.
DynamicArrayBasedImplementation.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
//size, isEmpty, expand, shrink, push, top, pop, toString
class DynamicArrayStackImpl {
    private int capacity;
     
    //default capacity
    private final static int DEFAULT_CAPACITY = 16;
     
    //to maintain minimum stackArray capacity.
    private final int INITIAL_CAPACITY;
     
    //top pointer
    private int top = -1;
     
    //stack holder
    private int[] stackArray;
     
    //Allocate memory with default capacity
    public DynamicArrayStackImpl() {
        //by default array initialize
        this(DynamicArrayStackImpl.DEFAULT_CAPACITY);
    }
     
    //Allocate memory with custom capacity
    public DynamicArrayStackImpl(int capacity) {
        this.capacity = capacity;
        this.INITIAL_CAPACITY = capacity;
        stackArray = new int[this.capacity];
    }
     
    //To get size of the stack
    public int size() {
        return top + 1;
    }
     
    //To check stack isEmpty
    public boolean isEmpty(){
        return top == -1;
    }
     
    //Double the stack size - if stack is full
    public void expand() {
        //double the capacity
        capacity = capacity * 2;
         
        //create new array with double capacity
        int[] newStack = new int[capacity];
         
        //copy old stack into new stack array
        System.arraycopy(stackArray, 0, newStack, 0, size());
         
        //assign new stack to stackArray.
        stackArray = newStack;
    }
     
    //Shrink to 1/2 if more than 3/4 is empty
    public void shrink() {
        //IF 1/2 array size is greater than INITIAL_CAPACITY size.
        //So that not shrink array less than INITIAL_CAPACITY
        if (INITIAL_CAPACITY <= (capacity >> 2)) {
            //Get 3/4 size
            int minSize = capacity >> 2;
            if (top < minSize) {
                //so stack is less than 3/4.
                 
                //divide capacity by 2. So new size is 1/2;
                capacity = capacity / 2;
                 
                //create new array with new capacity
                int[] newStack = new int[capacity];
                 
                //copy old stack into new stack array
                System.arraycopy(stackArray, 0, newStack, 0, size());
                 
                //assign new stack to stackArray.
                stackArray = newStack;
            }
        }
    }
     
    //Push ADT
    public void push(int data) throws Exception {
        //If stack is full - expand the size of the stack.
        if (size() == capacity) {
            expand();
        }
         
        //Otherwise add data to the stack.
        stackArray[++top] = data;
    }
     
    //Reading Top value
    public int top() throws Exception {
        //Exception handling - if stack is empty
        if (isEmpty()) {
            throw new Exception("Stack is empty!");
        }
         
        //Otherwise return top value.
        return stackArray[top];
    }
     
    //Pop ADT
    public int pop() throws Exception {
        //Exception handling - if stack is empty
        if (isEmpty()) {
            throw new Exception("Stack is empty!");
        }
         
        //Otherwise pop the top element
        int data = stackArray[top];
        stackArray[top--] = 0;
         
        //shrink if stack size is less than 3/4.
        shrink();
         
        return data;
    }
     
    //toString stackArray
    public String toString() {
        String arrayString = "[";
         
            for (int index = 0; index <= top; index++) {
                if (index == 0) {
                    //adding data if index is 0
                    arrayString += stackArray[index];
                }
                else {
                    //adding data with comma if index is not 0.
                    arrayString += "," + stackArray[index];
                }
            }
         
        arrayString += "]";
         
        return arrayString;
    }
}
 
/*
 * Dyanmic Array based stack implementation
 * using Java Example program
 */
public class DynamicArrayBasedImplementation {
    public static void main(String[] args) {
        //Using custom stack
        DynamicArrayStackImpl stack = new DynamicArrayStackImpl();
         
        try {
            //check stack is empty
            System.out.println("isEmpty: "+stack.isEmpty());
             
            //adding data to the stack
            stack.push(10);
            stack.push(20);
            stack.push(30);
            stack.push(40);
            stack.push(50);
 
             
            //will throw stack is full exception
            //stack.push(60);
             
            //print stack
            System.out.println("Stack: "+stack);
             
            //top element
            System.out.println("Top: "+stack.top());
            System.out.println("Stack: "+stack);
             
            //pop element
            System.out.println("Pop data: "+stack.pop());
            System.out.println("Stack: "+stack);
             
            //read size of the stack
            System.out.println("Size is "+stack.size());
             
            //check stack is empty
            System.out.println("isEmpty: "+stack.isEmpty());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Output
isEmpty: true
Stack: [10,20,30,40,50]
Top: 50
Stack: [10,20,30,40,50]
Pop data: 50
Stack: [10,20,30,40]
Size is 4
isEmpty: false
Related Examples
   Simple Array Stack Implementation using JAVA Example
   Dynamic Array Stack Implementation using JAVA Example
   Linked List Stack Implementation using JAVA Example
Copyright © 2016 Learn by Examples, All rights reserved