复制堆栈意味着什么?

时间:2022-02-06 16:56:10

I'm reading The C Programming Language and learned how to make a reverse Polish calculator using a stack. Here is one of the exercises that follow it:

我正在阅读C编程语言并学习如何使用堆栈制作反向波兰计算器。以下是其中一个练习:

Exercise 4-4. Add the commands to print the top elements of the stack without popping, to duplicate it, and to swap the top two elements. Add a command to clear the stack.

练习4-4。添加命令以打印堆栈的顶部元素而不弹出,复制它,以及交换前两个元素。添加命令以清除堆栈。

What do they mean by "duplicate"? Does it mean to print out the entire stack, or to push the entire stack onto itself (so that, for example, "1 2 3" would become "1 2 3 1 2 3"), or what?

“重复”是什么意思?是打算打印出整个堆栈,还是将整个堆栈推到自身上(例如,“1 2 3”会变成“1 2 3 1 2 3”),还是什么?

2 个解决方案

#1


No, not duplicate the stack, duplicate the "top" entry.

不,不重复堆栈,复制“顶部”条目。

So if your stack is:

所以如果你的堆栈是:

[1,2,3,4,5],

you get:

[1,2,3,4,5,5].

The subject "it", in this case, refers to "the top element of the stack", not "the stack".

在这种情况下,主题“它”指的是“堆栈的顶部元素”,而不是“堆栈”。

I gather "elements" was a typo.

我收集“元素”是一个错字。

#2


The Stack-oriented programming language entry in Wikipedia contains a description of stack manipulation operations:

*中面向堆栈的编程语言条目包含堆栈操作操作的描述:

Stack manipulation

Since the stack is the key means of data manipulation in a stack-oriented programming language, often these languages provide some sort of stack manipulation operators. Commonly provided are dup, to duplicate the element at the top of the stack, exch (or swap), to exchange elements at the top of the stack (the first becomes the second and the second becomes the first), roll, to cyclically permute elements in the stack or on part of the stack, pop (or drop), to discard the element at the top of the stack (push is implicit), and others. These become key in studying procedures.

由于堆栈是面向堆栈的编程语言中数据操作的关键手段,因此这些语言通常提供某种堆栈操作操作符。通常提供的是复制,复制堆栈顶部的元素,exch(或交换),交换堆栈顶部的元素(第一个成为第二个,第二个成为第一个),roll,循环置换堆栈中的元素或堆栈的一部分,pop(或drop),丢弃堆栈顶部的元素(push是隐式的),以及其他元素。这些成为学习程序的关键。

#1


No, not duplicate the stack, duplicate the "top" entry.

不,不重复堆栈,复制“顶部”条目。

So if your stack is:

所以如果你的堆栈是:

[1,2,3,4,5],

you get:

[1,2,3,4,5,5].

The subject "it", in this case, refers to "the top element of the stack", not "the stack".

在这种情况下,主题“它”指的是“堆栈的顶部元素”,而不是“堆栈”。

I gather "elements" was a typo.

我收集“元素”是一个错字。

#2


The Stack-oriented programming language entry in Wikipedia contains a description of stack manipulation operations:

*中面向堆栈的编程语言条目包含堆栈操作操作的描述:

Stack manipulation

Since the stack is the key means of data manipulation in a stack-oriented programming language, often these languages provide some sort of stack manipulation operators. Commonly provided are dup, to duplicate the element at the top of the stack, exch (or swap), to exchange elements at the top of the stack (the first becomes the second and the second becomes the first), roll, to cyclically permute elements in the stack or on part of the stack, pop (or drop), to discard the element at the top of the stack (push is implicit), and others. These become key in studying procedures.

由于堆栈是面向堆栈的编程语言中数据操作的关键手段,因此这些语言通常提供某种堆栈操作操作符。通常提供的是复制,复制堆栈顶部的元素,exch(或交换),交换堆栈顶部的元素(第一个成为第二个,第二个成为第一个),roll,循环置换堆栈中的元素或堆栈的一部分,pop(或drop),丢弃堆栈顶部的元素(push是隐式的),以及其他元素。这些成为学习程序的关键。