Merge pull request #59 from cosmeoes/patch-1

Added a few edge cases in the stack class
This commit is contained in:
Anup Kumar Panwar 2017-06-21 12:38:56 +05:30 committed by GitHub
commit 370b988cf7

View File

@ -38,8 +38,12 @@ class Stack{
* @param value The element added
*/
public void push(int value){
top++;
stackArray[top] = value;
if(!isFull()){ //Checks for a full stack
top++;
stackArray[top] = value;
}else{
System.out.prinln("The stack is full, can't insert value");
}
}
/**
@ -48,7 +52,12 @@ class Stack{
* @return value popped off the Stack
*/
public int pop(){
return stackArray[top--];
if(!isEmpty()){ //Checks for an empty stack
return stackArray[top--];
}else{
System.out.println("The stack is already empty");
return -1;
}
}
/**
@ -57,7 +66,12 @@ class Stack{
* @return element at the top of the stack
*/
public int peek(){
return stackArray[top];
if(!isEmpty()){ //Checks for an empty stack
return stackArray[top];
}else{
System.out.println("The stack is empty, cant peek");
return -1;
}
}
/**