Stack

DinS          Written on 2017/4/25

1. Visually, what is a stack(栈)?

Stack is a specialized data structure that is usually implemented as a vector. It follows a special rule called last-in-first-out, or LIFO. In many occasions using stacks will simplify your codes.

2. Access

We can only get the top element from stack. Since stack is basically a vector, it needs only O(1).

3. Insert

We can only insert elements to the top of stack. This operation is called push. Adding elements to the rear of vector only need O(1).

4. Delete

We can only delete the top element in stack. This operation is called pop. Deleting the last element in vector only need O(1).

5. Some Illustrations

First we have this stack.

Second we push in an element. Now we can only access Data2.

Third we pop the top element.

Now all comes back to previous state.

6.Example – Parenthesis Matching

Parenthesis Matching

Result: