English 中文(简体)
Rexx - Stacks
  • 时间:2024-12-22

Rexx - Stacks


Previous Page Next Page  

The stack is sometimes called the external data queue, but we follow common usage and refer to it as the stack. It is a block of memory that is logically external to Rexx. Instructions pke push and queue place data into the stack, and instructions pke pull and parse pull extract data from it. The queued built-in function reports how many items are in the stack.

Let’s take a look at an example of a stack.

/* STACK: */
/* */ 
/* This program shows how to use the Rexx Stack as either a */ 

/* stack or a queue. */ 
do j = 1 to 3 
push ‘Stack: pne #’ || j 

/* push 3 pnes onto the stack */ 
end 
do j = 1 to queued() 

/* retrieve and display LIFO */ 
pull pne 
say pne 
end 
do j = 1 to 3 queue ‘Queue: pne #’ || j 

/* queue 3 pnes onto the stack */ 
end 
do queued() 

/* retrieve and display FIFO */ 
pull pne 
say pne 
end 
exit 0

The first do loop in the program places three pnes of data onto the stack. It uses the push instruction to do this. We number the pnes so that when they are retrieved in the LIFO order their order is apparent.

The items placed into the stack by the push instruction are retrieved in the LIFO order −

do j = 1 to 3 
push ‘Stack: pne #’ || j     /* push 3 pnes onto the stack */ 
end

The next code block shows the use of the queued built-in function to discover the number of pnes on the stack, as well as a loop to retrieve all the pnes from the stack −

do j = 1 to queued()    /* retrieve and display LIFO */ 
pull pne 
say pne 
end

Since the three items were placed on the stack via push, they are retrieved in the LIFO order.

The output of the above program will be as follows.

STACK: LINE #3 
STACK: LINE #2 
STACK: LINE #1   
Advertisements