Recent

Author Topic: Linked List Using Two Classes  (Read 546 times)

vinntec

  • New Member
  • *
  • Posts: 11
Linked List Using Two Classes
« on: January 13, 2026, 06:20:57 pm »
I am coming back to Pascal after years away, and one of my projects needs a number of linked lists. I checked all the books I had, some of them recent, and most showed how to make a LL with records and pointers. One of them has a class but the data is still a record so pointers needed.

However, looking at something else I realised that using only OOP could make this much simpler and not use visible pointers. So I searched and couldn't find anything, then resorted to asking Google AI. It came up with this solution (which I have edited slightly to make it into a program).
Code: Pascal  [Select][+][-]
  1. program linked_list_ai;
  2.  
  3. type
  4.   TNode = class
  5.   public
  6.     Data: Integer;
  7.     Next: TNode;
  8.     constructor Create(AValue: Integer);
  9.   end;
  10.  
  11.   TLinkedList = class
  12.   private
  13.     FHead: TNode;
  14.   public
  15.     constructor Create;
  16.     destructor Destroy; override;
  17.     procedure Add(AValue: Integer);
  18.     procedure Display;
  19.   end;
  20.  
  21. constructor TNode.Create(AValue: Integer);
  22. begin
  23.   Data := AValue;
  24.   Next := nil;
  25. end;
  26.  
  27. constructor TLinkedList.Create;
  28. begin
  29.   FHead := nil;
  30. end;
  31.  
  32. // Destructor ensures all nodes are freed to prevent memory leaks
  33. destructor TLinkedList.Destroy;
  34. var
  35.   Temp: TNode;
  36. begin
  37.   while FHead <> nil do
  38.   begin
  39.     Temp := FHead;
  40.     FHead := FHead.Next;
  41.     Temp.Free;
  42.   end;
  43.   inherited;
  44. end;
  45.  
  46. // Method to add a new node to the end of the list
  47. procedure TLinkedList.Add(AValue: Integer);
  48. var
  49.   NewNode, Temp: TNode;
  50. begin
  51.   NewNode := TNode.Create(AValue);
  52.   if FHead = nil then
  53.     FHead := NewNode
  54.   else
  55.   begin
  56.     Temp := FHead;
  57.     while Temp.Next <> nil do
  58.       Temp := Temp.Next;
  59.     Temp.Next := NewNode;
  60.   end;
  61. end;
  62.  
  63. procedure TLinkedList.Display;
  64. var
  65.   Temp: TNode;
  66. begin
  67.   Temp := FHead;
  68.   while Temp <> nil do
  69.   begin
  70.     Write(Temp.Data, ' -> ');
  71.     Temp := Temp.Next;
  72.   end;
  73.   Writeln('NIL');
  74. end;
  75.  
  76. var
  77.   MyList: TLinkedList;
  78. begin
  79.   MyList := TLinkedList.Create;
  80.   try
  81.     MyList.Add(10);
  82.     MyList.Add(20);
  83.     MyList.Add(30);
  84.     MyList.Display;
  85.   finally
  86.     MyList.Free; // Calls the destructor and cleans up memory
  87.   end;
  88.   writeln('Press ENTER to continue...');
  89.   readln;
  90. end.
  91.  
For my purposes which has much more data I needed to be able to scan through the list and extract the data. The only change I needed to make was to make the FHead public (and modified Add to be Append and added an Insert method which allowed insertion into the front of the list). I put this into a Unit for convenience, then wrote some code to work through it as I wanted:
Code: Pascal  [Select][+][-]
  1. program linked_list_class;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   crt, LinkedList;
  7.  
  8. var
  9.   nodes: TLinkedList;
  10.   node : TNode;
  11.  
  12. begin
  13.   ClrScr;
  14.   nodes := TLinkedList.Create; { start with an empty list }
  15.   try
  16.     nodes.Append(10);
  17.     nodes.Append(20);
  18.     nodes.Append(30);
  19.     nodes.Display;
  20.  
  21.     { Test working through the list }
  22.     if nodes.Empty then
  23.       writeln('Nodes list is empty!')
  24.     else
  25.     begin
  26.       node := nodes.FHead; {I can probably change this to get FHead? }
  27.       while node <> nil do
  28.       begin
  29.         { I could change Data here with FHead exposed }
  30.         write(node.Data, ' => ');
  31.         node := node.Next;
  32.       end;
  33.       writeln('NIL');
  34.     end;
  35.   finally
  36.     nodes.Free;
  37.   end;
  38.   writeln;
  39.   Writeln('Done. Press Enter to exit...');
  40.   Readln;
  41. end.
  42.  
I just though this might be useful to others. You don't have to use ^ but refer to instances instead. You have to Free these of course, but the syntax seems much tidier.

cdbc

  • Hero Member
  • *****
  • Posts: 2600
    • http://www.cdbc.dk
Re: Linked List Using Two Classes
« Reply #1 on: January 13, 2026, 06:34:17 pm »
Hi
Looks good, well done.
If you feel like it, This thread in post #8 has a linked list, you could have a 'LookSee'...  :)
It amo. contains a sorting mechanism for linked lists...  ;D
...and it makes use of its own memorymanager...
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

Zvoni

  • Hero Member
  • *****
  • Posts: 3242
Re: Linked List Using Two Classes
« Reply #2 on: January 14, 2026, 08:31:24 am »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

440bx

  • Hero Member
  • *****
  • Posts: 6069
Re: Linked List Using Two Classes
« Reply #3 on: January 14, 2026, 08:56:54 am »
Since we're asking questions... why not make it an advanced record instead of a class ? 

It would be simpler, more flexible and require less code and, unless I'm missing something, it can do everything the equivalent class can do (and a few things a class cannot do!)
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: Linked List Using Two Classes
« Reply #4 on: January 14, 2026, 03:25:28 pm »
If it is about speed, a well implemented doubly linked list would be fast.
If it is about convenience one of the generics are notationally better.

Well, everybody knows or should know how to write a linked list....
It is basic... about lesson 1?
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

Zvoni

  • Hero Member
  • *****
  • Posts: 3242
Re: Linked List Using Two Classes
« Reply #5 on: January 14, 2026, 03:31:48 pm »
If it is about speed, a well implemented doubly linked list would be fast.
If it is about convenience one of the generics are notationally better.

Well, everybody knows or should know how to write a linked list....
It is basic... about lesson 1?

Nevermind, that the AI-generated code is a simple linked list (not doubly), which brings us to a Stack
And what do you know......
https://www.freepascal.org/docs-html/fcl/contnrs/tobjectstack.html
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: Linked List Using Two Classes
« Reply #6 on: January 14, 2026, 03:39:08 pm »
I merely pointed out a doubly linked list has obviously better search? Anyway, it is trivial.
Programmers should not need AI for that (although they almost always provide the right answers because lists are trivial)
« Last Edit: January 14, 2026, 03:41:17 pm by Thaddy »
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

 

TinyPortal © 2005-2018