File tree Expand file tree Collapse file tree 1 file changed +18
-4
lines changed Expand file tree Collapse file tree 1 file changed +18
-4
lines changed Original file line number Diff line number Diff line change @@ -38,8 +38,12 @@ public Stack(int size){
38
38
* @param value The element added
39
39
*/
40
40
public void push (int value ){
41
- top ++;
42
- stackArray [top ] = value ;
41
+ if (!isFull ()){ //Checks for a full stack
42
+ top ++;
43
+ stackArray [top ] = value ;
44
+ }else {
45
+ System .out .prinln ("The stack is full, can't insert value" );
46
+ }
43
47
}
44
48
45
49
/**
@@ -48,7 +52,12 @@ public void push(int value){
48
52
* @return value popped off the Stack
49
53
*/
50
54
public int pop (){
51
- return stackArray [top --];
55
+ if (!isEmpty ()){ //Checks for an empty stack
56
+ return stackArray [top --];
57
+ }else {
58
+ System .out .println ("The stack is already empty" );
59
+ return -1 ;
60
+ }
52
61
}
53
62
54
63
/**
@@ -57,7 +66,12 @@ public int pop(){
57
66
* @return element at the top of the stack
58
67
*/
59
68
public int peek (){
60
- return stackArray [top ];
69
+ if (!isEmpty ()){ //Checks for an empty stack
70
+ return stackArray [top ];
71
+ }else {
72
+ System .out .println ("The stack is empty, cant peek" );
73
+ return -1 ;
74
+ }
61
75
}
62
76
63
77
/**
You can’t perform that action at this time.
0 commit comments