java数组链表模拟栈

入栈则反之
在这里插入图片描述

1)先进后出的数据结构,通常用于保存方法中的参数,局部变量;
2)在java中,所有基本类型(byte, short, int, long, float, double, char, boolean)和引用类型的变量都在栈中存储;
3)栈中数据的生存空间一般在当前scopes内(由{…}括起来的区域;
4)栈的存取速度比堆要快;
5)栈中的数据可以共享;

// 数组模拟栈
public class Test {
    public static void main(String[] args) {
        Inn inn = new Inn(10);
        Scanner sc = new Scanner(System.in);
        System.out.println("数组模拟栈");
        boolean bo = true;
        while (bo){
            System.out.println("push:添加数据到栈(入栈)");
            System.out.println("pop:从栈中取出数据(出栈)");
            System.out.println("show:展示栈");
            System.out.println("exit:退出程序");
            String key = sc.next();
            switch(key){
                case "push":
                    System.out.println("请输入一个值");
                    inn.push(sc.nextInt());
                    break;
                case "pop":
                    try{
                        System.out.println("取出:"+inn.pop());
                    }catch(Exception e){
                        System.out.println(e.getMessage());
                    }
                    break;
                case "show":
                    inn.show();
                    break;
                case "exit":
                    sc.close();
                    bo = false;
                    break;
            }
        }
        System.out.println("退出程序成功~");
    }
}


class Inn{
    private int maxSize;    // 栈的大小
    private int top = -1;   // 栈顶   初始化值为-1
    private int[] arr;      // 数组模拟栈

    // 构造器
    public Inn(int maxSize){
        this.maxSize = maxSize;
        this.arr = new int[maxSize];
    }


    // 栈满
    public boolean isFull(){
        return top == maxSize-1;
    }


    // 栈空
    public boolean isNull(){
        return top == -1;
    }


    // 入栈
    public void push(int value){
        if (isFull()){
            System.out.println("凶残,栈满了~");

        }
        top++;
        arr[top] = value;
    }


    // 出栈
    public int pop(){
        if (isNull()){
            throw new RuntimeException("我丢,你的栈是空的~");
        }
        int value = arr[top];
        top--;
        return value;
    }


    // 遍历栈(先入后出)
    public void show(){
        if (isNull()){
            System.out.println("栈空~");
        }
        for (int i = top; i > -1; i--) {
            System.out.printf("arr[%d]=%d\n",i,arr[i]);
        }
    }
}

链表模拟栈

public class TestDemo {
    public static void main(String[] args) {
        Pojo pojo = new Pojo(1);
        Pojo pojo2 = new Pojo(2);
        Pojo pojo3 = new Pojo(3);
        Pojo pojo4 = new Pojo(4);
        Linkedlist ll = new Linkedlist();
        ll.push(pojo);
        ll.push(pojo2);
        ll.push(pojo3);
        ll.push(pojo4);
        ll.show();
        System.out.println("取出后~~~");
        try{
            System.out.println("取出:"+ll.pop());
            System.out.println("取出:"+ll.pop());
            System.out.println("取出:"+ll.pop());
        }catch(Exception e){
            System.out.println(e.getMessage());
        }
        ll.show();
    }
}


class Linkedlist{

    // 作为约束
    private Pojo head = new Pojo(-1);

    // 栈空
    public boolean isNull(){
        return  head.getNext() == null;
    }

    // 入栈
    public void push(Pojo pojo){

        Pojo node = head;

        while (true){
            if (node.getNext() == null){
                break;
            }
            node = node.getNext();
        }
        // 加入数据
        node.setNext(pojo);
        pojo.setPre(node);
    }


    // 出栈
    public Pojo pop(){
        if (isNull()){
            throw new RuntimeException("栈空~");
        }

        Pojo node = head;

        while(true){
            if (node.getNext() == null){
                break;
            }
            node = node.getNext();
        }

        Pojo value = node;
        Pojo po = node.getPre();
        po.setNext(null);
        return value;
    }


    // 展示栈
    // 栈(先入后出),反转遍历
    public void show(){

        if (isNull()){
            System.out.println("栈空~");
            return;
        }

        Pojo node = head;

        while(true){
            if (node.getNext() == null){
                break;
            }
            node = node.getNext();
        }

        while(true){
            if (node.getPre() == null){
                break;
            }
            System.out.println(node);
            node = node.getPre();
        }
    }

}

class Pojo{
    private int value;      // 值
    private Pojo next;      // 指向下一个节点
    private Pojo pre;       // 指向前一个节点

    public Pojo(int value){
        this.value = value;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public Pojo getNext() {
        return next;
    }

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

    public Pojo getPre() {
        return pre;
    }

    public void setPre(Pojo pre) {
        this.pre = pre;
    }

    @Override
    public String toString() {
        return "Pojo{" +
                "value=" + value +
                '}';
    }
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值