Recent

Author Topic: Advice on how to Combine a Linked List with a 3D Array.  (Read 871 times)

Weitentaaal

  • Hero Member
  • *****
  • Posts: 554
Advice on how to Combine a Linked List with a 3D Array.
« on: June 13, 2023, 11:11:19 am »
Hello,

im currently trying to Visualize a Linked List, but im stuck beacuse i don't HOW to achiev what i want. Every time i think i have a decent Solution, I find a new problem.
i try to explain my problem using Code examples , i hope you understand and could give me some advice on how to move on.
the project is similar to this one: https://forum.lazarus.freepascal.org/index.php/topic,38136.msg259652.html#msg259652

Briefly what i have:
Code: Pascal  [Select][+][-]
  1. T3DPoint = record
  2.         X: integer;
  3.         Y: Integer;
  4.         Z: Integer;
  5. end;
  6.  
  7. T3DCubeArr = Array of Array of Array of TCube;
  8.  
  9. TLinkListItem = class
  10.         //Properties, etc
  11. end;
  12.  
  13. TLinkList = class
  14. private
  15.         fFirst: TLinkListItem;
  16.         fLast: TLinkListItem;
  17.         fSize: integer;
  18. public
  19.         property First: TLinkListItem read fFirst;
  20.         property Last: TLinkListItem read fFirst;
  21.         property Size: integer read fSize;
  22.        
  23.         procedure Add(position: integer; item: TLinkListItem);
  24.         procedure Remove(position: integer);
  25. end;
  26.  
  27. TCube = class
  28. private
  29.         fI]"]>BlockednIt: TLinkListItem;
  30.         fPosition: T3DPoint;
  31.        
  32. public
  33.         property I]"]>BlockednIt: TLinkListItem read fI]"]>BlockednIt;  
  34.  
  35.         function Isempty: boolean
  36. end;
  37.  
  38. TcubeLinkList = class
  39.         //is this needed?
  40. end;
  41.  
  42. TVisualizeLinkList = class
  43. private
  44.         fDrawArea: T3DCubeArr;
  45. public
  46.         create(sizeX, sizeY, SizeZ); //create 3d array with certain size
  47.         procedure addCubeAt(point: T3DPoint);
  48.         procedure remCubeAt(point: T3DPoint);
  49. end;
  50.  

I need to be able to move, insert, add, remove, turn.
it's really similar to a snake game but it wont move constantly and there must be more operations available like turn whole list or  insert into the middle etc.

All i need is Advice on which Datastructures i should use and briefly how to actually implement this. I need this Code to be Solid, thats why i asked here in the forum.

Zvoni

  • Hero Member
  • *****
  • Posts: 3315
Re: Advice on how to Combine a Linked List with a 3D Array.
« Reply #1 on: June 13, 2023, 12:20:49 pm »
A linked list usually doesn't have First/Last, but Next and Previous.
Maybe that's your Problem.

I'd look for an example of a doubley linked list, and how to WALK such a list
Because if you know how to walk/navigate such a list, adding/removing is pretty simple.

I wouldn't be surprised, if TList (resp. its generic sister) uses this internally
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

Weitentaaal

  • Hero Member
  • *****
  • Posts: 554
Re: Advice on how to Combine a Linked List with a 3D Array.
« Reply #2 on: June 13, 2023, 12:42:01 pm »
A linked list usually doesn't have First/Last, but Next and Previous.
Maybe that's your Problem.

I'd look for an example of a doubley linked list, and how to WALK such a list
Because if you know how to walk/navigate such a list, adding/removing is pretty simple.

I wouldn't be surprised, if TList (resp. its generic sister) uses this internally

hey, thanks for the reply.
i did write it wrong in the example, i do know how tu use a Linked List. i have it implemented like this:

Code: Pascal  [Select][+][-]
  1. TItem = class
  2.    fNext: TItem;
  3.    fPrev: TItem;
  4. edn;
  5.  
  6. TList = class
  7.    fFirst: TItem;
  8.    fLast: TItem;
  9. end;
  10.  

my problem is that i don't know how to combine this list with the 3d array i have. if i move the whole items in the array (for example the way it moves in a Snake game), then it should not have any impact on the List. The problem i have then is that if i would need the position of the first i would need to search the whole array for this item (i am not sure if that could result in laggs someday, when i have a lot of items in it). I hope you can understand what i mean. im not sure if this method im using will be any good. So what i need is some advice on how i could do this. The final version should be able to take linked lists and view them on the Canvas where the user can change it (drag drop items, move the whole List, etc.)



 

TinyPortal © 2005-2018