Recent

Author Topic: First linked list I've coded in over 30 years.....  (Read 833 times)

TBMan

  • Full Member
  • ***
  • Posts: 127
First linked list I've coded in over 30 years.....
« on: January 28, 2025, 03:46:01 pm »
I started a layout app for my simple programs and games that I write using ptcgraph. I waste a lot of time guessing where I want a message box to pop up or an area to have info and/or graphics. So I had to bite the bullet and start to relearn how to use objects. One object by itself in this sort of app is useless and I could have used an array, but a linked list is better I think. Right now the layout app has one object, a rectangle, that I can move on the screen and resize using the mouse. It writes the code of the rectangle to a text file. The app will eventually allow multiple rectangles on the screen, each with a name for identification and for writing to the text file. The position of the rectangle is a record:

Code: Pascal  [Select][+][-]
  1. trect = record
  2.    x1,y1,
  3.    x2,y2 :integer;
  4. end;
  5.  

I have a simple procedure that sets the rectangle's area:

Code: Pascal  [Select][+][-]
  1.  
  2. Procedure SetTrect(var rect:Trect;x,y,xx,yy:integer);
  3. begin
  4. with rect do
  5.  begin
  6.   x1 := x;     y1 := y;
  7.   x2 := xx;   y2 :=yy;
  8.  end;
  9. end;
  10.  
  11.  

The code written by the app will be simply be a list of procedure calls with the trect names and positions.
A Trect is used to draw message boxes, push buttons, check boxes, layout screen graphics, capture mouse events, etc so it is the basis of much of what I'm doing right now.

Here's the code to my crude linked list, any pointers (no pun intended) or comments anyone can give would be appreciated. This code has been tested and doesn't crash, lol.

Code: Pascal  [Select][+][-]
  1. program listtest;
  2.  
  3. uses
  4.   ptccrt;
  5.  
  6. type
  7.   Pitem = ^Items;
  8.  
  9.   Items = record
  10.     x, y: integer;
  11.     Next: PItem;
  12.     Name: string;
  13.   end;
  14.  
  15. var
  16.   First, Current, last: Pitem;
  17.   ItemNumber: integer;  {for example output}
  18.  
  19.  
  20.   procedure additem(xx, yy: integer);
  21.   var
  22.     t: Pitem;
  23.   begin
  24.     T := New(Pitem);
  25.     T^.x := xx;
  26.     T^.Y := yy;
  27.     Inc(ItemNumber);
  28.     Str(ItemNumber, T^.Name);
  29.     if First = nil then
  30.     begin
  31.       First := T;
  32.       Current := T;
  33.     end
  34.     else
  35.     begin
  36.       Current^.Next := T;
  37.       Current := T;
  38.     end;
  39.     Current^.Next := nil;
  40.  
  41.     Last := Current;
  42.     Last^.Next := nil;
  43.  
  44.   end;
  45.  
  46.  
  47.   procedure displayItem(Pt: Pitem);
  48.   begin
  49.     with Pt^ do
  50.     begin
  51.       writeln('ItemNumber:', Name + ' has this data: x=', x, ' y = ', y);
  52.     end;
  53.  
  54.   end;
  55.  
  56. begin
  57.   ItemNumber := 0;
  58.   First := nil;
  59.   Last := nil;
  60.   Current := nil;
  61.  
  62.   AddItem(1, 1);
  63.   AddItem(5, 5);
  64.   additem(1, 8);
  65.   AddItem(4, 5);
  66.   AddItem(10, 10);
  67.  
  68.   Current := First;
  69.   repeat
  70.     DisplayItem(current);
  71.     Current := Current^.Next;
  72.  
  73.   until Current = nil;
  74.  
  75.   repeat until keypressed;
  76.  
  77. end.
  78.  

TRon

  • Hero Member
  • *****
  • Posts: 4351
Re: First linked list I've coded in over 30 years.....
« Reply #1 on: January 28, 2025, 03:54:14 pm »
Going old-school, nice !

Just for the record (phun intended) there already exist a TRect structure and a rect function.

Reason to mention is that it offers some additional functionality that could make your life a bit easier (translation f.i.). Combined with bounds can make code more understandable as well.
« Last Edit: January 28, 2025, 04:06:51 pm by TRon »
Today is tomorrow's yesterday.

cdbc

  • Hero Member
  • *****
  • Posts: 2125
    • http://www.cdbc.dk
Re: First linked list I've coded in over 30 years.....
« Reply #2 on: January 28, 2025, 04:13:47 pm »
Hi
I'm too, liking the 'Old school' approach \o/
If you want a little inspiration to/on linked lists, you could take a gander at THIS.
About this time tomorrow, I'll publish a singly linked list class & interface, if that might interest you... :D
I like your way of thinking  8)
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 3.6 up until Jan 2024 from then on it's both above &: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 4.99

Thaddy

  • Hero Member
  • *****
  • Posts: 16932
  • Ceterum censeo Trump esse delendam
Re: First linked list I've coded in over 30 years.....
« Reply #3 on: January 28, 2025, 05:34:02 pm »
I beat your linked list, be it a singly linked list, forward: next month 48 years ago..Just after I left the military draft.
Doubly linked list took a few more years.
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

dbannon

  • Hero Member
  • *****
  • Posts: 3344
    • tomboy-ng, a rewrite of the classic Tomboy
Re: First linked list I've coded in over 30 years.....
« Reply #4 on: January 29, 2025, 12:28:31 am »
I did some benchmark testing for a medium sized data structure in my app. It had a fairly complicated record at its center containing a potentially long string.

I tested a single linked list, a double one and then a FPList and FPList won the contest by a useful margin.

As I considered Linked Lists really cool, having used them in C code since early eighties, I was seriously surprised ! So, I suggest you look at using TFPList, its easy and works well.

Davo

Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

Joanna from IRC

  • Hero Member
  • *****
  • Posts: 1390
Re: First linked list I've coded in over 30 years.....
« Reply #5 on: January 29, 2025, 12:49:41 am »
I played around with double linked lists a long Time ago with turbo pascal.
It’s rather difficult to access things in middle of list. I soon replaced it with tcollection.
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  #pascal Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

cdbc

  • Hero Member
  • *****
  • Posts: 2125
    • http://www.cdbc.dk
Re: First linked list I've coded in over 30 years.....
« Reply #6 on: January 29, 2025, 02:35:50 am »
Hi
Right, so I've published a first draft of a 'Singly linked list' class & interface, in my 'IContainers' repository on Gitlab.
It's preliminary, but should work(too tired to test now) ...so stay tuned  8-)
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 3.6 up until Jan 2024 from then on it's both above &: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 4.99

 

TinyPortal © 2005-2018