假設pop0
是一個錯字,它應該是對pop()
的調用,它將刪除您推入堆棧並返回它的最後一個元素。讓我們按照該程序:
theStack.push(1);
// 1 is pushed to the stack. The stack now contains [1]
theStack.push(2);
// 2 is pushed to the stack. The stack now contains [2, 1]
theStack.push(3);
// 3 is pushed to the stack. The stack now contains [3, 2, 1]
theStack.push(theStack.pop());
// 3 is popped, and then pushed back in, so the stack still contains [3, 2, 1]
theStack.push(theStack.pop() + theStack.pop());
// 3 and 2 are popped, added, and pushed back in, so the stack now contains
// [5, 1]
theStack.push(6);
// 6 is pushed to the stack. The stack now contains [6, 5, 1]
theStack.push(7);
// 7 is pushed to the stack. The stack now contains [7, 6, 5, 1]
theStack.push(theStack.pop() * theStack.pop());
// 7 and 6 are popped, multiplied, and pushed back in, so the stack now contains
// [42, 5, 1]
他們迷失在您未發佈的代碼中。請閱讀[mcve]和[問]。 – Yunnosch
你確定這是'popO'而不是'pop()'嗎? (如果你不確定...)類似於':'。 –
爲什麼標記爲C? –