Page 1 of 2

Pointers admission for LinkedList functions

Posted: Thu Sep 18, 2003 5:37 pm
by Psychophanta
Perhaps this topic was issued in the past, but i have not found...

Code: Select all

Structure blair
  fil.l
  *bin.blair
  *hussein.bush
EndStructure

Structure bush
  aznar.l
  *ggg.blair
EndStructure

NewList saddam.blair()
NewList osama.bush()

AddElement(saddam())
AddElement(osama())

osama()\ggg=saddam()
osama()\ggg\fil=1234
;I can't do this:
ClearList(osama()\ggg);<-- doesn't work
;neither this:
AddElement(osama()\ggg);<-- doesn't work
;neither this:
*poi.blair=@saddam()
AddElement(*poi);<-- doesn't work
;and it would be interesting for these commands to admit pointers
;because sometimes programming is forced to implement unnecessary code, or more than one procedure
;where should be enought only one.
;If use pointers for these kind of commands (AddElement(),etc.) code would be sometimes smaller.
Debug saddam()\fil
Besides, i think that the ability of using pointers as parameters of such functions like CountList(), FirstElement(), ClearList(), etc. would make that LinkedList stuff was more coherent.


AL :roll:

Posted: Thu Sep 18, 2003 6:19 pm
by GPI

Code: Select all

Structure blair
Structure bush
NewList saddam.blair()
NewList osama.bush()
what to hell are you programming?

Posted: Thu Sep 18, 2003 6:48 pm
by Karbon
War Games 2.0 :-)

Posted: Thu Sep 18, 2003 6:52 pm
by Psychophanta
It don't occurred to me any names for variables, and i put those which are very in fashion now. :twisted:


AL

Posted: Thu Sep 18, 2003 10:41 pm
by Justin
some of them may sound insulting

osama == BIG MOTHERFUCKER

Posted: Thu Sep 18, 2003 11:09 pm
by Karbon
No doubt he's a piece of human trash.. I'm sure Psychophanta meant nothing by it though.. No worries..

Posted: Thu Sep 18, 2003 11:54 pm
by Kale
It would also be handy to pass a pointer of a LinkedList to a procedure such as:

Code: Select all

NewList Hello.s()

AddElement(Hello())
Hello() = "GoodDay"

AddElement(Hello())
Hello() = "Bonjour"

procedure PrintListContents(*ptr)
    ResetList(*ptr())
    While(NextElement(*ptr()))
        debug *ptr()
    Wend
endprocedure

PrintListContents(@Hello())
Something like that :D Maybe it could be thought out better.

Posted: Fri Sep 19, 2003 8:41 am
by Psychophanta
Exact, Kale; that is just what i request. :D




...and here is another example to show what we are requesting to Fred:

Code: Select all

;This code doesn't work:

NewList Hello1.s()
NewList Hello2.s()

AddElement(Hello1()) 
Hello1() = "GoodDay1" 

AddElement(Hello2()) 
Hello2() = "Bonjour2" 

Procedure PrintListContents(*ptr.s) 
    ResetList(*ptr.s) 
    While(NextElement(ptr.s)) 
        Debug *ptr.s 
    Wend 
EndProcedure 

PrintListContents(Hello1())

PrintListContents(Hello2())
Since ResetList() and NextElement() don't accept normal pointers as argument, we are forced to implement 2 almost identical functions in this case, which could be 3 or more functions almost identical in other cases; look:

Code: Select all

;This code works:
NewList Hello1.s()
NewList Hello2.s()

AddElement(Hello1()) 
Hello1() = "GoodDay1" 

AddElement(Hello2()) 
Hello2() = "Bonjour2" 

Procedure PrintListContents1(*ptr.s) 
    ResetList(Hello1()) 
    While(NextElement(Hello1())) 
        Debug *ptr.s
    Wend 
EndProcedure

Procedure PrintListContents2(*ptr.s) 
    ResetList(Hello2()) 
    While(NextElement(Hello2())) 
        Debug *ptr.s
    Wend 
EndProcedure

PrintListContents1(Hello1())
PrintListContents2(Hello2())

AL

Posted: Fri Sep 19, 2003 10:48 am
by PolyVector
That would be nice to have the list functions accept pointers... could get REALLY tedious if you were keeping many lists and needed many functions for doing the same thing with all of them... PolyVector to the rescue! :D
lol...how sad is this that i've only posted twice now and they've both been on linked lists...anywho...if you need a quick-fix here you go:

Code: Select all

;Modified: uses the 'supergeek' technology described in the help :) 
Structure LinkedString
*Next.LinkedString
*Previous.LinkedString
  value.s
EndStructure


NewList Hello1.LinkedString() 
NewList Hello2.LinkedString() 
*ptr.LinkedString
*ptr=AddElement(Hello1()) 
*ptr\value = "GoodDay1" 

*ptr=AddElement(Hello2()) 
*ptr\value = "Bonjour2" 

Procedure PrintListContents(*ptr.LinkedString) 
  Repeat
    If *ptr
      Debug *ptr\value
    EndIf
    *ptr=*ptr\Next
  Until *ptr=0 
EndProcedure 

PrintListContents(FirstElement(Hello1())) 
PrintListContents(FirstElement(Hello2())) 

Posted: Fri Sep 19, 2003 12:21 pm
by Kale
Nice code PolyVector but doing it like that means you would need many List structures to handle all data types. I was thinking about having a more generic and neat solution :)

Posted: Fri Sep 19, 2003 12:26 pm
by Psychophanta
Yes, of course that is a usual method for to solve the problem.

But anyway, we must face the facts that that code is ugly, not so understandable, because we are forced to use things like:

Code: Select all

    *ptr=*ptr\Next 
  Until *ptr=0 
when PureBasic has a beautiful function which is: NextElement(), which do all it and we can not use inside our functions. :cry:

Besides you are giving FirstElement(Hello1()) as parameter of functions which is not so clean too. :(


AL

Posted: Fri Sep 19, 2003 12:34 pm
by Psychophanta
Brr, sorry this can't accept inserting Code when i write the message. :oops:


Well i hope you understand it.


I take advantage to add that your method, if in a big program with lots of structures, and lots of functions, we forced to use linked lists in an own way (out of PureBasic commands and functions offer us), and to add to all (or most) of structures 2 more elements inside: *Next.structure and *Previous.structure.

AL

Posted: Mon Sep 22, 2003 8:51 am
by PolyVector
Yeah, I do agree that the linked list commands need a little work... Maybe I'm wierd but I always use linked lists the way I described above only because it makes more sense to me... hehe I guess we all code differently... :)

Posted: Mon Sep 22, 2003 10:09 am
by Psychophanta
Hi PolyVector.

You said:
"I always use linked lists the way I described above only because it makes more sense to me... "

However, keep in mind that the main matter of Linked Lists as is implemented in PB, is that each element in list points internally (made by PB transparent to the programmer) to its next and to its previous element, so i understand that it has more sense to you to program like that, but the matter is to make it easy and transparent to the programmer.
So your programs are efficient, but it is redundant to have to use externally a pointer to next and another to previous element in each element in list.

Thank you :D


AL

Posted: Mon Sep 22, 2003 7:22 pm
by Num3
The problem is you don't now where osama is...

And if you push bush into osama, then yu get a major crisis, maybe you should use saddam