Previous Page | Next Page

SCL Lists

Using SCL Lists as Stacks and Queues

You can create lists that function as stacks (first in, last out lists) or queues (first in, first out lists).


Using a List as a Stack

To use a list as a stack, use the INSERTC, INSERTN, INSERTL, or INSERTO function to insert items into a list. The default insertion position for these functions is the beginning of the list, so you need only specify the list identifier and the data to be inserted.

To pop (or delete) an item from a stack, use the POPN, POPC, POPL, or POPO function. You can use the ITEMTYPE function to determine the type of the item at the top of the stack if your application does not know the type. If your application always puts the same data type onto your stack (for example, if the stack is a stack of character strings and you use only INSERTC to put items into the list), then you do not need to use ITEMTYPE to check the type of the item at the top of the stack before popping.

If you do not want to keep the top value, use the DELITEM or DELNITEM function to delete the top item in the stack.

To replace the top item, use the SETITEMN, SETITEMC, SETITEML, or SETITEMO function.

You should not attempt to pop or delete an item unless you are sure the list contains at least one item. You can use the LISTLEN function to return the length of the list before you use a function to pop or delete an item.


Using a List as a Queue

When you use a list as a queue, you also use the INSERTN, INSERTC, INSERTL, or INSERTO function to put items in the list. However, you use an item index of -1 to insert an item at the end of the list.

To remove an item from a queue, use the POPN, POPC, POPL, or POPO function. As with stacks, you should use the ITEMTYPE and LISTLEN functions to verify the item's type and the list's length before popping an item from the list. Here is an example:

INIT:
   listid=makelist();
   rc=insertc(listid,'1st',-1);
   rc=insertc(listid,'2nd',-1);
   rc=insertc(listid,'3rd',-1);
   rc=insertc(listid,'4th',-1);
   rc=insertc(listid,'5th',-1);
   put 'Test of first in, first out queue:';
   do i=1 to listlen(listid);
      cval=popc(listid);
      put 'Popping item' i cval=;
   end;
   rc=dellist(listid);
return;

This program produces the following output:

Test of first in, first out queue:
Popping item 1 cval=1st
Popping item 2 cval=2nd
Popping item 3 cval=3rd
Popping item 4 cval=4th
Popping item 5 cval=5th

Previous Page | Next Page | Top of Page