Program LinkedList (input, output); Type
Var
ListData = Record LastName : String15; FirstName : String15; StreetAddress : String25; Zip : Integer; AmountOwed : Real; End; List = ^ListNode; ListNode = Record data : ListData; Link : List; End; CustList :List;
Procedure ListCreate(Var L : List); Begin L := Nil; End; Procedure ListAdd (Var L : List; Item : ListData; function precedes (Item1, Item2 : ListData) : Boolean: Var Success : Boolean); Var P. Prev : List; Procedure FindSlot (L : List; Item : ListData; function procedes (Item1, Item2: ListData): Boolean; Var Prev : List); Var Q : List; Found : Boolean;
Begin { Find Slot } Prev := Nil; Q := L; Found := False; While (Q <> Nil) and not Found Do If Procedes (Item, Q^.data) Then Found := True Else Begin Prev := Q; Q := Q^.link; end End; Begin
{ of ListAdd } New(P); P^.data := Item; FindSlot(L, Item, precedes, Prev); If Prev = Nil Then { Insert at the front } Begin P^.link := L; L:= P; end Else Begin P^.link := Prev^.link; Prev^.link := P End
end;
Procedure ListUpDate (Var L : List; Target: ListData; Function Match (Target, anyData: ListData): Boolean; NewValue : ListData; Var success: Boolean); Var P : List; Begin P := L; Success := False; While (P <> Nil) and Not Success Do if Match (Target, P^.data) Then Begin Success := True; P^.data := NewValue; End Else P := P^.link; End; Procedure ListRetrieve ( L : List; Target: ListData; Function Match (Target, AnyData: ListData): Boolean; Var Item: ListData; Var Success: Boolean); Var P : List; Begin P := L; Success := False; While (P <> Nil) and NOT Success Do If match (Target, P^.data) then Begin Item := P^.data; Success := True; End Else P := P^.link; End;
{ Matching function for ListUpdate and ListRetrieve, must match last and first names } Function Beta (Item1, Item2 : ListData) :Boolean; Begin
if (Item1.LastName =Item2.LastName) AND (Item1.FirstName = Item2.FirstName) Then Beta := True Else Beta := False;
end;
{ Precedes function used for ListAdd } Function Alpha (item1, item2 : ListData): Boolean; Begin if item1.LastName < Item2.LastName Then Alpha := True Else if item1.lastName > item2.Lastname Then Alpha := False Else { same last name } If item1.FirstName <= Item2.FirstName then Alpha := True else Alpha := False; End; { Using Alpha to add a new node } Var
Item : ListData; CustList : List; S : Boolean; Item.LastName := Miller'; Item.FirstName := Jean'; Item.StreeAddress := 496 E. Wabash'; Item. Zip := 62843; Item.AmountOwed := 0.0; ListAdd (CustList, Item, Alpha, S); If not S Then Writeln( Error occured');