News flash: I'm going to Australia in February, so this Web site may be switched off at any time.


Using Icon lists as stacks

Lists don't have to stay the same size. You can add or subtract elements at either end of a list. This allows you to use a list as a pushdown stack.

Here is an example. This program writes the lines of its input in reverse order, by pushing each line as it is read, and then writing each line as it is popped:

procedure main()
  stack := [];    # Make `stack' an empty list

  while push ( stack, read ( ) );   # Read and push each line
  while write ( pop ( stack ) );    # Pop and write each line
end
The first ``while'' statement calls the ``read()'' function repeatedly. Each time a line is read, it is pushed onto the stack. The second loop calls the ``pop()'' function repeatedly, and each time it succeeds, the value is written to the output.
Next: Using Icon lists as queues
See also: Using the Icon programming language
Previous: Using Icon lists as arrays

John W. Shipman, john@nmt.edu