LinkedList Problem lazarus / 61-Beginners ---------------------------------------------------------------------- fury2080 · 2016-06-27 13:15 Hello every one i'm trying to create a dynamic list and all is good only with small problem when i try to print the list it rise the following error (https://s31.postimg.org/n060jzmhj/6_27_2016_3_03_11_PM.jpg) (https://s31.postimg.org/xvh4mw6fb/6_27_2016_3_03_41_PM.jpg) Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program project1; {$mode objfpc}{$H+} uses  {$IFDEF UNIX}{$IFDEF UseCThreads}  cthreads,  {$ENDIF}{$ENDIF}  Classes, SysUtils, CustApp  { you can add units after this }; type  Stu = Record    Name : String[100];    Age : Integer;  end;  PTR = ^TStu;  TStu = Record    StudRec : Stu;    Next : PTR;  end; Var  HList, TList : PTR;  Student : Stu;  n, x, i : Integer; Procedure ListInitialize;Begin  HList := nil;  TList := HList;End; Procedure AddData(StudRec : Stu);Var  Node : TStu;Begin   Node.StudRec := StudRec;   New(Node.Next);   If (HList = Nil) Then    Begin        New(HList);        New(TList);        HList^ := Node;    end   Else       TList^.Next^ := Node;   TList^ := Node;End; Procedure InsertByIndex( Index:Integer; StudRec: Stu);Var  i :Integer;  TempPTR : PTR;  TempNode, Node : TStu;  Done : Boolean;Begin   Done := False;   If (HList = Nil) Then      Exit;   i := 0;   Node.StudRec := StudRec;   New(Node.Next);   TempPTR := HList;   If (Index = 0)Then      Begin        TempNode := HList^;      HList^ := Node;        Node.Next^ := TempNode;        Done := True;      end;   If (not Done) Then      While (i < Index-1) Do            Begin               If (TempPtr^.Next^.Next = nil) Then                Begin                    Done := True;                    Break;                end;               i := i + 1;               TempPtr := TempPtr^.Next;            end;   If (not Done) Then      Begin        TempNode := TempPTR^.Next^;        TempPTR^.Next^ := Node;        Node.Next^:= TempNode;      end;End; Procedure PrintList;Var  Temp : PTR;Begin   New(Temp);     Temp := HList;   While (Temp <> Nil) Do       Begin            Writeln(Temp^.StudRec.Name);            Writeln(Temp^.StudRec.Age);            Temp := Temp^.Next;         end;End;   begin  Writeln ('How Many Entry Do Yu Have?');  Readln(n);  ListInitialize;  For i := 1 To n Do      Begin         WriteLn('Enter The Name and Age');         Readln(Student.Name);       Readln(Student.Age);         AddData(Student);      end;   {Writeln ('What is the index?');  Readln(x);  WriteLn('Enter The Name and Age');  Readln(Student.Name);  Readln(Student.Age);  InsertByIndex(x, Student);  WriteLn('The List Is As Follow');}  PrintList;  Writeln('Done');  Readln;end.  as you see it's almost the simplest way to create this type of data structure so if any can tell me how to fix the printing code... thnx ---------------------------------------------------------------------- Bart · 2016-06-27 16:26 Borrowed from http://pascal-programming.info/articles/linkedlists.php? Bart ---------------------------------------------------------------------- fury2080 · 2016-06-27 16:32 QuoteBorrowed from http://pascal- programming.info/articles/linkedlists.php? Bart yea Bart that's right but not only this also this site http://pjhayward.net/pascal/questions/pointers.php so about the printing problem ... do you have any idea ? ---------------------------------------------------------------------- Leledumbo · 2016-06-27 17:07 Do these in sequence: [Initial condition] HList = TList = nil [AddData Node1] Node1.Next initialized, points to somewhere on the heap (1) HList initialized, points to somewhere on the heap (2) TList initialized, points to somewhere on the heap (3) HList pointed data gets a copy of Node1 data TList pointed data gets a copy of Node1 data At this point, examine: HList.Next & TList.Next. Do they point to (1), (2), (3) or somewhere else? Where should they supposed to point to? [AddData Node2] Node1.Next initialized, points to somewhere on the heap (1) TList.Next pointed data gets a copy of Node1 data TList pointed data gets a copy of Node1 data At this point, examine: HList.Next & TList.Next. Do they point to (1) or somewhere else? Where should they supposed to point to? Above steps should be enough to help you find your mistakes. Use the debugger facility to help answering the questions. Happy debugging! ---------------------------------------------------------------------- Bart · 2016-06-27 17:26 Sounds like a school assignment to me. My link has, at the bottom of the page a link to the full source. Remove  InsertRecordByID(00011, SampRec); //that one just crashes  DeleteNodeWithID(20211); AFAICS it tries to print more records than there are in the list. Code: [Select]================= Victor Saliba 19 12345 M ================= ================= Mario Petrack 42 11 M ================= ================= Mary Kels 22 20211 F ================= ================= Ken Bolimpart 19 4148 M ================= ================= Kelly Becks 16 4148 F ================= ================= -1 -1 ================= Exitcode = 216 Runtime error 216 at $004016F3   $004016F3  PRINTALL,  line 256 of lltest.lpr   $004018F1  main,  line 286 of lltest.lpr   $004091E1 Line 256 is in the PrintAll procedure: >>   Node := Node^.NodePtr; I would guess that when adding a new record the field NotePtr of the newly created node is not set to nil and therefore it will point to a random memory location. Code: [Select]Procedure AddRecord(StudRec : TStudRec); Var     Node : TNode; Begin  Node.StudRec := StudRec;  New(Node.NodePtr);  //inset this line to initialize the pointer to the next record to nil  Node.NodePtr^.NodePtr := nil; .. That fixes the printing. Notice also that the program never frees the memory it allocates. You can clearly see this when you compile with heaptrace enabled (-gh switch) Code: [Select]... ================= Kelly Becks 16 4148 F ================= Done Printing. Heap dump by heaptrc unit 28 memory blocks allocated : 624/696 20 memory blocks freed     : 432/504 8 unfreed memory blocks : 192 True heap size : 98304 (144 used in System startup) True free heap : 97392 Should be : 97456 Call trace for block $01575A70 size 24   $00401901  main,  line 287 of lltest.lpr Call trace for block $01575A10 size 24   $004018CE  main,  line 283 of lltest.lpr Call trace for block $015759B0 size 24   $004018A7  main,  line 281 of lltest.lpr Call trace for block $01575950 size 24   $00401880  main,  line 279 of lltest.lpr Call trace for block $015758F0 size 24   $00401859  main,  line 277 of lltest.lpr Call trace for block $01575890 size 24   $00401835  main,  line 275 of lltest.lpr Call trace for block $01575830 size 24   $00401835  main,  line 275 of lltest.lpr Call trace for block $015757D0 size 24   $00401835  main,  line 275 of lltest.lpr So, as a tutorial basically this one sucks  You can find a more basic example of linked lists at http://mypascalbook.blogspot.nl/2009/11/13-pointer-basics-link-lists- in-pascal.html. This example also at least tries to free the allocated memory, but due to an error in the code it only frees half of the points it created. Can you spot the errror? You might be better of trying to expand the latter example to fit your needs. If linked lists are NOT required then you can use other (easier) solutions (arrays, TList etc). Bart ---------------------------------------------------------------------- fury2080 · 2016-06-27 22:12 thnx all for the quick reply and thnx Bart Temp^.Next^.next := nil; in print procedure fixed it QuoteSounds like a school assignment to me. and no it's not school assignment i'm actually trying to learn pascal for real and i'm following different  tutorial for this and by the way it seems as if u think there are other ways to code the linked list but all the tutorials i read used this exact way and i'm trying to use it with the class instead of record now. so if u have any advice for a Newbie ... i'll be glad to follow. QuoteNotice also that the program never frees the memory it allocates. i know i need to dispose the allocated memory to prevent the memory leak and so but i just did not put the whole code thanx again everyone ---------------------------------------------------------------------- shobits1 · 2016-06-27 23:30 Quote from: fury2080 on June 28, 2016, 12:12:54 am... i'm trying to use it with the class instead of record now. so if u have any advice for a Newbie ... i'll be glad to follow. There is a TLinkedList class in FPC source,, you could study it's code : http://svn.freepascal.org/cgi- bin/viewvc.cgi/branches/fixes_3_0/rtl/objpas/classes/sllist.inc or you can found locally under [FPC directory]\rtl\objpas\classes\sllist.inc cheers. ---------------------------------------------------------------------- fury2080 · 2016-06-28 00:16 Thnx shobits1 it was great help