Skip to main content
deleted 423 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
  1. If there is a simpler way to do this (three procedures seems a lot for this)

    If there is a simpler way to do this (three procedures seems a lot for this)
  2. If any of the code could potentially become problematic during future usage.

    If any of the code could potentially become problematic during future usage

Here it is:

    program InserElement(input, output);
    {Has the user type in integers and forms a linked list out of them,
    then inserts an element at the end of that linked list and prints the
    linked list with the added new element}

    {$mode objfpc}{$H+}

    uses
      {$IFDEF UNIX}{$IFDEF UseCThreads}
      cthreads,
      {$ENDIF}{$ENDIF}
      Classes;

    type
      tRefList = ^tList;
      tList = record
                 info : integer;
                 next : tRefList
                end;
    var
     RefBeginning: tRefList;
     RefEnd : tRefList;
     Number : integer;



       procedure CreateList(var outRefBeginning: tRefList; var OutRefEnd: tRefList);
       { Creates a linear list through user input }
      var
       RefNew : tRefList;

      begin
       writeln('Please key in natural numbers. Key in 0 once you are done. ');
       readln(Number);
       while Number <> 0 do
       begin
         new (RefNew);
         RefNew^.info := Number;
         RefNew^.next := nil;
         if outRefBeginning = nil then
          begin
          outRefBeginning := RefNew;
          OutRefEnd := RefNew;
          end
         else
         begin
           outRefEnd^.next := RefNew;
           outRefEnd := RefNew
         end;
         readln (Number)
       end; { while-loop }
      end; {CreateList}

    procedure InsertElement(inNumber : integer; var outRefBeginning : tRefList; var outRefEnd : tRefList);
      { Inserts a new element at the end of the list. outRefBeginning points to the first
      element of that list, outRefEnd points to the last element of it. The Value of inNumber is
      assigned to the record component info of the new element}

      var
       RefNew : tRefList;

      begin
      { Create and initialise new element }
      new(RefNew);
      RefNew^.info := inNumber;
      RefNew^.next := nil;
      { Insert element at the end of the linear list }
      if outRefBeginning = nil then
         begin
         outRefBeginning := RefNew;
         outRefEnd := RefNew
         end
         else
             begin
               outRefEnd^.next := RefNew;
               outRefEnd := RefNew;
             end;
      end;{ InsertElement }

      procedure PrintList;
      { Prints all elements of the linked list }

      var
       RefNew : tRefList;

      begin
       RefNew := RefBeginning;
       while RefNew <>  nil do
       begin
         writeln (RefNew^.info);
         RefNew := RefNew^.next
       end;
      end;

    begin
      RefBeginning := nil;
      RefEnd := nil;
      CreateList(RefBeginning, RefEnd);
      InsertElement(5,RefBeginning,RefEnd);
      PrintList;
      readln;
    end.
  1. If there is a simpler way to do this (three procedures seems a lot for this)

  2. If any of the code could potentially become problematic during future usage.

Here it is:

    program InserElement(input, output);
    {Has the user type in integers and forms a linked list out of them,
    then inserts an element at the end of that linked list and prints the
    linked list with the added new element}

    {$mode objfpc}{$H+}

    uses
      {$IFDEF UNIX}{$IFDEF UseCThreads}
      cthreads,
      {$ENDIF}{$ENDIF}
      Classes;

    type
      tRefList = ^tList;
      tList = record
                 info : integer;
                 next : tRefList
                end;
    var
     RefBeginning: tRefList;
     RefEnd : tRefList;
     Number : integer;



       procedure CreateList(var outRefBeginning: tRefList; var OutRefEnd: tRefList);
       { Creates a linear list through user input }
      var
       RefNew : tRefList;

      begin
       writeln('Please key in natural numbers. Key in 0 once you are done. ');
       readln(Number);
       while Number <> 0 do
       begin
         new (RefNew);
         RefNew^.info := Number;
         RefNew^.next := nil;
         if outRefBeginning = nil then
          begin
          outRefBeginning := RefNew;
          OutRefEnd := RefNew;
          end
         else
         begin
           outRefEnd^.next := RefNew;
           outRefEnd := RefNew
         end;
         readln (Number)
       end; { while-loop }
      end; {CreateList}

    procedure InsertElement(inNumber : integer; var outRefBeginning : tRefList; var outRefEnd : tRefList);
      { Inserts a new element at the end of the list. outRefBeginning points to the first
      element of that list, outRefEnd points to the last element of it. The Value of inNumber is
      assigned to the record component info of the new element}

      var
       RefNew : tRefList;

      begin
      { Create and initialise new element }
      new(RefNew);
      RefNew^.info := inNumber;
      RefNew^.next := nil;
      { Insert element at the end of the linear list }
      if outRefBeginning = nil then
         begin
         outRefBeginning := RefNew;
         outRefEnd := RefNew
         end
         else
             begin
               outRefEnd^.next := RefNew;
               outRefEnd := RefNew;
             end;
      end;{ InsertElement }

      procedure PrintList;
      { Prints all elements of the linked list }

      var
       RefNew : tRefList;

      begin
       RefNew := RefBeginning;
       while RefNew <>  nil do
       begin
         writeln (RefNew^.info);
         RefNew := RefNew^.next
       end;
      end;

    begin
      RefBeginning := nil;
      RefEnd := nil;
      CreateList(RefBeginning, RefEnd);
      InsertElement(5,RefBeginning,RefEnd);
      PrintList;
      readln;
    end.
  1. If there is a simpler way to do this (three procedures seems a lot for this)
  2. If any of the code could potentially become problematic during future usage
program InserElement(input, output);
{Has the user type in integers and forms a linked list out of them,
then inserts an element at the end of that linked list and prints the
linked list with the added new element}

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes;

type
  tRefList = ^tList;
  tList = record
             info : integer;
             next : tRefList
            end;
var
 RefBeginning: tRefList;
 RefEnd : tRefList;
 Number : integer;



   procedure CreateList(var outRefBeginning: tRefList; var OutRefEnd: tRefList);
   { Creates a linear list through user input }
  var
   RefNew : tRefList;

  begin
   writeln('Please key in natural numbers. Key in 0 once you are done. ');
   readln(Number);
   while Number <> 0 do
   begin
     new (RefNew);
     RefNew^.info := Number;
     RefNew^.next := nil;
     if outRefBeginning = nil then
      begin
      outRefBeginning := RefNew;
      OutRefEnd := RefNew;
      end
     else
     begin
       outRefEnd^.next := RefNew;
       outRefEnd := RefNew
     end;
     readln (Number)
   end; { while-loop }
  end; {CreateList}

procedure InsertElement(inNumber : integer; var outRefBeginning : tRefList; var outRefEnd : tRefList);
  { Inserts a new element at the end of the list. outRefBeginning points to the first
  element of that list, outRefEnd points to the last element of it. The Value of inNumber is
  assigned to the record component info of the new element}

  var
   RefNew : tRefList;

  begin
  { Create and initialise new element }
  new(RefNew);
  RefNew^.info := inNumber;
  RefNew^.next := nil;
  { Insert element at the end of the linear list }
  if outRefBeginning = nil then
     begin
     outRefBeginning := RefNew;
     outRefEnd := RefNew
     end
     else
         begin
           outRefEnd^.next := RefNew;
           outRefEnd := RefNew;
         end;
  end;{ InsertElement }

  procedure PrintList;
  { Prints all elements of the linked list }

  var
   RefNew : tRefList;

  begin
   RefNew := RefBeginning;
   while RefNew <>  nil do
   begin
     writeln (RefNew^.info);
     RefNew := RefNew^.next
   end;
  end;

begin
  RefBeginning := nil;
  RefEnd := nil;
  CreateList(RefBeginning, RefEnd);
  InsertElement(5,RefBeginning,RefEnd);
  PrintList;
  readln;
end.
Source Link
Lucky
  • 185
  • 1
  • 4

Inserting an element into a linked list

This program adds an element to the end of a linked list of integers, which is previously keyed in by the user. This works fine on my computer, but I am wondering:

  1. If there is a simpler way to do this (three procedures seems a lot for this)

  2. If any of the code could potentially become problematic during future usage.

Here it is:

    program InserElement(input, output);
    {Has the user type in integers and forms a linked list out of them,
    then inserts an element at the end of that linked list and prints the
    linked list with the added new element}

    {$mode objfpc}{$H+}

    uses
      {$IFDEF UNIX}{$IFDEF UseCThreads}
      cthreads,
      {$ENDIF}{$ENDIF}
      Classes;

    type
      tRefList = ^tList;
      tList = record
                 info : integer;
                 next : tRefList
                end;
    var
     RefBeginning: tRefList;
     RefEnd : tRefList;
     Number : integer;



       procedure CreateList(var outRefBeginning: tRefList; var OutRefEnd: tRefList);
       { Creates a linear list through user input }
      var
       RefNew : tRefList;

      begin
       writeln('Please key in natural numbers. Key in 0 once you are done. ');
       readln(Number);
       while Number <> 0 do
       begin
         new (RefNew);
         RefNew^.info := Number;
         RefNew^.next := nil;
         if outRefBeginning = nil then
          begin
          outRefBeginning := RefNew;
          OutRefEnd := RefNew;
          end
         else
         begin
           outRefEnd^.next := RefNew;
           outRefEnd := RefNew
         end;
         readln (Number)
       end; { while-loop }
      end; {CreateList}

    procedure InsertElement(inNumber : integer; var outRefBeginning : tRefList; var outRefEnd : tRefList);
      { Inserts a new element at the end of the list. outRefBeginning points to the first
      element of that list, outRefEnd points to the last element of it. The Value of inNumber is
      assigned to the record component info of the new element}

      var
       RefNew : tRefList;

      begin
      { Create and initialise new element }
      new(RefNew);
      RefNew^.info := inNumber;
      RefNew^.next := nil;
      { Insert element at the end of the linear list }
      if outRefBeginning = nil then
         begin
         outRefBeginning := RefNew;
         outRefEnd := RefNew
         end
         else
             begin
               outRefEnd^.next := RefNew;
               outRefEnd := RefNew;
             end;
      end;{ InsertElement }

      procedure PrintList;
      { Prints all elements of the linked list }

      var
       RefNew : tRefList;

      begin
       RefNew := RefBeginning;
       while RefNew <>  nil do
       begin
         writeln (RefNew^.info);
         RefNew := RefNew^.next
       end;
      end;

    begin
      RefBeginning := nil;
      RefEnd := nil;
      CreateList(RefBeginning, RefEnd);
      InsertElement(5,RefBeginning,RefEnd);
      PrintList;
      readln;
    end.