brut03 Эта задача очень похожа на твою предыдущую задачу... Вот очень простое решение:
Код
Type
TType =
Record
name: String[20];
sport: String[20];
score: integer;
End;
Type
PTListItem = ^TListItem;
TListItem =
Record
Val: ^TType;
prev, next: PTListItem;
End;
Function CreateItem(Var T: TType;
ptPrev, ptNext: PTListItem): PTListItem;
Var item: PTListItem;
Begin
New(item);
New(item^.Val);
item^.Val^ := T;
item^.Prev := ptPrev;
item^.Next := ptNext;
CreateItem := item
End;
Procedure DeleteItem(Var p: PTListItem);
Begin
Dispose(p^.Val);
Dispose(p); p := nil
End;
Type
TList =
Record
First, Last: PTListItem;
End;
Procedure Create(Var List: TList);
Begin
List.First := nil;
List.Last := nil;
End;
Procedure Destroy(Var List: TList);
Var
p, pt: PTListItem;
Begin
pt := List.First;
While pt <> nil Do
Begin
p := pt; pt := pt^.next;
DeleteItem(p)
End;
List.First := nil;
List.Last := nil
End;
Procedure PrintList(Var List: TList);
Var
pt: PTListItem;
Begin
pt := List.First;
While pt <> nil Do
Begin
With pt^.Val^ Do
WriteLn(name:25, sport:25, score:10);
pt := pt^.next
End
End;
Procedure InsertFirst(Var List: TList;
Var T: TType);
Var
pt: PTListItem;
Begin
pt := CreateItem(T, nil, List.First);
If List.First = nil Then List.Last := pt
Else List.First^.prev := pt;
List.First := pt
End;
Procedure AppendLast(Var List: TList;
Var T: TType);
Var
pt: PTListItem;
Begin
pt := CreateItem(T, List.Last, nil);
If List.First = nil Then List.First := pt
Else List.Last^.next := pt;
List.Last := pt
End;
Procedure InsertBefore(Var List: TList;
Var T: TType; p: PTListItem);
Var
ToAdd: PTListItem;
Begin
If (p = nil) or (p^.Prev = nil) Then
InsertFirst(List, T)
Else
Begin
ToAdd := CreateItem(T, p^.Prev, p);
p^.Prev^.Next := ToAdd;
p^.Prev := ToAdd;
End;
End;
Function FindPlace(Var List: TList;
value: Integer; Var wasFound: Boolean): PTListItem;
Var pt: PTListItem;
Begin
FindPlace := nil; WasFound := False;
pt := List.First;
While pt <> nil Do
If pt^.Val^.score > value Then
pt := pt^.next
Else
Begin
wasFound := True;
FindPlace := pt; Exit
End;
End;
Var
Rec: TType;
noMore: Boolean;
List: TList;
ToInsert: PTListItem;
Found: Boolean;
Begin
Create(List);
Repeat
Write('name > '); ReadLn(Rec.name);
noMore := Rec.Name = '';
If not noMore Then
Begin
Write('sport > '); ReadLn(Rec.sport);
Write('score > '); ReadLn(Rec.score);
If List.First = nil Then
InsertFirst(List, Rec)
Else
Begin
ToInsert := FindPlace(List, Rec.score, Found);
If Found Then
InsertBefore(List, Rec, ToInsert)
Else AppendLast(List, Rec)
End;
PrintList(List)
End;
Until noMore;
PrintList(List);
Destroy(List)
End.