栈的方法(push()、stack.pop()、stack.peek()、stack.empty())具体用法(图解、思路与实现)

本文介绍了Java中栈的基本操作,包括push()、pop()、peek()、empty()的使用方法,并通过实例展示了它们的功能。栈的底层实现是数组,示例代码展示了如何实现这些方法,包括栈满时的扩容和栈空时的检查。最后提到了将栈改为泛型的可能改进。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

栈底层实际上就是个数组

在这里插入图片描述
栈方法

Stack stack = new Stack<>();
stack.push(1);//入栈
stack.push(20);
1.System.out.println(stack.peek());//查看拿到栈顶元素 不删除 结果为20
2.System.out.println(stack.pop());//出栈 删除栈顶元素 结果为20
3.System.out.println(stack.peek());//结果为1
4.System.out.println(stack.empty());//判断栈是否为空 结果为false

在这里插入图片描述
Stack stack = new Stack<>();
stack.push(1);//入栈
stack.push(2);
stack.push(3);
stack.push(4);
stack.push(5);
System.out.println(stack.size());//栈中含有的元素 5
System.out.println(stack.search(2));//返回从栈顶往前数第size()- i(i为栈下标)个元素 4

import java.util.Stack;
public class MyStack {
    public static void main(String[] args) {
        Stack<Integer> stack = new Stack<>();
        stack.push(1);//入栈
        stack.push(2);
        stack.push(3);
        stack.push(4);
        stack.push(5);
        System.out.println(stack.size());//栈中含有的元素  5
        System.out.println(stack.search(2));//返回从栈顶往前数第size()-i(i为下表)个元素  4
        /*System.out.println(stack.peek());//查看拿到栈顶元素 不删除  结果为20
        *//*System.out.println(stack.pop());//出栈  删除栈顶元素  20
        System.out.println(stack.peek());//1*//*
        System.out.println(stack.empty());//判断栈是否为空  false*/

    }
}

top表示的是当前可以存放数据元素的下标

在这里插入图片描述

在这里插入图片描述
push元素val

public boolean full() {
return this.top == this.elem.length;//判断栈是否满了
}
public void push(int val) {
if (full()) {//如果是满的 扩容 拷贝原来数据 2
this.elem = Arrays.copyOf(this.elem,2
this.elem.length);
}
this.elem[top++] = val;//如果没有满 val放到栈顶top指向的位置 然后top++
}

pop() 元素
public boolean empty() {//判断是否为空的
return this.top == 0;
}
public int pop() {
if (empty()) {
throw new RuntimeException(“栈空!”);//防止栈中有-1元素
//return -1//栈中有可能有-1元素
}
int data = this.elem[this.top-1];//拿到要pop的数据的数据 用data存放
this.top–;//删掉那个元素
return data;
//return this.elem[–this.top];//上面的简写

peek()元素
public int peek() {
if (empty()) {
throw new RuntimeException(“栈空!”);//防止栈中有-1元素
//return -1//栈中有可能有-1元素
}
return this.elem[this.top-1];//此时拿到top-1的元素 但是top依旧是top
}

size()
public int size() {
return this.top;
}

import java.util.Arrays;

public class MyStack {
    public int[] elem;//数组  默认值为null
    public int top;//表示可以存放数据元素的下标

    public MyStack() {//构造方法
        this.elem = new int[10];//数组放10个元素  都是0
    }

    public void push(int val) {
        if (full()) {//如果是满的  扩容  拷贝原来数据 *2
            this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
        }
        this.elem[top++] = val;//如果没有满 val放到top指向的位置 然后top++
    }

   /* public boolean empty() {//判断是否为空的
        return this.top == 0;
    }*/
    public boolean full() {
        return this.top == this.elem.length;//判断栈是否满了
    }
    public boolean empty() {//判断是否为空的
        return this.top == 0;
    }
    public int pop() {
        if (empty()) {
            throw new RuntimeException("栈空!");//防止栈中有-1元素
            //return -1//栈中有可能有-1元素
        }
        int data = this.elem[this.top-1];//拿到要pop的数据的数据  用data存放
        this.top--;//删掉那个元素
        return data;
        //return this.elem[--this.top];//上面的简写

    }
    public int peek() {
        if (empty()) {
            throw new RuntimeException("栈空!");//防止栈中有-1元素
            //return -1//栈中有可能有-1元素
        }
        return this.elem[this.top-1];//此时拿到top-1的元素  但是top依旧是top
    }
    public int size() {
        return this.top;
    }
}

改为泛型

import java.util.Arrays;

public class MyStack<T> {
    public T[] elem;//数组  默认值为null
    public int top;//表示可以存放数据元素的下标

    public MyStack() {//构造方法
        this.elem = (T[]) new Object[10];//数组放10个元素  都是0
    }

    public void push(T val) {
        if (full()) {//如果是满的  扩容  拷贝原来数据 *2
            this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
        }
        this.elem[top++] = val;//如果没有满 val放到top指向的位置 然后top++
    }

   /* public boolean empty() {//判断是否为空的
        return this.top == 0;
    }*/
    public boolean full() {
        return this.top == this.elem.length;//判断栈是否满了
    }
    public boolean empty() {//判断是否为空的
        return this.top == 0;
    }
    public T pop() {
        if (empty()) {
            throw new RuntimeException("栈空!");//防止栈中有-1元素
            //return -1//栈中有可能有-1元素
        }
        T data = this.elem[this.top-1];//拿到要pop的数据的数据  用data存放
        this.top--;//删掉那个元素
        return data;
        //return this.elem[--this.top];//上面的简写

    }
    public T peek() {
        if (empty()) {
            throw new RuntimeException("栈空!");//防止栈中有-1元素
            //return -1//栈中有可能有-1元素
        }
        return this.elem[this.top-1];//此时拿到top-1的元素  但是top依旧是top
    }
    public int size() {
        return this.top;
    }
}
import java.util.List;
import java.util.Stack;

public class TestDemo {
    public static void main(String[] args) {
       // List<Integer> list = new Stack<>();//可以这样写  但是一般情况下不这样写
        Stack<Integer> list = new Stack<>();//直接用自己类写
        MyStack<Integer> myStack = new MyStack<>();
        myStack.push(1);
        myStack.push(2);
        myStack.push(3);
        System.out.println(myStack.peek());//3
        System.out.println(myStack.pop());//3
        System.out.println(myStack.peek());//2
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值