使用数组模拟栈

package com.zzb.stack;

import java.util.Scanner;

/**
 * @Auther: Administrator
 * @Date: 2020/1/20 12:25
 * @Description: 使用 数组 模拟 栈 数据结构
 * (1)栈是一个先入后出(FILO-First In Last Out)的有序列表
 * (2)栈(stack)是限制线性表中元素的插入和删除只能在线性表的同一端进行的一种特殊线性表。允许插入和删除的
 *  一端,为变化的一端,称为栈顶(Top)另一端为固定的一端,称为栈底(Bottom)
 *  (3)根据栈的定义可知,最先放入栈中元素在栈底,最后放入的元素在栈顶,而删除元素刚好相反,最后放入的元
 *  素最先删除,最先放入的元素最后删除
 */
public class ArrayStackTest {
    public static void main(String[] args) {
        // 创建栈
        ArrayStack stack = new ArrayStack(3);
        // 接受用户输入
        String order = "";
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;
        while(loop) {
            System.out.println("---------------push:添加数据到栈----------------");
            System.out.println("---------------pop:从栈中获取数据---------------");
            System.out.println("---------------show:遍历栈---------------------");
            System.out.println("---------------top:查看栈顶数据-------------");
            System.out.println("---------------exit:退出程序----------------------");
            // 接收一个字符
            order = scanner.next();
            switch(order) {
                case "push":
                    System.out.println("输入一个数据");
                    int data = scanner.nextInt();
                    stack.push(data);
                    break;
                case "pop":
                    int pop = stack.pop();
                    System.out.println("获取的数据为  " + pop);
                    break;
                case "show":
                    stack.show();
                    break;
                case "top":
                    int top = stack.top();
                    System.out.println("队列的头部数据为  " + top);
                    break;
                case "exit":
                    loop = false;
                    scanner.close();
                    break;
                default:
                    break;
            }
        }
        System.out.println("程序退出");
    }
}

// 数组模拟栈
class ArrayStack {
    // 栈的最大容量,即数组的最大长度
    private int maxSize;
    //栈顶索引,压栈、弹栈时栈中的数组要移动的索引
    // 指向数组的最后一个元素,初始化为 -1,表示 空栈
    private int top;
    // 数组模拟栈
    private int[] array;

    // 构造器初始化栈
    public ArrayStack(int maxSize) {
        this.maxSize = maxSize;
        this.top = -1;
        this.array = new int[maxSize];
    }

    // 判断栈是否为空
    public boolean isEmpty() {
        return this.top == -1;
    }

    // 判断栈是否满
    public boolean isFull() {
        return this.top == this.maxSize - 1;
    }

    // 压栈 push
    public void push(int data) {
        // 判断栈是否满
        if(isFull()) {
            System.out.println("栈已满,不能添加数据!");
            return;
        }
        this.top++;
        this.array[top] = data;
    }

    // 弹栈 pop
    public int pop() {
        // 判断栈是否为空
        if(isEmpty()) {
            throw new RuntimeException("栈为空,不能获取数据!");
        }
        int data = this.array[top];
        this.top--;
        return data;
    }

    // 遍历栈中数据
    public void show() {
        // 判断栈是否为空
        if(isEmpty()) {
            System.out.println("栈为空!");
            return;
        }
        for(int i = this.top; i >=0; i--) {
            System.out.print(this.array[i] + "\t");
        }
        System.out.println();
    }

    // 显示栈顶数据
    public int top() {
        // 判断栈是否为空
        if(isEmpty()) {
            throw new RuntimeException("栈为空!");
        }
        return this.array[this.top];
    }


    public int getMaxSize() {
        return maxSize;
    }

    public void setMaxSize(int maxSize) {
        this.maxSize = maxSize;
    }

    public int getTop() {
        return top;
    }

    public void setTop(int top) {
        this.top = top;
    }

    public int[] getArray() {
        return array;
    }

    public void setArray(int[] array) {
        this.array = array;
    }
}
发布了28 篇原创文章 · 获赞 11 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_16645099/article/details/104051734
今日推荐