栈的实现

基于顺序表的栈实现:

package StackExercise;

public class MyStack {
    // 最大尺寸
    public static final int MAXSIZE = 20;

    // 顶端
    private int top;
    // 所有的栈
    private Object[] objs;
    // 栈的最大值
    private int max;

    // 初始化
    public MyStack() {
        this(MyStack.MAXSIZE);
    }

    public MyStack(int size) {
        if (size <= 0) {
            throw new RuntimeException("初始化栈必须大于0!" + size);
        } else {
            objs = new Object[size];
            top = -1;
            max = size;
        }
    }

    // 压入元素
    public void push(T t) {
        if (top == max - 1) {
            throw new RuntimeException("栈以满!");
        } else {
            objs[++top] = t;
        }
    }

    // 弹出顶部元素
    public T pop() {
        if (top == -1) {
            throw new RuntimeException("栈为空!");
        } else {
            T ret = (T) objs[top];
            objs[top--] = null;
            return ret;
        }
    }

    // 是否为空
    public boolean isEmpty() {
        return top == -1;
    }

    // 是否满
    public boolean isFull() {
        return top == max - 1;
    }

    // 查看顶部元素,不弹出
    public T peek() {
        if (top == -1) {
            throw new RuntimeException("栈为空!");
        } else {
            return (T) objs[top];
        }

    }

    public void print() {
        for (int i = 0; i <= top; i++) {
            System.err.println(objs[i]);
        }
    }
}

测试代码:

package StackExercise;

public class Main {
    public static void main(String[] args) {
        MyStack t = new MyStack();
        for (int i = 0; i < 10; i++) {
            t.push(i);
        }
        t.print();
        for (int i = 0; i < 3; i++) {
            t.pop();
        }
        t.print();
    }
}

基于顺序表的链表实现:

package StackExercise;

public class LinkedStack {
    // 最大尺寸
    public static final int MAXSIZE = 20;
    // 所有的栈
    public LinkedStackData head;
    // 栈的顶端
    public int top;
    // 栈的最大值
    public int max;

    // 初始化
    public LinkedStack() {
        this(LinkedStack.MAXSIZE);
    }

    public LinkedStack(int size) {
        if (size <= 0) {
            throw new RuntimeException("初始化栈必须大于0!" + size);
        } else {
            head = new LinkedStackData();
            top = -1;
            max = size;
        }
    }

    // 是否为空
    public boolean isEmpty() {
        return top == -1;
    }

    // 是否满
    public boolean isFull() {
        return top == max - 1;
    }

    // 查看顶部元素,不弹出
    public T peek() {
        if (top == -1) {
            throw new RuntimeException("栈为空!");
        } else {
            return (T) (findTop().data);
        }
    }

    // 弹出顶部元素
    public T pop() {
        T ret = null;
        if (top == -1) {
            throw new RuntimeException("栈为空!");
        } else {
            LinkedStackData tData = head;
            while (tData.next != null) {
                if (tData.next.next == null) {
                    ret = tData.next.data;
                    tData.next = null;
                    break;
                }
                tData = tData.next;
            }
        }
        return ret;
    }

    // 压入元素
    public void push(T t) {
        if (top == max - 1) {
            throw new RuntimeException("栈以满!");
        } else {
            LinkedStackData newData = new LinkedStackData();
            newData.data = t;
            LinkedStackData topData = findTop();
            topData.next = newData;
            top++;
        }
    }

    // 寻找顶部元素
    private LinkedStackData findTop() {
        LinkedStackData tData = head;
        while (tData.next != null) {
            tData = tData.next;
        }
        return tData;
    }

    // 打印
    public void print() {
        LinkedStackData i = head;
        while (i.next != null) {
            i = i.next;
            System.err.println(i.data);
        }
        
    }
}

基础数据类:

package StackExercise;

public class LinkedStackData {
    // 下一个
    public LinkedStackData next;
    // 当前数据
    public T data;
}

测试代码:

package StackExercise;

public class Main {
    public static void main(String[] args) {

        LinkedStack t = new LinkedStack();
        for (int i = 0; i < 10; i++) {
            t.push(i);
        }
        t.print();
        for (int i = 0; i < 3; i++) {
            t.pop();
        }
        t.print();
    }
}

以为这个会比链表东西会多一些,但是写起来感觉比链表好像要简单,也可能是方法少,但是理解起来可能对新手有一些难度吧。

你可能感兴趣的:(栈的实现)