【數據結構】棧的編寫以及棧的簡單應用

首先寫兩個棧的異常類,分別是棧滿和棧空的類:

package com.jim.stack;
public class ExceptionStackEmpty extends RuntimeException{
    public ExceptionStackEmpty(String err){
        System.out.println(err);
    }
}
package com.jim.stack;
public class ExceptionStackFull extends RuntimeException {
    public ExceptionStackFull(String err){
        System.out.println(err);
    }
}

然後編寫棧的接口,規定棧的五個基本方法的格式,參數等:

package com.jim.stack;
public interface Stack {
    public int getSize();
    public boolean isEmpty();
    public Object top() throws ExceptionStackEmpty;
    public void push(Object ele)throws ExceptionStackFull;
    public Object pop() throws ExceptionStackEmpty;
}

然後寫棧的實現(數組的方法):

package com.jim.stack.impl;
import com.jim.stack.ExceptionStackEmpty;
import com.jim.stack.ExceptionStackFull;
import com.jim.stack.Stack;
public class Stack_Array implements Stack {
    public static final int CAPACITY = 1024;
    private int capacity;
    private Object[] obj;
    private int top = -1;
             
    public Stack_Array(){
        this(CAPACITY);
    }
    public Stack_Array(int capacity2) {
        capacity = capacity2;
        obj = new Object[capacity];
    }
    @Override
    public int getSize() {
        return top+1;
    }
    @Override
    public boolean isEmpty() {
        return (top < 0);
    }
    @Override
    public Object pop() throws ExceptionStackEmpty {
        Object ele;
        if(this.isEmpty())
            throw new ExceptionStackEmpty("異常:棧爲空");
        ele = obj[this.top];
        obj[top] = null;
        top--;
        return ele;
    }
    @Override
    public void push(Object ele) throws ExceptionStackFull{
        if(this.getSize() == CAPACITY)
            throw new ExceptionStackFull("異常:棧滿");
        top++;
        obj[top] = ele;
    }
    @Override
    public Object top() throws ExceptionStackEmpty {
        if(this.isEmpty())
            throw new ExceptionStackEmpty("異常:棧空");
        return obj[this.top];
    }
}

下面是測試棧的應用:

包括數組的倒序和一些編譯器常用的檢查符號配對的方法:

package com.jim.test;
import com.jim.stack.impl.Stack_Array;
public class Test {
    public static Integer[] reverse(Integer[] a) {
        Stack_Array s = new Stack_Array(a.length);
        Integer[] b = new Integer[a.length];
        for (int i = 0; i < a.length; i++)
            s.push(a[i]);
        for (int i = 0; i < b.length; i++)
            b[i] = (Integer) s.pop();
        return b;
    }
        
    public static void matches(String str){
        char c[] = str.toCharArray();
        Stack_Array s = new Stack_Array(c.length);
            
        for(int i = 0;i < c.length;i++){
            if(c[i] == '('){
                s.push(c[i]);
            }else if(c[i] == ')'){
                s.pop();
            }
        }
        if(s.isEmpty()){
            System.out.println("配對");
        }else{
            System.out.println("不配對");
        }
    }
    /*public static void main(String[] args) {
        Integer[] a = new Integer[5];
        Integer[] b = new Integer[a.length];
        for (int i = 0; i < a.length; i++)
            a[i] = i;
        System.out.println("交換順序之前:");
        for (int i = 0; i < a.length; i++)
            System.out.print(a[i] + " ");
        b = reverse(a);
        System.out.println();
        System.out.println("交換順序之後:");
        for (int i = 0; i < a.length; i++)
            System.out.print(b[i] + " ");
    }*/
        
    public static void main(String[] args) {
        String str = "78+89*45/(89+(45-96)+45";
        matches(str);
    }
        
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章