数据结构和算法——基于Java——4.1栈(数组实现栈、链表实现栈)

理论补充

在这里插入图片描述

先进后出 FILO(First-Input-Last-Out)的有序列表,限制线性表中元素的插入和删除只能在线性表的同一端进行

  • 栈顶:变化的一端
  • 栈底:固定的一端

代码实现

2.1 数组模拟栈

package com.b0.stack;

import java.util.Scanner;

public class ArrayStackDemo {
    public static void main(String[] args) {
        ArrayStack stack = new ArrayStack(5);
        String key = "";
        boolean loop = true;//控制是否退出菜单
        Scanner scanner = new Scanner(System.in);
        while (loop){
            System.out.println("show:表示显示栈");
            System.out.println("exit:退出程序");
            System.out.println("push:入栈");
            System.out.println("pop:出栈");
            System.out.println("请输入你的选择");
            key = scanner.next();
            switch (key){
                case "show":
                    stack.list();
                    break;
                case "push":
                    System.out.println("请输入一个数");
                    int value = scanner.nextInt();
                    stack.push(value);
                    break;
                case "pop":
                    try {
                        int res = stack.pop();
                        System.out.println("出栈数据是:"+res);
                    }catch (Exception e){
                        System.out.println(e.getMessage());
                    }
                    break;
                case "exit":
                    scanner.close();
                    loop = false;
                    break;
                default:
                    break;
            }
        }
        System.out.println("程序退出~");
    }
}
class ArrayStack{
    private int maxSize;//栈的大小
    private int[] stack;//数组模拟栈
    private int top = -1;//栈顶,初始化为-1
    //构造器
    public ArrayStack(int maxSize) {
        this.maxSize = maxSize;
        stack = new int[this.maxSize];
    }
    //判断栈满
    public boolean isFull(){
        return top == maxSize-1;
    }
    //栈空
    public boolean isEmpty(){
        return top == -1;
    }
    //入栈
    public void push(int num){
        //判断是否栈满
        if (isFull()){
            System.out.println("栈满");
            return;
        }
        top++;
        stack[top] = num;
    }
    //出栈
    public int pop(){
        //判断是否栈空
        if (isEmpty()){
            //抛出异常
            throw new RuntimeException("栈空,没有数据!");
        }
        int value = stack[top];
        top--;
        return value;
    }
    //遍历栈
    public void list(){
        if (isEmpty()){
            System.out.println("栈空!");
            return;
        }
        while (top != -1){
            //从栈顶开始遍历
            System.out.println(stack[top]);
            top--;
        }
    }
}

2.2 链表模拟栈(头插法)

在这里插入图片描述

package com.b0.stack;

public class LinkedStackDemo {
    public static void main(String[] args) {
        LinkedStack linkedStack = new LinkedStack();
        linkedStack.push(1);
        linkedStack.push(2);
        linkedStack.push(3);
        linkedStack.push(4);
        System.out.println("出栈值为:"+linkedStack.pop());
        System.out.println("出栈值为:"+linkedStack.pop());
        System.out.println("出栈值为:"+linkedStack.pop());
        System.out.println("出栈值为:"+linkedStack.pop());
    }
}
class LinkedStack{
    //头节点
    Node head = new Node(0);
    //栈空,尾插法
    public boolean isEmpty(){
        return head.next == null;
    }
    //入栈,头插法
    public void push(int num){
        Node node = new Node(num);
        if (isEmpty()){
            head.next = node;
        }
        node.next = head.next;
        head.next = node;
    }
    public int pop(){
        if (isEmpty()){
            //抛出异常
            throw new RuntimeException("栈空,没有数据!");
        }
        //定义零时变量,不修改head指针指向
        Node cur = head;
        int value = cur.next.no;
        cur.next = cur.next.next;
        return value;
    }
}
class Node{
    public int no;
    public Node next;

    public Node(int no) {
        this.no = no;
    }
}

2.3 链表模拟栈(尾插法,不推荐)

package com.b0.stack;

public class LinkedStackDemo {
    public static void main(String[] args) {
        LinkedStack linkedStack = new LinkedStack();
        linkedStack.push(1);
        linkedStack.push(2);
        linkedStack.push(3);
        linkedStack.push(4);
        System.out.println("出栈值为:"+linkedStack.pop());
        System.out.println("出栈值为:"+linkedStack.pop());
        System.out.println("出栈值为:"+linkedStack.pop());
        System.out.println("出栈值为:"+linkedStack.pop());
        System.out.println("出栈值为:"+linkedStack.pop());
    }
}
class LinkedStack{
    //头节点
    Node head = new Node(0);
    //栈空
    public boolean isEmpty(){
        return head.next == null;
    }
    //入栈
    public void push(int num){
        Node node = new Node(num);
        Node cur = head;
        while (true){
            if (cur.next == null)break;
            cur = cur.next;
        }
        cur.next = node;
    }
    //出栈
    public int pop(){
        if (isEmpty()){
            //抛出异常
            throw new RuntimeException("栈空,没有数据!");
        }
        //定义零时变量,不修改head指针指向
        Node cur = head;
        int value;
        while (true){
            if (cur.next.next == null){
                value = cur.next.no;
                cur.next = null;
                break;
            }
           cur = cur.next;
        }
        return value;
    }
}
class Node{
    public int no;
    public Node next;

    public Node(int no) {
        this.no = no;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值