Java学习day14

本文详细介绍了栈这一数据结构,包括其基本操作如创建、入栈和出栈,并提供了一个具体的Java CharStack实现示例。通过示例展示了如何使用栈进行元素的添加和删除,演示了栈的后进先出(LIFO)特性。此外,文章还讨论了栈在括号匹配和计算后缀表达式等实际问题中的应用,并指出灵活运用栈的重要性。
摘要由CSDN通过智能技术生成

一.栈

二.代码展示

三.总结

一.栈

栈是一种特殊的线性表,,其插入和删除操作只允许在线性表的一端进行,一般而言,把允许操作的一端称为栈顶(Top),不可操作的一端称为栈底(Bottom),同时把插入元素的操作称为入栈(Push),删除元素的操作称为出栈(Pop)。若栈中没有任何元素,则称为空栈。
如何所示,对元素的处理只能在栈顶进行处理,因此栈能实现后进先出(Last In First Out,简称LIFO)或先进后出(First In Last Out,简称FILO)的原则。
在这里插入图片描述

栈的操作

栈的创建

   /*
    constant
     */
    public static final int MAX_DEPTH = 10;
    /*
    actual depth
     */
    int depth;
    /*
    data stored in the stack
     */
    char[] data;

    /*
    construct an empty stack
     */
    public CharStack() {
        depth = 0;
        data = new char[MAX_DEPTH];
    }//of constructor

    /*
    override the method "toString"

     */
    public String toString() {
        String resultString = "";
        for (int i = 0; i < depth; i++) {//Attention depth and data.length are different.
            resultString += data[i];

        }//of for i
        return resultString;
    }//of toString

其实这里的初始化和线性表,链表的类似,只是栈在进行pop和push操作的时候略有不同。
入栈操作(添加元素)

 /*
    push an element to the stack
    @param paramChar the given element
    @return success or not.
     */
    public boolean push(char paramChar) {
        if (depth == MAX_DEPTH) {
            System.out.println("The stack is full");
            return false;
        }//of if
        data[depth] = paramChar;
        depth++;
        return true;
    }//of method push

我们只需要给末尾元素之后的空位赋值并且让栈深度+1即可(对于从0开始记录的任何表,表长都默认值最后一个元素的下一个位置。depth默认指向的位置刚好就是尾元素后面的空位,刚好可以直接插入,于是可以使用: data[depth] = paramChar; depth++;
出栈操作(删除元素)

 /*
    pop an element from the stack
    @return the poped char.
     */
    public char pop() {
        if (depth == 0) {
            System.out.println("The stack is empty");
            return '\0';
        } //of  if
        char popedChar = data[depth - 1];
        depth--;
        return popedChar;

    }//of pop

首先还是需要判断栈里是否还有元素
然后我们使用popedChar先接收栈顶数据,之后再进行栈指针的下移操作。

二.代码展示

package dataStructure.list;


/**
 * @author Donghao Xu
 */
public class CharStack {

    /*
    constant
     */
    public static final int MAX_DEPTH = 10;
    /*
    actual depth
     */
    int depth;
    /*
    data stored in the stack
     */
    char[] data;

    /*
    construct an empty stack
     */
    public CharStack() {
        depth = 0;
        data = new char[MAX_DEPTH];
    }//of constructor

    /*
    override the method "toString"

     */
    public String toString() {
        String resultString = "";
        for (int i = 0; i < depth; i++) {//Attention depth and data.length are different.
            resultString += data[i];

        }//of for i
        return resultString;
    }//of toString

    /*
    push an element to the stack
    @param paramChar the given element
    @return success or not.
     */
    public boolean push(char paramChar) {
        if (depth == MAX_DEPTH) {
            System.out.println("The stack is full");
            return false;
        }//of if
        data[depth] = paramChar;
        depth++;
        return true;
    }//of method push

    /*
    pop an element from the stack
    @return the poped char.
     */
    public char pop() {
        if (depth == 0) {
            System.out.println("The stack is empty");
            return '\0';
        } //of  if
        char popedChar = data[depth - 1];
        depth--;
        return popedChar;

    }//of pop


    /*
    the entrance of the program
    @param args not used now.
     */
    public static void main(String[] args) {
        CharStack tempStack = new CharStack();
        for (char ch = 'a'; ch < 'm'; ch++) {
            tempStack.push(ch);
            System.out.println("The current stack is:" + tempStack);

        }//of for ch
        char tempChar;
        for (int i = 0; i < 12; i++) {
            tempChar = tempStack.pop();
            System.out.println("poped char is " + tempChar);
            System.out.println("The current stack is:" + tempStack);

        }//of for i
    }//of main
}//of class CharStack

运行结果

The current stack is:a
The current stack is:ab
The current stack is:abc
The current stack is:abcd
The current stack is:abcde
The current stack is:abcdef
The current stack is:abcdefg
The current stack is:abcdefgh
The current stack is:abcdefghi
The current stack is:abcdefghij
The stack is full
The current stack is:abcdefghij
The stack is full
The current stack is:abcdefghij
poped char is j
The current stack is:abcdefghi
poped char is i
The current stack is:abcdefgh
poped char is h
The current stack is:abcdefg
poped char is g
The current stack is:abcdef
poped char is f
The current stack is:abcde
poped char is e
The current stack is:abcd
poped char is d
The current stack is:abc
poped char is c
The current stack is:ab
poped char is b
The current stack is:a
poped char is a
The current stack is:
The stack is empty
poped char is  
The current stack is:
The stack is empty
poped char is  
The current stack is:

三.总结

栈的操作其实并不复杂,可以看成是栈顶指针的使用。使用栈这种数据结构,可以解决很多实际问题
例如括号匹配,计算后缀表达式等等,因此Java类库也提供了Stack类来完成操作。现在看来,当初学的模模糊糊的数据结构栈也能解决许多实际的问题,但是如何去灵活的使用栈,还是一件并不容易的事情。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值