Skip to content

Commit

Permalink
added dynamic array stack in java
Browse files Browse the repository at this point in the history
  • Loading branch information
GauravWalia19 committed Jan 25, 2019
1 parent a0aa49e commit 9327403
Show file tree
Hide file tree
Showing 3 changed files with 189 additions and 0 deletions.
182 changes: 182 additions & 0 deletions Java/Data-Structures/STACKS/DYNAMIC-ARRAY-STACK/DynamicArrayStack.java
Original file line number Diff line number Diff line change
@@ -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<oldsize;i++) //copying current stack to arr
{
arr[i] = stack[i];
}
stack = arr;
}

/**
* pop()
* this function will pop an element from the stack
*
* @param none
*
* @return int for element to be popped
**/
public int pop() throws EmptyStackException
{
if(top==-1) //if stack is empty
{
throw new EmptyStackException();
}
int temp = stack[top];
stack[top]=0;
top--;
if(top+1==s/2)
{
shrink(); //shrink the array size
}
return temp;
}

/**
* shrink()
* it will shrink the stack array size to its half
*
* @param void
*
* @return void
**/
public void shrink()
{
s=s/2;
int[] arr = new int[s];
for(int i=0;i<s;i++)
{
arr[i] = stack[i];
}
stack = arr;
}

/**
* peek()
* it will return the top element of the stack
*
* @param void
*
* @return int for top element of the stack
**/
public int peek()
{
return stack[top];
}

/**
* print()
* it will print the whole array for checking only never do that
*
* @param void
*
* @return void
**/
public void print()
{
for(int i=0;i<s;i++)
{
System.out.print(stack[i]+" ");
}
System.out.println();
}

/**
* size()
* it will return the size of the stack
*
* @param void
*
* @return int for size of the stack
**/
public int size()
{
return top+1;
}
}

public static void main(String[] args)
{
try
{
DArrayStack S = new DArrayStack();
S.push(1);
S.push(2);
S.push(3);
S.push(4);
S.push(5);
S.push(6);
S.push(10);
S.pop();
S.push(11);
S.push(12);
S.print();
System.out.println(S.peek());
System.out.println(S.size());
}
catch(Exception e)
{
System.out.println(e);
}
}
}
1 change: 1 addition & 0 deletions Java/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
* FIXED ARRAY STACK
* [FixedArray Stack](Data-Structures/STACKS/FIXED-ARRAY-STACK/ArrayStack.java)
* DYNAMIC ARRAY STACK
* [Dynamic Array Stack](Data-Structures/STACKS/DYNAMIC-ARRAY-STACK/DynamicArrayStack.java)
* LINKED STACK
* [Linked Stack](Data-Structures/STACKS/LINKED-STACK/LinkedStack.java)
* INBUILT STACKS
Expand Down
6 changes: 6 additions & 0 deletions datastructures.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,12 @@ Indexer for Data Structures Lover

### DYNAMIC ARRAY STACK

* blog
* docs
* implementation
* [JAVA](Java/Data-Structures/STACKS/DYNAMIC-ARRAY-STACK/DynamicArrayStack.java)
* complexity

### LINKED STACK

* blog
Expand Down

0 comments on commit 9327403

Please sign in to comment.