Skip to content

Added Resize for Stacks #145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 27, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions data_structures/Stacks/Stacks.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void push(int value){
top++;
stackArray[top] = value;
}else{
System.out.println("The stack is full, can't insert value");
resize(maxSize*2);
}
}

Expand All @@ -54,7 +54,12 @@ public void push(int value){
public int pop(){
if(!isEmpty()){ //Checks for an empty stack
return stackArray[top--];
}else{
}

if(top < maxSize/4){
resize(maxSize/2);
}
else{
System.out.println("The stack is already empty");
return -1;
}
Expand All @@ -74,6 +79,16 @@ public int peek(){
}
}

private void resize(int newSize){
private int[] transferArray = new int[newSize];

for(int i = 0; i < stackArray.length(); i++){
transferArray[i] = stackArray[i];
stackArray = transferArray;
}
maxSize = newSize;
}

/**
* Returns true if the stack is empty
*
Expand Down