The exercises are mainly selected from the book "Data structure and algorithm JavaScript description" published by Orelly.
Reference code visible: Https://github.com/dashnowords/blogs/tree/master/Structure/Stack
Basic Exercises
- Implement a class based on the characteristics of the stack
Stack
, and use it in subsequent topics when you need a stack.
- Write a function
unitTrans(num, unit)
, num
for a 10 decimal number, unit
to convert the number of binary, to seek the results of the conversion.
- Write a function
recursion(num)
num
for a 10 binary number that requires num!
the output result.
- Writes a function
palindrome(str)
that str
is a string, if it is a palindrome string, then returns true
, otherwise returns false
.
After-school exercises (fourth in the book)
- In an arithmetic expression, there
{}
are ()
[]
three kinds of parentheses, write a function, accept an arithmetic expression as an argument, return if the parentheses match exactly true
, or return the position where the parentheses are missing.
- The suffix expression for an expression is opt1 opt2 operator, which writes a function that accepts an arithmetic expression as an argument (an infix expression in the form of an arithmetic expression that is usually used) and converts it to a suffix expression (which can be persisted regardless of the priority of the operation).
- In the box from top to bottom there are an indefinite number of "red", "white", "xxx" three kinds of sweets, write a program, you can use one or more stacks, in order to ensure the original candy sequence unchanged, remove all the "xxx" candy.
Exercise Ideas
- The expression is parsed by character, and the opening parenthesis is pressed into the stack, and the closing parenthesis pops an element from the top of the stack to see if they match, and if the match continues, the position is returned if the mismatch is not matched. It is important to note that the final state of the stack needs to be empty if all brackets are fitted.
- Reverse parse the original expression, will
操作数
and 操作符
respectively press into two stacks, and then first popped from the operand stack first element, in turn from the operand stack and operation Ching popup elements until the stack is empty. If the front-to-back parsing, the top of the stack is the last element, the stack is taken into account in order.
- Only with an extra stack, the "Red" "white" candy pressed into the new stack, the xxx candy removed, when the candy box is empty, and then the new candy stack one by one pop-up elements back to the candy box stack.
Wild front-end data structure exercises (1)--Stack