Skip to content

Added a few edge cases in the stack class #59

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
Jun 21, 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
22 changes: 18 additions & 4 deletions data_structures/Stacks.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,12 @@ public Stack(int size){
* @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");
}
}

/**
Expand All @@ -48,7 +52,12 @@ public void push(int value){
* @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;
}
}

/**
Expand All @@ -57,7 +66,12 @@ public int pop(){
* @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;
}
}

/**
Expand Down