Merge pull request #399 from khalil2535/master

Update Stacks.java
This commit is contained in:
Christian Bender 2018-04-01 20:37:55 +02:00 committed by GitHub
commit dba791e515
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -43,6 +43,7 @@ class Stack{
stackArray[top] = value; stackArray[top] = value;
}else{ }else{
resize(maxSize*2); resize(maxSize*2);
push(value);// don't forget push after resizing
} }
} }
@ -58,6 +59,7 @@ class Stack{
if(top < maxSize/4){ if(top < maxSize/4){
resize(maxSize/2); resize(maxSize/2);
return pop();// don't forget pop after resizing
} }
else{ else{
System.out.println("The stack is already empty"); System.out.println("The stack is already empty");
@ -80,9 +82,11 @@ class Stack{
} }
private void resize(int newSize){ private void resize(int newSize){
private int[] transferArray = new int[newSize]; //private int[] transferArray = new int[newSize]; we can't put modifires here !
int[] transferArray = new int[newSize];
for(int i = 0; i < stackArray.length(); i++){ //for(int i = 0; i < stackArray.length(); i++){ the length isn't a method .
for(int i = 0; i < stackArray.length; i++){
transferArray[i] = stackArray[i]; transferArray[i] = stackArray[i];
stackArray = transferArray; stackArray = transferArray;
} }