The purpose of the linked list is to be able to resequence data and that is exactly what this assignment required. I created the moveAbove() function that accepts two arguments, the node to be moved and the node to be superceded in the list.
The linked list data structure is a list of girls' names. The list was initialized with the girls' names in alphabetical order - Amy, Betty, Carrie, Denise, Ertha and Fannie. Then moveAbove(5:2) is run. The results are shown in blue above the program code. The DSPLY statements show the name, the original seq# and the nextPointer (not really a pointer). Note the seq# is their original sequence and the data in the array is still in that order. All that moveAbove() does is reassign next values and the print function prints in the linked list sequence. I was taught that the program is more efficient since data is not moved. The function is extensible as the data element is referenced by a pointer.
DSPLY Amy 001 005 DSPLY Ertha 005 002 DSPLY Betty 002 003 DSPLY Carrie 003 004 DSPLY Denise 004 006 DSPLY Fannie 006 007
h option(*srcstmt:*nodebugio)
d printList pr
d moveAbove pr
d 3P 0 VALUE
d 3P 0 VALUE
d topOfList s 3P 0 inz(1)
d list ds qualified dim(20)
d seq 3P 0 inz
d data * inz(*NULL)
d next 3P 0 inz
d dsplyField s 14A
d amy s 6A inz('Amy ')
d betty s 6A inz('Betty ')
d carrie s 6A inz('Carrie')
d denise s 6A inz('Denise')
d ertha s 6A inz('Ertha ')
d fannie s 6A inz('Fannie')
/free list(1).seq = 1;
list(1).next = 2;
list(1).data = %addr(amy);
list(2).seq = 2;
list(2).next = 3;
list(2).data = %addr(betty);
list(3).seq = 3;
list(3).next = 4;
list(3).data = %addr(carrie);
list(4).seq = 4;
list(4).next = 5;
list(4).data = %addr(denise);
list(5).seq = 5;
list(5).next = 6;
list(5).data = %addr(ertha);
list(6).seq = 6;
list(6).next = 7;
list(6).data = %addr(fannie);
moveAbove(5:2);
printList();
*inlr = *on;
/end-free
p printList b
d pi
d x s 3P 0
d ptrName s *
d name s 6A based(ptrName)
/free
x = topOfList;
dow list(x).data <> *NULL;
ptrName = list(x).data;
dsplyField = name
+ %editw(list(x).seq:'0 ')
+ %editw(list(x).next:'0 ') ;
dsply dsplyField;
x = list(x).next;
endDo;
/end-free
p e
*
p moveAbove b
d pi
d number1 3P 0 VALUE
d number2 3P 0 VALUE
d x s 3P 0
d ptrName s *
d name s 8A based(ptrName)
/free
// move 5 above 2
for x = 1 to %elem(list) ;
if list(x).next <> 0;
// make 4 point to 6
if list(x).next = list(number1).seq; // 4.next = 5
list(x).next = list(list(x).next).next; // 4.next = 6.seq
leave;
endIf;
endIf;
endFor;
for x = 1 to %elem(list) ;
// make 1 point to 5 and 5 to 2
if list(x).next = list(number2).seq; // 1.next = 2.seq
list(x).next = list(number1).seq; // 1.next = 5.seq
list(number1).next = list(number2).seq; // 5.next = 2.seq
leave;
endIf;
endFor;
/end-free
p e