03-Stacks and queues

1. Learning summary


2.PTA experiment work

2.1 Topic 1: Expression Conversion

2.2 Design ideas

定义俩个字符数组exp,poxtexp,exp用来存放表达式,postexp用来存放后缀表达式
输入表达式,存入数组exp
初始化运算符栈op
将‘=’入栈
从exp读取字符
while(*exp!='/0'){
   if(*exp不是运算符){
        将数字或'.'依次存入到postexp中,并以字符“#”标志数值串结束
   else switch(Precede(op栈顶运算符){
           如果栈顶运算符优先级低,*exp入栈,继续读取下一个字符
           如果栈顶运算符为'( ',*exp为')'   '(' 退栈,继续读取下一个字符
           如果栈顶运算符优先级高,退栈运算符并将其放入postexp中
}
}
若字符串exp扫描结束,将运算符栈op中除了‘=’之前的所有运算符依次出栈并存放到postexp
控制输出格式,最后得到后缀表达式postexp

2.3 Code screenshots

2.4 PTA Submission List Instructions.

Error 1 : The operand exceeds 1 integer and there is a non-integer
1. The existence of the decimal is not considered. When judging whether it is an operator, consider the decimal point and store it together with the number in the output postexp
2. Not considered If the operand exceeds one digit, the output is found to be 23, and the output result is 2 3. A number string is added to end the operation with #, and # is used for correct output when outputting

Mistake 2 : Only 1 Number

Changed the output to ensure that the output format of a number also conforms to the meaning of the question

Error 3 : There is a positive and negative sign before the operand
(it has not been changed yet, if it is changed, it must be modified to determine whether it is an operator and other functions) The first character is + or -, indicating that it is not an operator, if it does not appear in front, in In the middle of the expression, the negative numbers are enclosed in parentheses. The numbers and positive and negative signs are stored in the array postexp. The parentheses are removed and not retained.

2.1 Topic 2: Symbol Pairing

2.2 Design ideas

定义顺序栈类型SqStack
typedef struct snode
{
    ElemType data[Maxsize];
    int top;//栈顶指针
}SqStack;
typedef SqStack *Stack;
定义字符数组str,用来存放输入的C语言源程序,循环变量i=0
定义栈s
初始化栈s
循环输入C语言语句到str
记录字符数存入变量n
如果输入'.'并且n=0,说明输入结束,跳出循环
扫描str中所有字符
   如果当前字符为左括号,将其进栈,i+1
   如果当前字符为'/',下一个字符是'*',将注释符号换成'<'入栈,i+2
   如果当前字符为')'
       如果不是空栈,将栈顶元素出栈
               如果栈顶元素是'(',则配对成功,出栈;否则说明不配对,输出NO,输出(-?
       如果是空栈,说明不配对,缺少左括号,输出NO,输出?-)
 i++,继续处理其他元素
  如果当前字符为']'
       如果不是空栈,将栈顶元素出栈
               如果栈顶元素是'[',则配对成功,出栈;否则说明不配对,输出NO,输出[-?
       如果是空栈,说明不配对,缺少左括号,输出NO,输出?-]
 i++,继续处理其他元素
如果当前字符为'}'
       如果不是空栈,将栈顶元素出栈
               如果栈顶元素是'{',则配对成功,出栈;否则说明不配对,输出NO,输出{-?
       如果是空栈,说明不配对,输出NO,缺少左括号,输出?-}
 i++,继续处理其他元素
如果当前字符为'*',下一个字符是'/'
       ++i;
       如果不是空栈,将栈顶元素出栈
               如果栈顶元素是'<',则配对成功,出栈;否则说明不配对,输出NO,输出/*-?
       如果是空栈,说明不配对,输出NO,缺少左括号,输出?-*/   
 i++,继续处理其他元素 
循环结束
如果均配对输出YES

2.3 Code screenshots






2.4 PTA Submission List Description


In this question, I am mainly stuck on the missing item in the output. The idea is very simple to pop the top element of the stack, and then see if the match does not match, and make the corresponding output. At the beginning, the comment character was treated as a character, but it didn't seem to work. After referring to the code, it was found that it could be converted into other characters and put on the stack, so that the matching judgment of a character was easy.

2.1 Topic 3: Counting Game

2.2 Design ideas

变量n表示整队人数,m表示退出位置
构造一个空队列q,将front和rear指针均设置为初始状态即0
构建初始序列
for i=1 to i=n
   q.rear=(q.rear+1)%MaxSize
    q,data[q.rear]=i
end for
队列不空循环
  如果队列不空
      for i=1 to i=m-1
       出队一个元素,再将刚出列的元素进队
      end for
  前m-1个元素被依次放入对尾,此时出队一个元素,即原先处于m位置的元素
所有元素出队后,循环结束

2.3 Code screenshots

Main function:

2.4 PTA Submission List Description

At the beginning, it imitated the book, using the loop to output the element at the position m, and then the dequeuing element and then the enqueuing element, but the output was chaotic. It was found by drawing that the sequence was disrupted by the wrong use of dequeuing and enqueuing. It should be to dequeue the element before m and re-enter the team, and then the element that is dequeued is the element at the original m position.

3. Screenshot of the PTA final ranking of this week's topic set

3.1 Stack PTA ranking

3.2 Cohort PTA ranking

3.3 My total score: 2 points

4. Read the code






Code function: convert expressions into suffix expressions
Advantages 1. Store the expression to be processed into an array, and it is easier to determine whether it is a sign or an operator for array
operations remove

5. Screenshot of code Git commit record




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324420558&siteId=291194637
Recommended