Stack is a linear data structure where insertion and deletion of items takes place at one end (i.e. the top), which is known as LIFO - Last In First Out.

Here is a structural definition of stack (which is quite interesting):

A stack is a recursive data structure.
It is either empty or it consists of a top and the rest of which is a stack.

There are three basic operations performed on stack:

• push: add an item to the top of the stack
• pop: remove the item on the top of the stack

A stack can be implemented through an array or a linked list. The key is to maintain the index of the top element.

Applications of stack include:

• DFS and backtracking due to its support for recursion
• evaluating expressions
• undo operations

The rest of this post will be focused on the applications.

## Min stack

155. Min Stack: design a stack that supports retrieving the minimum element in constant time.

It is an interesting problem. A straightforward solution is to use an extra stack to maintain the minimum element for each new element that is pushed into the stack, but it doubles the space complexity. How can we solve the problem without using extra space?

Instead of storing the raw number in the stack, we can store the difference between the raw number and the current minimum number. The trick is that the difference is negative if the raw number is less than the current minimum number, and this is how we decide whether to update the minimum number when an element is popped out. ## Evaluating expression

To simplify the problem, the expression only contains positive integers, +, -, *, - and ().

An expression can be represented using a binary tree, with the operators as the non-leaf nodes and the operands as the leaf nodes, and brackets are not required in the binary tree. To evaluate an expression, we can start from the leaf nodes and move upwards, exactly the same as we traverse a binary tree according to post-order.

However, maintaining a binary tree is too much in this case. Instead, we can directly convert an expression to a postfix expression (aka. Reverse Polish Notation) and then evaluate the postfix expression.

For example, the postfix expression of (2+3)\(7-4)+9-6/3 is 23+74-*9+63/-*. This can be done by using a queue and a stack - a queue for storing the postfix expression and a stack for storing the operators. It takes only one scan of the expression:

• if it is a number, add it to the queue
• if it is a ‘(‘, push it into the stack
• if it is a ‘)’, pop out the operators from the stack until ‘(‘, and add them to the queue
• if it is a ‘*‘ or ‘/‘, pop out the operators (‘*‘ or ‘/‘) from the stack, and add them to the queue, push the new operator into the stack
• if it is a ‘+’ or ‘-‘, pop out the operators from the stack until ‘(‘, and add them to the queue, push the new operator into the stack

The evaluation of the postfix expression is easy with the help of the stack - a stack for storing the numbers and a stack for storing the postfix expression (because the elements are fetched from the head), but after the conversion the postfix expression is stored in a queue. Do we need to get the elements from the queue and push them into the stack? The answer is no. Let me introduce Deque in Java Collections here. It is a data structure which can be serveed as both a queue and a stack. The evaluation takes only one scan of the postfix stack as well:

• if it is a number, push it into the number stack
• if it is an operand, pop two numbers from the number stack, do the math and then push the result into the number stack
• the number left in the number stack is the final result

Here is an implementation of evaluating an infix expression:

As you may notice, the deque for storing post fix expression is an intermediate result. We can save the effort of building such a deque; instead, it is enough for the evaluation to use two stacks - one stack for numbers and the other for operators. The process takes only one scan as well:

• if it is a number, add it to the number stack
• if it is a ‘(‘, push it into the operator stack
• if it is a ‘)’, pop out the operators from the operator stack until ‘(‘, do the math and push the result into the number stack
• if it is a ‘*‘ or ‘/‘, pop out the operators (‘*‘ or ‘/‘) from the operator stack, do the math and push the result into the number stack; push the new operator into the operator stack
• if it is a ‘+’ or ‘-‘, pop out the operators from the operator stack until ‘(‘, do the math and push the result into the number stack; push the new operator into the operator stack

## Valid parentheses

Stack is born for handling problems relating to brackets, like this simple one 20. Valid Parentheses.

Here is a tough one 32. Longest Valid Parentheses, which is worth a try.

My idea is to make use of two stacks:

• Stack1: maintain the index of ‘(‘, if there is a matching ‘)’, then pop this out ‘(‘
• Stack2: maintain the start index and the ending index of the longest valid parentheses; merge the adjacent valid parentheses, e.g. ()(), (())

I learned from the Discuss-solution-using-a-stack) that it can be solved using only one stack. The idea is to keep the index of the unmatched parathesis in the stack.