In Python, I've begun to solve Data Structure difficulties. I'm converting from infix to postfix, however, I'm having trouble debugging a mistake in my software. The last return statement, when I do the join operation, contains NONE input. When I started debugging, I saw that something is incorrect in this section of code following the first push operation (for *). After that, whenever I call pop(), it returns NONE instead of *. Could someone please point out what is wrong here? *else: while (not s.isEmpty()) and (prec[s.peek()] >= prec[token]): #print token outlst.append(s.pop()) #print outlst s.push(token) print (s.peek())* Code (markup): Source: Scaler
The issue likely lies in your indentation. The print(s.peek()) line after s.push(token) might be outside the while loop. This means it would try to print the element on the stack (s.peek()) even if the loop didn't add anything (resulting in NONE if the stack is empty). Make sure print(s.peek()) is indented within the while loop to only print after a value is popped and added to the output list.