linked list pointer

Just starting out? Need help? Post your questions and find answers here.
ymerdy
User
User
Posts: 10
Joined: Thu Jan 18, 2018 11:54 pm

linked list pointer

Post by ymerdy »

Hello,

I'm coding graph charts.
I try to have a data structure with a pointer to a global linked list, in order to share common lists between several charts (a list for colors used by the graph, a list of values, etc...).

Does anybody know how PureBasic manages linked lists to store list informations such as listsize, first element, last element?

I know that PB adds 2 pointers (*previous and *next) in a header of each element, but I don't know where are stored other informations.

Thanks!
User avatar
Demivec
Addict
Addict
Posts: 4086
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: linked list pointer

Post by Demivec »

You can have several pointers to a common list that is stored in a structure but the list can have only one set of attributes, such as current element.

Code: Select all

Structure commonList
  List myList.i()
EndStructure 

Global colors.commonList

Define *graph1.commonList = @colors, *graph2.commonList = @colors

AddElement(colors\myList()): colors\myList() = RGB(10, 10, 10)
AddElement(colors\myList()): colors\myList() = RGB(20, 20, 20)
AddElement(colors\myList()): colors\myList() = RGB(30, 30, 30)
AddElement(colors\myList()): colors\myList() = RGB(40, 40, 40)
AddElement(colors\myList()): colors\myList() = RGB(50, 50, 50)
AddElement(colors\myList()): colors\myList() = RGB(60, 60, 60)
AddElement(colors\myList()): colors\myList() = RGB(70, 70, 80)
AddElement(colors\myList()): colors\myList() = RGB(80, 80, 80)
AddElement(colors\myList()): colors\myList() = RGB(90, 90, 90)
AddElement(colors\myList()): colors\myList() = RGB(100, 100, 100)

FirstElement(*graph1\myList())
Debug ListSize(*graph1\myList())
ForEach *graph2\myList()
  Debug "color: " + Hex(*graph2\myList(), #PB_Long)
Next

;Since the list being pointed to is the same both indexes are the same, the index of the last element
Debug ListIndex(*graph1\myList())
Debug ListIndex(*graph2\myList())

;You can move through the common list if only reading elements by using
;nested PushListPosition()/PopListPosition(), but this cannot be done in a shuffled manner.
;If this is done in separate threads then a Mutex or other access control feature needs to be used also.
SelectElement(*graph1\myList(), 3) ;select element 3 of list using *graph1 pointer.

Debug "graph1 color: " + Hex(*graph1\myList(), #PB_Long)

PushListPosition(*graph1\myList()) ;save the current position, either pointer could be used (i.e. *graph2 or *graph1)

;cycle through all colors again using the list pointer *graph2
ForEach *graph2\myList()
  Debug "graph2 color: " + Hex(*graph2\myList(), #PB_Long)
Next

PopListPosition(*graph1\myList()) ;restore the current position, either pointer could be used (i.e. *graph2 or *graph1)
Debug "graph1 color: " + Hex(*graph1\myList(), #PB_Long)
Last edited by Demivec on Fri Apr 20, 2018 2:52 am, edited 1 time in total.
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: linked list pointer

Post by Josh »

ymerdy wrote:Hello,

I'm coding graph charts.
I try to have a data structure with a pointer to a global linked list, in order to share common lists between several charts (a list for colors used by the graph, a list of values, etc...).

Does anybody know how PureBasic manages linked lists to store list informations such as listsize, first element, last element?

I know that PB adds 2 pointers (*previous and *next) in a header of each element, but I don't know where are stored other informations.

Thanks!
Maybe you should explain what you have in mind before you want to go deep into PureBasic Internas in your first posting.
sorry for my bad english
Fred
Administrator
Administrator
Posts: 16618
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: linked list pointer

Post by Fred »

Messing with PB internal is a bad idea, it could break anytime in future version
ymerdy
User
User
Posts: 10
Joined: Thu Jan 18, 2018 11:54 pm

Re: linked list pointer

Post by ymerdy »

Fred wrote:Messing with PB internal is a bad idea, it could break anytime in future version
For sure, but it'good to understand how things work in the background!

Envoyé de mon Power_3 en utilisant Tapatalk
User avatar
Olliv
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Sep 22, 2009 10:41 pm

Re: linked list pointer

Post by Olliv »

Hi Ymerdy,

I think you have exactly what you want here without internal overview.

Is it okay?

Code: Select all

Structure LST
List Act.S()
EndStructure

Global Todo.LST

Structure LSTPTR
List *Act()
EndStructure



Procedure Ajouter(Quoi.S, *AQuoi.LSTPTR)

AddElement(Todo\Act() ) ; inchangé
Todo\Act() = Quoi

AddElement(*AQuoi\Act() )
*AQuoi\Act() = @Todo\Act()

EndProcedure.


Procedure Lire(*Quoi.LST)

ForEach(*Quoi\Act() )
ChangeCurrentElement(Todo(), *Quoi\Act() )
Debug Todo\Act()
Next

Debug " "
EndProcedure

Define Lundi.LSTPTR
Define Mardi.LSTPTR

Ajouter("Courses", Lundi)
Ajouter("Coiffeur", Mardi)
Ajouter("Dentiste", Mardi)

ForEach Todo()
Debug Todo()
Next
Debug " "

Lire(Lundi)
Lire(Mardi)
User avatar
Demivec
Addict
Addict
Posts: 4086
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: linked list pointer

Post by Demivec »

ymerdy wrote:I try to have a data structure with a pointer to a global linked list, in order to share common lists between several charts (a list for colors used by the graph, a list of values, etc...).
What kind of access to the list or lists is needed? Did you need to only read from the list or did you also need to add and delete items (possibly reordering them)?

Did you need to access a list common to several charts for only one chart at a time or did you need to interleave or mix the access to the same list from more than one chart or object at the same time?

Can you give a brief outline of how you might use the type of access you are interested in?
Post Reply