diff --git a/Java/Data-Structures/STACKS/DYNAMIC-ARRAY-STACK/DynamicArrayStack.java b/Java/Data-Structures/STACKS/DYNAMIC-ARRAY-STACK/DynamicArrayStack.java new file mode 100644 index 0000000..4e10e3e --- /dev/null +++ b/Java/Data-Structures/STACKS/DYNAMIC-ARRAY-STACK/DynamicArrayStack.java @@ -0,0 +1,182 @@ +import java.util.*; +abstract class DynamicArrayStack +{ + private static class DArrayStack + { + private int[] stack; //array for stack + private int top; //top for stack + private int s; //s for calculating the size of the stack + + /** + * DArrayStack() + * this is default constructor + **/ + public DArrayStack() + { + this(1); //call parameterised contructor as size 1 + } + + /** + * DArrayStack() + * this is parametrized constructor + * + * @param num for size of the input + **/ + public DArrayStack(int num) + { + this.s = num; + top=-1; + this.stack = new int[num]; + } + + /** + * push() + * this function will push an element to the stack + * + * @param num for element to push + * + * @return void + **/ + public void push(int num) + { + if(top==s-1) + { + expand(); //expand array when stack gets full + } + + top++; + stack[top] = num; //push to stack + } + + /** + * expand() + * this function will expand the array to its double + * + * @param void + * + * @return void + **/ + public void expand() + { + int oldsize = s; + s = s*2; //increase the array size to double + int[] arr = new int[s]; + for(int i=0;i