java数据结构——栈

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yoonerloop/article/details/81606518

栈是一种特殊的线性表,仅能在线性表的一端操作,栈顶允许操作,栈底不允许操作,遵循“后进先出”的特点。数据结构图如下:

基本操作包括创建栈、入栈、出栈、获取栈顶元素、获取栈的大小等。栈的实现分为两种,一种是通过数组实现;一种是通过链表来实现。二者的区别是数组实现栈简单简洁,而使用链表实现比较复杂;组数的实现容量有限,需要指定初始容量,而链表的实现栈的容量是无限的,不需要指定初始容量。接下来分别介绍:

一、数组实现栈

栈的构造方法:

public class ArrayStack {

    private int[] stack;
    //栈的大小
    private int size;
    //栈顶数据
    private int top;

    /**
     * 默认栈的大小
     */
    public ArrayStack() {
        stack = new int[5];
        top = -1;
        size = 0;
    }

    /**
     * 指定栈的大小
     */
    public ArrayStack(int size) {
        stack = new int[size];
        top = -1;
        size = 0;
    }
}

1、入栈:栈顶插入元素

    /**
     * 存入数据
     */
    public void push(int data) {
        if (size == stack.length) {
            throw new RuntimeException("stack is full");
        }
        stack[size++] = data;
        top = data;
    }

2、出栈:栈顶删除元素

    /**
     * 删除数据
     */
    public int pop() {
        if (isEmpty()) {
            top = -1;
            return -1;
        }
        int data = stack[size - 1];
        top = stack[--size];
        return data;
    }

3、查看栈内元素

    /**
     * 查看栈内元素
     */
    public void display() {
        if (isEmpty()) {
            System.out.println("栈内元素为空");
            return;
        }
        for (int i = 0; i < size; i++) {
            System.out.print(stack[i] + " ");
        }
        System.out.println();
    }

4、查看栈顶元素

    /**
     * 查看栈顶元素
     */
    public int getTop() {
        return top;
    }

5、查看栈内元素大小

    /**
     * 栈的实际大小
     */
    public int size() {
        return size;
    }

6、栈是是否为空

    /**
     * 栈是否为空
     */
    public boolean isEmpty() {
        return size() == 0;
    }

7、栈是否满了

    /**
     * 栈是否满了
     */
    public boolean isFull() {
        return size() == stack.length;
    }

二、链表实现栈

链表的节点:

public class Node {
    //下一个节点
    public Node next;
    //当前节点的数据
    public String data;

    public Node(String data) {
        this.data = data;
        this.next = null;
    }
}

栈的构造方法:

public class LinkedListStack {

    //栈顶
    private Node top;
    //栈底元素
    private Node bottom;
    //栈的长度
    private int size;

    public LinkedListStack() {
        top = null;
        size = 0;
    }
}

1、入栈:栈顶插入元素

    /**
     * 插入数据
     */
    public void push(String data) {
        Node node = new Node(data);
        if (isEmpty()) {
            //第一次进来栈底元素和栈顶元素一致
            bottom = node;
            top = node;
        } else {
            //栈顶元素指向下一个,并且栈顶重置
            top.next = node;
            top = node;
        }
        size++;
    }

2、出栈:栈顶删除元素

    /**
     * 删除元素
     */
    public void pop() {
        if (isEmpty()) {
            System.out.println("栈内数据为空,没有可删除的数据");
            return;
        }
        Node lastNode = null;
        Node currentNode = bottom;
        //当前元素的下一个元素为空跳出循环
        while (currentNode.next != null) {
            //上一个元素指向下一个元素
            lastNode = currentNode;
            //向下循环走一步
            currentNode = currentNode.next;
        }
        //执行完毕上一个元素指向下一个元素设置为null
        lastNode.next = null;
        //栈顶元素重置
        top = lastNode;
        size--;
    }

3、查看栈内元素

    /**
     * 查看栈内元素
     */
    public void display() {
        if (isEmpty()) {
            System.out.println("栈内元素为空");
            return;
        }
        Node currentNoe = bottom;
        while (currentNoe != null) {
            System.out.print(currentNoe.data + " ");
            currentNoe = currentNoe.next;
        }
        System.out.println();
    }

4、查看栈顶元素

    /**
     * 查看栈顶元素
     */
    public String getTop() {
        if (top == null){
            return null;
        }
        return top.data;
    }

5、查看栈内元素大小

    /**
     * 栈的实际大小
     */
    public int size() {
        return size;
    }

6、栈是否为空

    /**
     * 栈是否为空
     */
    public boolean isEmpty() {
        return size() == 0;
    }

猜你喜欢

转载自blog.csdn.net/yoonerloop/article/details/81606518