Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Task;
- import java.util.LinkedList;
- /*
- * simply stack (FILO) for char based on LinkedList<Character> functional..
- */
- public class MyStack {
- MyStack(){
- list=new LinkedList<Character>();
- }
- /*
- * throws IndexOutOfBoundsException of LinkedList..
- */
- public Character pop() throws Exception{
- return list.remove(0);
- }
- public Character top() throws Exception{
- return list.getFirst();
- }
- public void push(Character c){
- list.addFirst(c);
- }
- public boolean isEmpty(){
- return 0==list.size();
- }
- /*
- * @param String str - sequence [ { ( ) } ] and some all characters..
- * Checking. is str it right bracket sequence?
- */
- public static boolean isCorrect(String str){
- MyStack stack = new MyStack();
- for(int i=0;i<str.length();++i){
- Character cStr =str.charAt(i);
- //if bracket - opening..
- if((cStr=='(')||(cStr=='{')||(cStr=='[')){
- stack.push(cStr);
- }else if((cStr==')')||(cStr=='}')||(cStr==']')){
- //if bracket - closing.. (testing because it may be not bracket (but numbers or smt. ))
- Character cStack;
- try {
- cStack = stack.pop();
- } catch (Exception e) {
- //open bracket is less than closing
- return false;
- }
- //parsing gotten character
- switch(cStack){
- case '(':
- if(cStr!=')')
- //i hope that != correctly work with Character and i not required use equals...
- //ok, it works..
- return false;
- break;
- case '{':
- if(cStr!='}')
- return false;
- break;
- case '[':
- if(cStr!=']')
- return false;
- break;
- };
- }
- }
- if(stack.isEmpty())
- return true;
- else
- return false;
- }
- private LinkedList<Character> list;
- }
Add Comment
Please, Sign In to add comment