0

I'm currently try to implement one way linked list using pascal but i'm kinda lost , below i tried to create a list before adding a node then assign nil to the last node but then when i'm trying adding a new node i can't access it (runtime error 216) or that i get the last pointer value for some reasons i don't understand.

Program test;
type
  TNodePtr = ^TNode;
  TNode = record
          number:Integer;
          next:TNodePtr;
          end;
var
  head, tail, temp : TNodePtr;
  node : TNode;
  i : INTEGER;
procedure InitLists;
  begin
    head := nil ;
    tail := head ;
  end;
begin
  //Initializing the list
  InitLists;
  new(head);
  new(tail);

  //creating the list
  //first node
  node.number := 1 ;
  new(node.next);
  head^ := node ;
  tail^ := node ;

  //second node
  node.number := 2 ;
  new(node.next);
  tail^.next^ := node;
  tail^ := node ;

  //third node
  node.number := 3 ;
  node.next := nil;
  tail^.next^ := node;
  tail^ := node ;

  //adding a node (forth node)
  node.number := 4 ;
  new(node.next);
  new(tail^.next);
  tail^.next^ := node ;
  tail^ := node ;


  //printing the list
  temp := head ;
  for i:=1 to 4 do
    begin
      writeln(temp^.number);
      if temp^.next <> nil then
        begin
          temp := temp^.next ;
          //this is for testing
          //The final node is not being printed
          //but instead i'm getting 3 which  is the previous node value
          writeln('done')
        end
    end
end.
from_pluto
  • 21
  • 4
  • 3
    You should be able to find countless FPC examples of a singly-linked list as it's an elementary programming exercise. If you can't, use the debugger to identify the problem yourself - you'll learn far more than if someone just tells you the answer. Iac, you can invariably debug linked list problems "on paper", i.e. just by drawing out the steps you code executes. – MartynA Aug 09 '20 at 07:46

1 Answers1

2

My problem was with the node : Tnode that i used , and as suggested by someone in the comments section (He deleted his comment) I tried not using that method and allocating space and assigning nodes directly into the list without an intermediate and here how it worked for me : (Ps : Thanks a lot unknown hero)

Program test;

type
  TNodePtr = ^TNode;
  TNode = record
          number:Integer;
          next:TNodePtr;
          end;

var
  head, tail, node : TNodePtr;

procedure InitLists;
  begin
    head := nil ;
    tail := head ;
  end;



begin
  //Initializing the list
  InitLists;

  //creating the list
  //first node
  writeln('=======================');
  new(node);
  node^.number := 1 ;
  node^.next := nil ;
  head := node ;
  tail := node ;
  writeln(tail^.number);
  writeln(head^.number);
  writeln('=======================');

  //second node
  //this is where my issue is
  writeln('=======================');
  new(node);
  node^.number := 2 ;
  node^.next := nil ;
  tail^.next := node ;
  tail := node ;
  writeln(tail^.number);
  writeln(head^.number);
  //exactly here
  writeln(head^.next^.number);
  writeln('=======================');

  //third node
  writeln('=======================');
  new(node);
  node^.number := 3 ;
  node^.next := nil ;
  tail^.next := node ;
  tail := node ;
  writeln(tail^.number);
  writeln(head^.number);
  writeln(head^.next^.number);
  writeln(head^.next^.next^.number);
  writeln('=======================');

  //adding a node (forth node)
  writeln('=======================');
  new(node);
  node^.number := 4 ;
  node^.next := nil ;
  tail^.next := node ;
  tail := node ;
  writeln(tail^.number);
  writeln(head^.number);
  writeln(head^.next^.number);
  writeln(head^.next^.next^.number);
  writeln(head^.next^.next^.next^.number);
  writeln('=======================');

end.

from_pluto
  • 21
  • 4