Next / Previous / Index / ITC Help System / Publications / Site map / NM Tech homepage

Using Icon lists as stacks

Tech Computer Center logo

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 list type in Icon
Previous: Using Icon lists as arrays
Site map
Index: Keyword index to help pages
Help: New Mexico Tech Information Technology and Communications: Help System
ITC Publications
To report a problem: File a ticket
Send mail to the User consultant on duty or call them at 575-835-5437
Home: About New Mexico Tech

John Shipman, john@nmt.edu
Last updated: 1996/01/06 21:12:57 UT
URL: http://www.nmt.edu/tcc/help/lang/icon/stacklist.html
QR two-dimensional bar code