File tree 1 file changed +17
-2
lines changed
1 file changed +17
-2
lines changed Original file line number Diff line number Diff line change @@ -42,7 +42,7 @@ public void push(int value){
42
42
top ++;
43
43
stackArray [top ] = value ;
44
44
}else {
45
- System . out . println ( "The stack is full, can't insert value" );
45
+ resize ( maxSize * 2 );
46
46
}
47
47
}
48
48
@@ -54,7 +54,12 @@ public void push(int value){
54
54
public int pop (){
55
55
if (!isEmpty ()){ //Checks for an empty stack
56
56
return stackArray [top --];
57
- }else {
57
+ }
58
+
59
+ if (top < maxSize /4 ){
60
+ resize (maxSize /2 );
61
+ }
62
+ else {
58
63
System .out .println ("The stack is already empty" );
59
64
return -1 ;
60
65
}
@@ -74,6 +79,16 @@ public int peek(){
74
79
}
75
80
}
76
81
82
+ private void resize (int newSize ){
83
+ private int [] transferArray = new int [newSize ];
84
+
85
+ for (int i = 0 ; i < stackArray .length (); i ++){
86
+ transferArray [i ] = stackArray [i ];
87
+ stackArray = transferArray ;
88
+ }
89
+ maxSize = newSize ;
90
+ }
91
+
77
92
/**
78
93
* Returns true if the stack is empty
79
94
*
You can’t perform that action at this time.
0 commit comments