Recent

Author Topic: How to implement generic TListNode?  (Read 582 times)

Kyun

  • Newbie
  • Posts: 3
How to implement generic TListNode?
« on: May 10, 2025, 05:55:34 am »
I attempted to learn data structures using Pascal, but soon ran into problems.

Code: Pascal  [Select][+][-]
  1. type
  2.   generic TListNode<T> = record
  3.     Value: T;
  4.     Prev: TListNode<T>; // error
  5.     Next: TListNode<T>; // error
  6.   end;
  7.  

I don't know what kind of grammar to use here.
« Last Edit: May 10, 2025, 05:57:33 am by Kyun »

ALLIGATOR

  • Sr. Member
  • ****
  • Posts: 404
  • I use FPC [main] 💪🐯💪
Re: How to implement generic TListNode?
« Reply #1 on: May 10, 2025, 06:10:44 am »
Code: Pascal  [Select][+][-]
  1.   generic TListNode<T> = record
  2.     Value: T;
  3.     Prev: Pointer;
  4.     Next: Pointer;
  5.   end;
  6.  
or
Code: Pascal  [Select][+][-]
  1. program app;
  2. {$modeswitch advancedrecords}
  3.  
  4. type
  5.   generic TListNode<T> = record
  6.   type
  7.     TSelf = specialize TListNode<T>;
  8.     PSelf = ^TSelf;
  9.   public
  10.     Value: T;
  11.     Prev: PSelf;
  12.     Next: PSelf;
  13.   end;
  14.  
I may seem rude - please don't take it personally

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: How to implement generic TListNode?
« Reply #2 on: May 10, 2025, 06:25:48 am »
but soon ran into problems.

ALLIGATOR showed possible solutions. Reason you got stuck is described here which basically tells that it is not possible to forward types (unless using a pointer to the type).

Today is tomorrow's yesterday.

Thaddy

  • Hero Member
  • *****
  • Posts: 18787
  • To Europe: simply sell USA bonds: dollar collapses
Re: How to implement generic TListNode?
« Reply #3 on: May 10, 2025, 08:34:24 am »
Yes. Prefer the second example over the typeless pointer, though.
The reason that it is the correct way, using a typed pointer,  is that when you would use a record type itself you will get an endless loop if the compiler would not prevent it. And an untyped pointer is almost never a good idea.
Using a record itself would expand into infinity..... (Think!)
« Last Edit: May 10, 2025, 09:00:20 am 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...

Kyun

  • Newbie
  • Posts: 3
Re: How to implement generic TListNode?
« Reply #4 on: May 10, 2025, 04:52:39 pm »
Code: Pascal  [Select][+][-]
  1.   generic TListNode<T> = record
  2.     Value: T;
  3.     Prev: Pointer;
  4.     Next: Pointer;
  5.   end;
  6.  
or
Code: Pascal  [Select][+][-]
  1. program app;
  2. {$modeswitch advancedrecords}
  3.  
  4. type
  5.   generic TListNode<T> = record
  6.   type
  7.     TSelf = specialize TListNode<T>;
  8.     PSelf = ^TSelf;
  9.   public
  10.     Value: T;
  11.     Prev: PSelf;
  12.     Next: PSelf;
  13.   end;
  14.  

Thanks, Pointer like C++'s void*

Kyun

  • Newbie
  • Posts: 3
Re: How to implement generic TListNode?
« Reply #5 on: May 10, 2025, 04:53:58 pm »
but soon ran into problems.

ALLIGATOR showed possible solutions. Reason you got stuck is described here which basically tells that it is not possible to forward types (unless using a pointer to the type).

Thank you, this document is very useful.

 

TinyPortal © 2005-2018