{ Renata Lara } { CSCI 3333.01 } { Feb. 5, 1998 } { This program implements List ADT in an array based structure. } PROGRAM ListADT; USES help1380; { Links to file help1380 which contains WinCrt. } CONST max = 10; TYPE comparisonType = ( less, greater, equal); ListElementType = integer; indexType = 0..max; list_type = RECORD data : array [1..max] of integer; length : integer; currentItem : indexType; END; VAR list : list_type; newElement : ListElementType; ans : char; { loop control } PROCEDURE create_list(var L:list_type); BEGIN L.length:=0; writeln; writeln('List created'); writeln; writeln(logfile); { LOGFILE } writeln(logfile, 'List created'); { LOGFILE } writeln(logfile); { LOGFILE } END; PROCEDURE destroy_list(var L:list_type); BEGIN L.length:=0; writeln; writeln('List Destroyed'); writeln; writeln(logfile); { LOGFILE } writeln(logfile, 'List Destroyed'); { LOGFILE } writeln(logfile); { LOGFILE } END; FUNCTION length(L:list_type):integer; BEGIN length := L.length; END; FUNCTION empty(L:list_type) : boolean; BEGIN empty :=(L.length = 0); END; FUNCTION full(L:list_type) : boolean; BEGIN full := (L.length = max); END; FUNCTION compare(num1, num2 : ListElementType): ComparisonType; BEGIN if num1 < num2 then compare := less else if num1 > num2 then compare := greater else compare := equal; END; PROCEDURE insert (var L:list_type; newElem : ListElementType); VAR location : integer; index : integer; MoreToSearch : boolean; BEGIN if L.length <> max then begin location := 1; MoreToSearch := (location <= L.length); {* Find insertion point *} while MoreToSearch do case compare (newElem, L.data[location]) of greater : begin location := location + 1; MoreToSearch := location <= L.length; end; less : MoreToSearch := false end; {* case compare *} {* create space for new element *} for index := L.length downto location do L.data[index+1] := L.data[index]; {* put new element in the list *} L.data[location] := newElem; L.length := L.length + 1; end else begin writeln('List is full!!!'); writeln(logfile, 'List is full!!!'); { LOGFILE } end; END; PROCEDURE print(L:list_type); VAR j : integer; BEGIN write('Printed List: '); write(logfile, 'Printed List: '); { LOGFILE } for J:= 1 to L.length do begin write(L.data[j], ' '); write(logfile, L.data[j], ' '); end; writeln; writeln(logfile); { LOGFILE } END; PROCEDURE rev_print(L:list_type; num:integer); BEGIN if not(num = L.length) then rev_print(L, (num+1)); write(L.data[num],' '); write(logfile, L.data[num],' '); { LOGFILE } END; BEGIN {***} setup_logfile; { initialize the logfile } writeln('Initializing program...'); create_list(List); repeat write('Please enter the new element as an integer: '); readln(NewElement); write(logfile, 'Please enter the new element as an integer: '); writeln(logfile, NewElement); { LOGFILE } insert(List, NewElement); writeln; writeln(logfile); { LOGFILE } print(list); writeln; write('Would you like to enter another element? (Y/N)'); readln(ans); writeln; writeln(logfile); { LOGFILE } write(logfile, 'Would you like to enter another element? (Y/N)'); writeln(logfile, ans); { LOGFILE } writeln(logfile); { LOGFILE } writeln; writeln(logfile); { LOGFILE } until (ans = 'N') or (ans = 'n'); write('Reverse List: '); write(logfile, 'Reverse List: '); { LOGFILE } rev_print(List, 1); writeln; writeln(logfile); { LOGFILE } destroy_list(List); {***} display_logfile; {***} holdscreen; {***} close_logfile { closes logfile } END.
Make your own free website on Tripod.com