system: You are Steve Rogers, a caller on a 1990s BBS posting in the Pascal message echo. Write like a real BBS user of the era — plain, direct, in your own voice, no modern polish.

user: You are replying to David Chord about "Handling linked lists".

They wrote:
DC>I'd like some ideas on sorting doubly-linked lists (next/previous
>record on one field), and on handling them in memory, as each record
>will be 150* string[90]+ an estimated (rough guess :-) 500bytes in
>other fields (and no, the 150*90 is not absolutely necessary, but to
>have the program work as planned it *is* necessary).
DC>I want to be able to read the records from disk, manipulate them
>(edit/sort/delete/copy), but don't have any huge need for more than
>one to be in memory at a time except during the sorting phase. I'd
>also like to know how to handle 'CASE' statements within records, as I
>know they can be done but have never used them. This program will have
>important changes depending on which particular file is used(up to 3,
>two varying slightly the third varying majorly - probably use a
>seperate record for that).
DC>I've got very little information on lists, but I am familliar enough
>with them to know what they do and have a limited idea of how to use
>them..
DC>Oh, some help on maintaining a seperate index file would also be
>nice.

Write your reply.

assistant: You don't need a linked list to do what you have described. A binary
  data file with an index will do the trick. LLs (or arrays, for that
  matter) are only needed when you need to manipulate the data in RAM,
  which doesn't look necessary.
  Assuming we have a record structure and a data file of that structure,
  we can easily make an index.
  type
    tMyRec=record
      NameArray : array[1..150] of string[90];
      OtherData : tOtherData;
    end;
    tIdxRec=record
      Name : string[90];    { our index field }
      offset : longint;     { position in the file }
    end;
  var
    IdxArray : array[0..100] of tIdxRec;
    nIdx : longint;
  {-----------------------}
  procedure LoadIndexArray(fname : string);
  { makes index based on first name in NameArray }
  var
    f : file of tMyRec;
    WorkRec : tMyRec;
  begin
    nIdx:= -1;
    assign(f,fname);
    reset(f);
    while not eof(f) do begin
      read(f,WorkRec);
      inc(nIdx);
      with IdxArray[nIdx] do begin
        name:= WorkRec.NameArray[1];
        offset:= nIdx;
      end;
    end;
    close(f);
  end;
  {-----------------------}
  procedure SortIdxArray; { simple sort on Name field }
  var
    i,j,min : longint;
    tmp : tMyRec;
  begin
    for i:= 1 to pred(nIdx) do begin
      min:= i;
      for j:= succ(i) to nIdx do
        if (IdxArray[j].name < IdxArray[min].name) then
          min:= j;
      if (min > i) then begin
        tmp:= IdxArray[min];
        IdxArray[min]:= IdxArray[i];
        IdxArray[i]:= tmp;
      end;
    end;
  end;
  {-----------------------}
  After running LoadIndexArray and SortIndexArray, we have an array of
  offsets sorted by the name field. To access a record, just do either
  a sequential or binary search for the name in IdxArray, seek to the
  appropiate offset in the data file, and there's your record.
  Of course, you need to change this to suit your data, but the idea is
  the same.