Main Menu

Reverse Linked Link Order

Started by kevin, August 07, 2019, 11:44:51 PM

Previous topic - Next topic

kevin


  Reverse Linked Link Order

   Here's a function to reverse the order of items in a linked list

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