From 9327403a53cf2c7fe30d597aa6ff132421149a35 Mon Sep 17 00:00:00 2001 From: GauravWalia19 Date: Fri, 25 Jan 2019 21:39:46 +0530 Subject: [PATCH] added dynamic array stack in java --- .../DynamicArrayStack.java | 182 ++++++++++++++++++ Java/README.md | 1 + datastructures.md | 6 + 3 files changed, 189 insertions(+) create mode 100644 Java/Data-Structures/STACKS/DYNAMIC-ARRAY-STACK/DynamicArrayStack.java 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