数据结构(三):栈与Java Stack类

本文目录

1 Stack简述

2 Stack类中的方法

2.1 构造函数

2.2 入栈

2.3 出栈

2.4 获取栈顶元素

2.5 栈是否为空

2.6 在栈中搜索一个元素

2.7 其他方法

3 栈的应用

3.1 直接应用

3.2 间接应用


1 Stack简述

栈是一个有序线性表,只能在表的一端(栈顶,top)执行插入和删除操作。最后插入的元素将第一个被删除。所有站也称为后进先出(LIFO)或先进后出(FILO)线性表。

两个改变栈操作都有专用名称,一个称为入栈(push),表示在栈中插入一个元素;另一个被称为出栈(pop),表示从栈中删除一个元素。试图对一个空栈执行出栈操作称为下溢(underflow);试图对一个满栈执行入栈操作称为溢出(overflow)。通常,溢出和下溢均认为是异常。

2 Stack类中的方法

Stack类继承自Vector类,源码如下所示:

class Stack<E> extends Vector<E> {
}

在Java中,Vector类是动态数组类,这意味中在Stack底层是通过动态数组来实现的。

Stack类中的方法(不包括继承的方法)有如下: 

2.1 构造函数

Stack类只有一个默认的构造函数:

public Stack() {
    }

2.2 入栈

Stack类的入栈操作函数如下:该方法将一个元素添加到栈顶中。

/**
     * Pushes an item onto the top of this stack. This has exactly
     * the same effect as:
     * <blockquote><pre>
     * addElement(item)</pre></blockquote>
     *
     * @param   item   the item to be pushed onto this stack.
     * @return  the {@code item} argument.
     * @see     java.util.Vector#addElement
     */
    public E push(E item) {
        addElement(item);

        return item;
    }

 

2.3 出栈

Java 中Stack类的出栈元素如下:该方法将栈顶元素出栈,即删掉栈顶元素。

/**
     * Removes the object at the top of this stack and returns that
     * object as the value of this function.
     *
     * @return  The object at the top of this stack (the last item
     *          of the {@code Vector} object).
     * @throws  EmptyStackException  if this stack is empty.
     */
    public synchronized E pop() {
        E       obj;
        int     len = size();

        obj = peek();
        removeElementAt(len - 1);

        return obj;
    }

可以发现,该出栈函数pop()实际是先找到该Stack的栈顶元素(peek()方法),然后将其从栈顶删除,返回被删除的栈顶元素。 

2.4 获取栈顶元素

Stack类获取栈顶元素的源码如下:

/**
     * Looks at the object at the top of this stack without removing it
     * from the stack.
     *
     * @return  the object at the top of this stack (the last item
     *          of the {@code Vector} object).
     * @throws  EmptyStackException  if this stack is empty.
     */
    public synchronized E peek() {
        int     len = size();

        if (len == 0)
            throw new EmptyStackException();
        return elementAt(len - 1);
    }

该方法中,先获取栈中元素的大小,如果当前栈中元素不为0,返回栈顶元素(不删除)。 

2.5 栈是否为空

判断栈是否为空的源码如下:

/**
     * Tests if this stack is empty.
     *
     * @return  {@code true} if and only if this stack contains
     *          no items; {@code false} otherwise.
     */
    public boolean empty() {
        return size() == 0;
    }

该方法直接返回Stack中元素大小的判断。

2.6 在栈中搜索一个元素

Stack类中,在栈中搜索一个元素的方法源码如下:

    /**
     * Returns the 1-based position where an object is on this stack.
     * If the object {@code o} occurs as an item in this stack, this
     * method returns the distance from the top of the stack of the
     * occurrence nearest the top of the stack; the topmost item on the
     * stack is considered to be at distance {@code 1}. The {@code equals}
     * method is used to compare {@code o} to the
     * items in this stack.
     *
     * @param   o   the desired object.
     * @return  the 1-based position from the top of the stack where
     *          the object is located; the return value {@code -1}
     *          indicates that the object is not on the stack.
     */
    public synchronized int search(Object o) {
        int i = lastIndexOf(o);

        if (i >= 0) {
            return size() - i;
        }
        return -1;
    }

该方法在栈中自栈顶开始向栈底查找一个元素,返回首先找到的该元素的位置(从栈顶为1开始)。如果栈中不存在该元素,返回-1。

2.7 其他方法

需要注意的是:Stack类继承自Vector类,因此也拥有了Vector类的方法。继承的方法详见Vector类。

3 栈的应用

3.1 直接应用

Stack在实际场景中的应用主要如下:

(1)符号匹配;

(2)中缀表达式转换为后缀表达式;

(3)计算后缀表达式;

(4)实现函数调用(递归);

(5)求范围误差(极差);

(6)网页浏览器中已访问页面的历史记录(后退(back)按钮);

(7)文本编辑器中的撤销序列;

(8)HTML和XML文件中的标签(tag)匹配。

3.2 间接应用

(1)作为一个算法的辅助数据结构;

(2)其他数据结构的组件。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值