zoukankan      html  css  js  c++  java
  • 堆叠顺序 和 推断是否成对括号

    public class SqStack {
    
    	public int[] data;
    	public int top;
    	public SqStack() {
    		data = new int[20];
    		top = -1;
    	}
    	public boolean empty() {
    		return top == -1;
    		
    	}
    	public boolean push(int e) {
    		if(top == 19) {
    			return false;
    		}
    		top++;
    		data[top] = e;
    		return true;
    	}
    	public int[] pop() {
    		int[] arr = new int[2];
    		if(top == -1) {
    			arr[0] = 0;
    			return arr;
    		}
    		arr[0] = 1;
    		arr[1] = data[top--];
    		return arr;
    	}
    	public int[] top() {
    		int[] arr = new int[2];
    		if(top == -1) {
    			arr[0] = 0;
    			return arr;
    		}
    		arr[0] = 1;
    		arr[1] = data[top];
    		return arr;
    	}
    	public void display() {
    		for(int i=0; i<=top; i++) {
    			System.out.print(data[i]+ " ");
    		}
    		System.out.println();
    	}
    	
    	public static void main(String[] args) {
    		SqStack ss = new SqStack();
    		System.out.println(ss.empty());
    		ss.push(2);
    		ss.push(3);
    		ss.display();
    		ss.pop();
    		ss.display();
    		ss.push(1);
    		ss.push(2);
    		ss.display();
    		ss.pop();
    		ss.push(4);
    		ss.display();
    		ss.top();
    		ss.display();
    
    	}
    
    }
    


    结果:

    true
    2 3 
    2 
    2 1 2 
    2 1 4 
    2 1 4 
    


    public class SqStack {
    
    	public int[] data;
    	public int top;
    	public SqStack() {
    		data = new int[20];
    		top = -1;
    	}
    	public boolean empty() {
    		return top == -1;
    		
    	}
    	public boolean push(int e) {
    		if(top == 19) {
    			return false;
    		}
    		top++;
    		data[top] = e;
    		return true;
    	}
    	public int[] pop() {
    		int[] arr = new int[2];
    		if(top == -1) {
    			arr[0] = 0;
    			return arr;
    		}
    		arr[0] = 1;
    		arr[1] = data[top--];
    		return arr;
    	}
    	public int[] top() {
    		int[] arr = new int[2];
    		if(top == -1) {
    			arr[0] = 0;
    			return arr;
    		}
    		arr[0] = 1;
    		arr[1] = data[top];
    		return arr;
    	}
    	public void display() {
    		for(int i=0; i<=top; i++) {
    			System.out.print(data[i]+ " ");
    		}
    		System.out.println();
    	}
    	
    	//检查括号是否配对
    	public boolean check(String s) {
    		for(int i=0; i<s.length(); i++) {
    			if(s.charAt(i) == '(') {
    				push(1);
    			} else if(s.charAt(i) == ')') {
    				if(empty()) 
    					return false;
    				else 
    					pop();
    			}
    		}
    		if(empty()) {
    			return true;
    		} else {
    			return false;
    		}
    	}
    	public static void main(String[] args) {
    		SqStack ss = new SqStack();
    		String s = new String("(sd()f(() )d()j)");
    		System.out.println(ss.check(s));
    		s = new String("(sd()f(() ))d()j)");
    		System.out.println(ss.check(s));
    		s = new String("(sd()f((() )d()j)");
    		System.out.println(ss.check(s));
    
    	}
    
    }
    

    结果:

    true
    false
    false
    



    版权声明:本文博主原创文章,博客,未经同意不得转载。

  • 相关阅读:
    100多个淘宝抢的双11红包怎么用?最多才能累积使用15个
    灵动标签调用友情链接
    台湾短片:《血战古.宁.头》
    discuz_style_default.xml 修改
    discuz门户文章页面模板修改
    ps快捷键
    拍摄好的图片,如何抠图去背景纯白..
    Centos6.5安装rabbmitmq蛋碎
    好用的YUM源
    python_函数的参数
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/4908740.html
Copyright © 2011-2022 走看看