/* Linked List example by Michael Rask Christensen. */ /* This piece of code is supposed to create a linked list */ /* of structures (nodes). Before first call of the function, */ /* the topPtr must be initialized with null(). */ /* If a key is already in the list, we'll return */ /* an error code in rc. */ NewNode: proc( topPtr, inKey, inData, rc ) recursive; dcl topPtr ptr, inKey char(*), indata char(*), rc bin(15,0); dcl null builtin, length builtin; dcl 1 node based, 3 key char(length(inKey)), 3 data char(length(inData)), 3 fwdPtr ptr; if topPtr = null() then do; alloc node set( topPtr ); newPtr->node.key = inKey; newPtr->node.data = inData; newPtr->node.fwdPtr = null(); rc = 0; end; else if inKey = topPtr->node.key then rc = 4; /* Duplicate key */ else call NewNode( topPtr->node.fwdPtr, inKey, inData ); end NewNode; /* Most of the next little piece of code is actually "stolen" */ /* from the IBM manual, but I think it ought to be shown here, too. */ /* The topPtr is "the same" as in the previous example */ /* and the function is searching the linked list. */ /* If nothing is found a null pointer is returned. */ FindKey: proc( topPtr, inKey ) returns( ptr ); dcl topPtr ptr, inKey char(*); dcl 1 node based, 3 key char(length(inKey)), 3 data char(length(inData)), 3 fwdPtr ptr; dcl p ptr; dcl null builtin; do p = topPtr repeat( p->node.fwdPtr ) while( p ^= null() & p->node.key ^= inKey ); end; /*while*/ return(p); end FindKey;