listpascalturbo-pascal

How to form a linked list from input in Pascal?


I have an input file in the format:

(a n), (a n-1), ... (a 0)

How can I form a list like below in Pascal

type
  tt = ^t;
  t = record
       a: Integer;
       n: Integer;
       next: tt
  end;

For example:
(5 10), (5 9), (5 8), (5 7), (5 6), (5 5), (5 4), (5 3), (5 2), (5 1), (5 0)
Should be like on the image:
enter image description here

New code (works as expected):

program ex4_19;

type
  tt = ^t;
  t = record
       a: Integer;
       n: Integer;
       next: tt
  end;

var
  ukzv, ukrs: tt;
  inp: text;
  raDone: boolean;
  i: integer;
  str: string;
begin
  assign(inp, 'f1.txt'); reset(inp);
  assign(output, 'out.txt'); rewrite(output);

  new(ukzv);
  ukrs:=ukzv; 

  read(inp, str);
  writeln(str);

  for i:=1 to length(str) do
  begin
    case str[i] of
      '(':
      begin
         raDone:=false;
         new(ukzv^.next);
         ukzv:=ukzv^.next;
         ukzv^.a:=0;
         ukzv^.n:=0;
      end;
      '0' .. '9':
      begin
         if raDone = false then
            ukzv^.a:=ukzv^.a * 10 + (ord(str[i]) - ord('0'))
         else
            ukzv^.n:=ukzv^.n * 10 + (ord(str[i]) - ord('0'));
      end;
      ' ':
      begin
         if raDone = false then
         begin
            raDone:=true;
         end;
      end;
      ')':
      begin
         ukzv^.next:=nil;
      end;
    end;
  end;

   ukzv:=ukrs;

   while ukzv^.next <> nil do
   begin
     writeln(ukzv^.next^.a, ' ', ukzv^.next^.n);
     ukzv:=ukzv^.next;
   end;
end.

I have the error "Invalid numeric format" because after the second number we have ')'. I don't know how to read number only until ')' because numbers can be different (1-1000).


Solution

  • There are different ways, one is the following. Start with reading the file into a string variable.

    '(5 10), (5 9), (5 8), (5 7), (5 6), (5 5), (5 4), (5 3), (5 2), (5 1), (5 0)'
    

    Then use a loop (for..do, repeat..until or while..do) to step through the characters one at a time. Use a case statement to decide on the actions.

    Here's the processing per character spelled out, should be straight forward to implement as a case statement.

    You need a boolean (e.g. raDone: boolean) to indicate whether new digits go to a or n in the records.

    Get next char, '(', you know it's time to link in a new record (`r` in the following).
    Get next char, '5', it's a digit and `not raDone`, so you accumulate `r.a` with it. See below!
    Get next char, ' ', it's a space and `not raDone`, you know that entry for `r.a` has ended, set `raDone` to indicate next digits belong to `r.n`.
    Get next char, '1', it's a digit and `raDone`, so you accumulate `r.n` with it.
    Get next char, '0', it's a digit and `raDone`, so you accumulate `r.n` with it.
    Get next char, ')', you know the entry for the current record is ready. 
    Get next char, comma, nothing to do, just skip it
    Get next char, ' ', space, nothing to do, just skip it
    

    To accumulate a binary value (say r.n) with a decimal digit (converted from a character):

    r.n := r.n * 10 + (ord(decimal character) - ord('0');
    

    You probably want to add error checking for erroneous content in the input string.