contains(Object input)
elementAt(int index)
peek()
search(Object input)
Below is the code snippet.
Click here to view the previous code of CustomStack.
public boolean contains(Object input){
boolean found = false;
if(empty()){
throw new RuntimeException( "Stack is already empty. No elements to check." );
}
for(int i = 0; i<size(); i++) {
if (stackElements[i].hashCode() == input.hashCode()) {
found = true;
break;
}
}
return found;
}
public Object elementAt(int index){
return get(index);
}
public Object peek(){
return top();
}
public int search(Object input){
int index = -1;
if(empty()){
throw new RuntimeException( "Stack is already empty. No elements to search." );
}
for(int i = 0; i<size(); i++) {
if (stackElements[i].hashCode() == input.hashCode()) {
index = i;
break;
}
}
return index;
}
0 comments:
Post a Comment