tags:

views:

363

answers:

2

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:

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.

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?

+5  A: 

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.

paxdiablo
A: 

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.

gimel