Main Menu

Reading a Linked List

Started by RetroBasic, August 03, 2019, 10:45:03 AM

Previous topic - Next topic

RetroBasic

Is there a way to read a Linked List in the same order the items were added?
e.g. Adding them in the order 1,2,3 and then reading them back out in the order 1,2,3
Using "For Each" results in the last item added being the first item out e.g. 1,2,3 becomes 3,2,1

As a work around I'm reading the items out of one List and straight into another, in order to reverse the order.


kevin

#1
 Yeah the insertion methods were on the todo list back in the day, but have somehow been omitted.   There's a conversation about some syntax ideas somewhere on here, but but that's all i remember.  


You could man handle the array to implement a reserve order or even a sort insert function, but it's not for the faint hearted.  



PlayBASIC Code: [Select]
   setfps 20

Type tSTUFF
Name$
X#,Y#,Size#
EndType


Dim MyList as tStuff List

Do
cls
print "Press space to add to list"
print "Enter to reverse list"
print ""

// Press SPACE KEY to add something to the list
if Spacekey()
MyList = new tStuff
MyList.Name$ =Make$(Chr$(asc("a")+Index),8)
Index++
endif

// Press ENTER KEY to reverse the list order
if enterkey()
ReverseList(MyList())
endif


// show the list
for each Mylist()
print MyList.Name$
next

Sync
loop



Function ReverseList(Me as tStuff List)

List_Size=getlistsize(me())

if List_Size>1

// Do flip of handles in the array
ptr=getarrayptr(me(),true)

// Get the size of array table
Size = GetarrayElements(me())

// make a temp array big enough to hold the data
Dim _Temp(Size)

for each me()
// grab the handle and store it in the array
_Temp(index) = me
Index++
next

for each me()
Index--
pos=Getlistpos(me())

PokeInt Ptr+(pos*4),_Temp(Index)

next

endif
EndFunction






btw:  Could you please post your code snippet,   So anyone else looking into the subject can find some ideas on a solution..