Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Input information from lines (external file) (unkown number of lines)

SlladSllad Member Posts: 19
I want to input information (integers) (one per line). But i dunno the number of lines. Lines could be 1, 2, 10000 etc. and i need all integers. How do i do that (also i want to know in which line is each integer) Can u help me plz?

Comments

  • zibadianzibadian Member Posts: 6,349
    : I want to input information (integers) (one per line). But i dunno the number of lines. Lines could be 1, 2, 10000 etc. and i need all integers. How do i do that (also i want to know in which line is each integer) Can u help me plz?
    :
    For this you need to create a linked-list, and traverse that. Here's the basic linked list of integer:
    [code]
    type
    PInteger = ^TInteger;
    TInteger = record;
    Value: integer;
    Next: PInteger;
    end;

    function Add(NewValue: integer; List: PInteger): PInteger;
    { Adds NewValue to list, and creates the list if necessary
    Returns resulting list }
    var
    Current: PInteger;
    begin
    if List = nil then
    begin
    Current := New(PInteger);
    Current^.Value := NewValue;
    Add := Current;
    end else begin
    Add := List;
    Current := List;
    while Current^.Next <> nil do
    Current := Current^.Next;
    Current^.Next := New(PInteger);
    Current^.Next^.Value := NewValue;
    end;
    end;

    function GetValue(Index: integer; List: PInteger): integer;
    { Returns indexth value in the list
    Note: List is 0-based, returns 0 if index is out of range }
    var
    Current: PInteger;
    begin
    Current := List;
    while Current <> nil do
    begin
    if Index = 0 then
    begin
    GetValue := Current^.Value;
    Exit;
    end;
    Current := Current^.Next;
    end;
    GetValue := 0;
    end;

    function GetLength(List: PInteger): integer;
    { Returns length of the list }
    var
    Current: PInteger;
    l: integer;
    begin
    l := 0;
    Current := List;
    while Current <> nil do
    begin
    inc(l);
    Current := Current^.Next;
    end;
    GetLength := l;
    end;
    [/code]
  • SlladSllad Member Posts: 19
    Umm, sry but i can't rly use that. Where shall i write my programm? i mean i'm novice and trying to learn. Where shall i write my code in this code u gave me, and what vars should i use. I mean please make something like a tutorial about that. Super thnx.
Sign In or Register to comment.