Stack (栈)

Basic

- Linear data structure

- Last In First Out (LIFO) 先进后出

Here, you can (1) Put a new plate on top; (2) Remove the top plate. And, if you want to plate at the bottom, you must first remove all the plates on top. 

Operations

1. Push: Add an element to the of a stack

2. Pop: Remove an element from the top of a stack

3. IsEmpty: 

4. IsFull:

5. Peek: Get the value of the top element without removing it

public class SimpleStack {
    private int maxSize;
    private int[] stackArray;
    private int top;

    // Constructor to initialize the stack
    public SimpleStack(int size) {
        this.maxSize = size;
        this.stackArray = new int[maxSize];
        this.top = -1; // Indicates the stack is empty
    }

    // Method to add an element to the top of the stack
    public void push(int element) {
        if (!isFull()) {
            stackArray[++top] = element;
        } else {
            System.out.println("Stack is full, cannot push element.");
        }
    }

    // Method to remove and return the top element of the stack
    public int pop() {
        if (!isEmpty()) {
            return stackArray[top--];
        } else {
            System.out.println("Stack is empty, cannot pop element.");
            return -1; // Assuming -1 is not a valid stack value
        }
    }

    // Method to check if the stack is empty
    public boolean isEmpty() {
        return (top == -1);
    }

    // Method to check if the stack is full
    public boolean isFull() {
        return (top == maxSize - 1);
    }

    // Method to get the value of the top element without removing it
    public int peek() {
        if (!isEmpty()) {
            return stackArray[top];
        } else {
            System.out.println("Stack is empty.");
            return -1; // Assuming -1 is not a valid stack value
        }
    }

    public static void main(String[] args) {
        SimpleStack stack = new SimpleStack(5);

        // Push elements
        stack.push(10);
        stack.push(20);
        stack.push(30);

        // Peek at the top element
        System.out.println("Top element: " + stack.peek());

        // Pop elements
        System.out.println("Popped: " + stack.pop());
        System.out.println("Popped: " + stack.pop());

        // Check if the stack is empty
        System.out.println("Is the stack empty? " + stack.isEmpty());
    }
}

Time Complexity

待补充

Application: 

To reverse a word: Put all the letters in a stack and pop them out. Because of the LIFO order of stack, you will get the letters in reverse order.

LC 20: 有效的括号

【题目描述】

括号匹配是使用栈解决的经典问题

【思路】

由于栈结构的特殊性,适合处理对称匹配类的问题。先考虑分析字符串中的括号有几种不匹配的情况。

1. 左边的括号多余: 已经遍历了字符串,但是栈不为空,说明有相应的左括号,但没有右括号,所以返回错误。

2. 右边的括号多余:在遍历字符串的过程中栈已经为空,没有匹配的字符了,说明右括号没有找到对应的左括号,返回错误。

3. 括号没有多余,括号的类型不匹配:在遍历字符串的过程中,发现栈中没有要匹配的字符,返回错误。

public boolean isValid2(String s) {
        Stack<Character> stack = new Stack<Character>();
        for (char c : s.toCharArray()) {
            if (c == '(')
                stack.push(')');
            else if (c == '{')
                stack.push('}');
            else if (c == '[')
                stack.push(']');
            else if (stack.isEmpty() || stack.pop() != c)
                return false;
        }
        return stack.isEmpty();
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值