一、栈的应用

  1、直接应用

    符号匹配

    中缀表达式转换为后缀表达式

    计算后缀表达式

    实现函数调用(包括递归)

    求范围误差(极差)

    网页浏览器中的back按钮和历史记录

    文本编辑器中的撤销操作

    HTML和XML中的tag匹配

  2、间接应用

    作为一个算法的辅助数据结构

    其他数据结构的组件(例如模拟队列)

二、数组实现

  

//这个没有进行测试过,后面的进行了简单测试
public
class Stack { private int top; private int capacity; private int [] array; public Stack(){ capacity = 1; array = new int[capacity]; top = -1; } public boolean isEmpty(){ return top==-1; } public boolean isFullStack(){ return top==array.length-1; } public void push(int data){ if(isFullStack()){ System.out.println("Stack OverFlow"); } else { array[++top]=data; } } public int pop(){ if(isEmpty()){ System.out.println("Stack is empty"); return 0; } else { return array[top--]; } } public void deleteStack(){ top = -1; } }

三、动态数组实现

  

//基于动态数组的stack实现
public class DynArrayStack {
    private int top;
    private int capacity;
    private int [] array;
    public DynArrayStack(){
        capacity = 1;
        array = new int[capacity];
        top = -1;
    }
    public boolean isEmpty(){
        return top == -1;
    }
    public boolean isStackFull(){
       // System.out.println("top " + top);
        return top == array.length-1;
    }
    public void push(int data){
        if(isStackFull()){
            doubleStack();
        }
        array[++top]=data;//注意这里是加号在前面
    }
    //倍增太多可能导致溢出
    private void doubleStack(){
        int [] newArray = new int[capacity*2];
        //拷贝函数
        System.arraycopy(array,0,newArray,0,capacity);
        capacity*=2;
        array = newArray;
    }
    public int pop(){
        if(isEmpty()){
            System.out.println("Stack is empty");
            return 0;
        }
        else{
            return  array[top--];//这个是减号在前
        }
    }
    public void deleteStack(){
        top = -1;
    }

    public static void main(String[] args) {
        DynArrayStack stack = new DynArrayStack();
        stack.push(1);
        stack.push(2);
        System.out.println(stack.pop());
        System.out.println(stack.pop());
    }
}

四、使用链表实现

  

class LLNode {
    private int data;
    private LLNode next;

    public LLNode(int data){
        this.data = data;
    }

    public void setData(int data){
        this.data = data;
    }

    public int getData(){
        return data;
    }

    public void setNext(LLNode next){
        this.next = next;
    }

    public LLNode getNext(){
        return this.next;
    }

}

public class LLStack {
    private LLNode headNode;
    public LLStack(){
        this.headNode = null;
    }
    public void push(int data){
        if(headNode==null){
            headNode = new LLNode(data);
        }
        else{
            LLNode temp = new LLNode(data);
            temp.setNext(headNode);
            headNode = temp;
        }
    }
    public int pop(){
        if(isEmpty()){
            System.out.println("Stack is empty");
            return 0;
        }
        else{
            int data = headNode.getData();
            LLNode temp = headNode.getNext();
            headNode = null;
            headNode = temp;
            return data;
        }
    }
    public boolean isEmpty(){
        return headNode == null;
    }

    public static void main(String[] args) {
        LLStack stack = new LLStack();
        stack.push(1);
        stack.push(2);
        System.out.println(stack.pop());
        System.out.println(stack.pop());
    }
}

五、其他

  java中的数组赋值方法 System.arraycopy()

 

 

posted on 2018-01-27 22:06  ZhangのBlog  阅读(124)  评论(0编辑  收藏  举报