I've read several articles regarding how to create dynamic arrays using RPG. While the results have been helpful, they have also been limited. I speak now of the inherent 32,767 (x'FF') length limitation of any field or file on the iSeries. Granted, if you're planning to use an array to hold more than 32,767 elements, you should probably rethink it. However, if you want to have truly dynamic "arrays" and allocate memory only for those elements that are being used, then the solution is a linked list.
If you search for the definition of a linked list (as I did), you will find something like "a linked list is a complex data structure," which meant exactly nothing to me. Simply put, a linked list is a list of items in which each item points to at least one other item in the list. Fortunately for me, the information I found had a graphical representation. (Oh good! I can understand pictures!) The structure of a singly linked list, one in which each item points only to the next item, looks like the picture shown below.
The last item in a given list will typically have a NULL pointer to signal the end of the list. There are other possibilities, such as a circular list, which I will discuss later. The singly linked list is the simplest form of linked list. A doubly linked list, one in which each item has pointers to both the next item and the previous item, is a little more complicated. The figure below represents a doubly linked list with two items in it.
In the case of a doubly linked list, both the first item and the last item will contain a NULL pointer to signal the beginning and the end of the list.
When I was writing the functions that are included with this article, one of my fellow developers (one who works primarily with C/C++) asked me, "Why would you use RPG? It's so much easier to do in C." When he said it, I agreed. But now that I have completed the functions, I am not so sure.
In RPG, we have to alter the basic form of a linked list structure a bit in order to make it function with variable-length data. You can see in the /COPY member (@LNKLSTFNC) that, instead of using a single data structure, I use three. The first is the actual "linked list" structure. This is the structure that holds the pointers to the item data, the next item, and, because it is a doubly linked list, the next item. Then, there is the data structure used to contain the actual data. This is used so that you can create a single list that contains multiple pieces of data, as in the example I have included. Finally, I used a data structure (the "control" data structure) to hold some important information about the list. This information is used by some of the functions and simplifies some of the operations.
The beauty of linked lists is that they can be accessed in the same manner as arrays, and they are limited only by the amount of memory available. Another nifty feature of a linked list is that, once it is created, you can access the list in multiple programs within the same activation group simply by passing the pointer to the control data structure.
Remember this when you are using linked lists: When you are done with a list, be sure to "delete" it by deallocating all the memory that it is using. Otherwise, you will have a memory leak. (Microsoft is always looking for people with this skill.) Also, be careful about clearing any pointers, as doing so can lead to orphaned items or lists and also cause memory leaks.
As I mentioned earlier, there are several variations on the linked list. One is the circular list, which simply has the last item pointing back to the first. With a doubly linked circular list, the first item also points back to the last. Another variation is the ordered list. It this case, you put the items into some sequence as they are added to the list. This actually isn't too difficult. You could add two fields to the control structure: one to contain the "key" field location and another to contain the key field length. Then, you need to create some new functions for adding and inserting items in the correct sequence. Once your list is in sequence, you can start trying to improve the performance of your searches. This can get complicated, but there is a lot of open-source C code available on the Internet that will help.
As you can tell, there is much more to linked lists than can easily be covered here. I discovered linked lists because of a specific problem I needed to solve. I think you will find (as I have) that once you add them to your programming arsenal, you'll find more and more uses for linked lists.
Happy linking.
Jeff Olen is a member of the AS/400 development team at Gauss Interprise, a content management software company located in Irvine, California. He can be reached by email at
TechTip: Linked Lists
Typography
- Smaller Small Medium Big Bigger
- Default Helvetica Segoe Georgia Times
- Reading Mode
LATEST COMMENTS
MC Press Online