I recently developed code to resemble a Stack data structure in RPG, but am curious as to how I could improve this. Currently I use a blank field ('type') that describes the type for the Stack, and use the LIKE() keyword on that type. This allows for only changing the type of the 'type' field in order to change what type the Stack supports. Is there a better way to accomplish this? Do you see any other ways that this code could be enhanced?
Code:
DCL-DS Stack Qualified; type CHAR(10); array LIKE(Stack.type) DIM(1000); size ZONED(4:0) INZ(0); END-DS; DCL-PROC Push; DCL-PI *N; obj LIKE(Stack.type) VALUE; END-PI; DCL-S i ZONED(4: 0); // Set counter to end of stack i = Stack.size; // Shift Stack down 1 DOW i > 0; Stack.array(i+1) = Stack.array(i); i = i - 1; ENDDO; // Adds new object Stack.array(1) = obj; // Increments stack size Stack.size = Stack.size + 1; END-PROC; DCL-PROC Pop; DCL-PI *N LIKE(Stack.type); END-PI; DCL-S obj LIKE(Stack.type); DCL-S i ZONED(4:0) INZ(2); // Stores top object for return obj = Stack.array(1); Clear Stack.array(1); // Shift Stack up 1 DOW i <= Stack.size; Stack.array(i-1) = Stack.array(i); i = i + 1; ENDDO; // Decrements stack size Stack.size = Stack.size - 1; // Returns top object RETURN obj; END-PROC;
Comment