{ Renata Lara } { CSCI 3333.01 } { Feb. 5, 1998 } { This program implements List ADT in a linked structure. } PROGRAM LlistADT; USES help1380; { Links to file help1380 which contains WinCrt. } TYPE comparisonType = ( less, greater, equal); ListElementType = integer; NodePtrType = ^NodePtr; NodePtr = RECORD info : ListElementType; next : NodePtrType; END; list_type = RECORD length : integer; data : NodePtrType; currentItem : NodePtrType; END; VAR list : list_type; ElementPtrType : NodePtrType; newElement : ListElementType; ans : char; { loop control } PROCEDURE create_list(var L:list_type); BEGIN L.data := NIL; L.length := 0; writeln; writeln('List created'); writeln; writeln(logfile); { LOGFILE } writeln(logfile, 'List created'); { LOGFILE } writeln(logfile); END; PROCEDURE destroy_list(var L:list_type); VAR temp : list_Type; BEGIN L.length:=0; while L.data <> NIL do begin temp.data := L.data; L.data := L.data^.next; dispose(temp.data); end; writeln; writeln('List Destroyed'); writeln; writeln(logfile); { LOGFILE } writeln(logfile, 'List Destroyed'); { LOGFILE } writeln(logfile); END; FUNCTION length (L:list_type) : integer; BEGIN length := L.length END; FUNCTION empty(L:list_type) : boolean; BEGIN if L.data = NIL then empty := true else empty := false END; FUNCTION full(L:list_type) : boolean; BEGIN full := false 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 NewNode : NodePtrType; PredLoc : NodePtrType; location: NodePtrType; MoreToSearch : boolean; BEGIN location := L.data; PredLoc := NIL; MoreToSearch := location <> NIL; L.length := L.length + 1; {* Find insertion point *} while MoreToSearch do case compare (newElem, location^.info) of greater : begin PredLoc := location; location := location^.next; MoreToSearch := location <> NIL; end; less : MoreToSearch := false; end;{ case compare } New(NewNode); NewNode^.Info := newElem; {* insert new node into the list *} if PredLoc = NIL then begin NewNode^.Next := L.data; L.data := NewNode end{if then} else {* inserting at the middle or at the end *} begin NewNode^.Next := PredLoc^.Next; PredLoc^.Next := NewNode end{else} END; PROCEDURE print (L: list_type); VAR temp: list_type; BEGIN write('Printed List: '); write(logfile, 'Printed List: '); { LOGFILE } while L.data <> NIL do begin temp.data := L.data; write(Temp.data^.info, ' '); write(logfile, Temp.data^.info, ' '); L.data := L.data^.next; end; writeln; writeln(logfile); END; PROCEDURE rev_print(L:list_type); VAR temp : list_type; BEGIN if L.data <> NIL then begin temp.data := L.data; L.data := L.data^.next; rev_print(L); write(Temp.data^.info, ' '); write(logfile, Temp.data^.info, ' '); end; writeln; writeln(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); writeln; writeln(logfile); { LOGFILE } destroy_list(List); {***} display_logfile; {***} holdscreen; {***} close_logfile { closes logfile } END.
Make your own free website on Tripod.com