help in deleting last object in a linked list please

Started by micky4fun, May 06, 2011, 05:56:50 AM

Previous topic - Next topic

micky4fun

hi all

here is something im trying out , at the moment pressing the up arrow deletes the first item in the linked list (top egg) , but i want to delete the last one (bottom egg) , the one just drawn , but i cant suss it out , is proberly easy enough , but dont want to stuck on this for days
so any help would be great

PlayBASIC Code: [Select]
loadfximage"egg2.bmp",1
Type bask
x#,y#
sprite
EndType
dim pipe as bask list

xpos=0
do
cls 0
if downkey()=1
flushkeys
ypos=ypos+20
pipe = New bask
pipe.sprite=getfreesprite()
createsprite pipe.sprite
spriteimage pipe.sprite,1
positionsprite pipe.sprite,500,ypos
endif

If upKey()
For Each pipe()
If getlistnext(pipe()) = -1
deletesprite pipe.sprite
pipe = NULL
EndIf
Next
FlushKeys
EndIf

drawallsprites
sync

loop


thanks
mick

micky4fun

hi all

its ok suss it , just marked each object then used GetListSize

mick ;D

kevin

#2
  A list works like a stack, so newly added items are added to the head of the list.  Meaning the first item in the list is actually the last item added, since it's the newest on the stack.      

 To delete the last added item, we just reset the list pointer and then null it.    If you wanted to delete the first item added, then you'd have iterate through them.  

PlayBASIC Code: [Select]
   loadfont "arial",1,24

Type tStuff
x,y,Radius,Colour
EndType
Dim Thing as tStuff List

// make a bunch of things
// new items are added to the head of the list, so the
// first thing on the list, is the last thing added.
for lp=1 to 10
Thing = new tStuff
Thing.x = rnd(800)
Thing.y = rnd(600)
Thing.radius = rndrange(10,20)
Thing.Colour = rndrgb()
next


Setfps 30

Do

CLS

DrawScene()

// delete the last added item in the list.
if spacekey()

// reset the list pointer to it's start position
ResetList Thing()

// kill this item, since the head of the list points
// at the last item
Thing=Null

flushkeys

EndIF


print "LIst Size:"+Str$(GetLIstSize(Thing()))

Sync
loop

Function DrawScene()
// draw items, numbered from first to last.

// the the size of the list, since this is the FIRST
// item in the list.
Index=GetListSize(Thing())
for each Thing()
x=thing.x
y=thing.y
circlec x,y,Thing.radius,true,Thing.Colour

// draw it's addition position
centertext x,y-10,Index
// decrease this position
Index--
next
EndFunction






micky4fun

hi all

thanks Kevin , thats made my code quite a bit simpler .
lets keep tapping away and see if dig anything up with this

mick :D

kevin

#4
  You can grab the raw pointer to the link list array and hack the array container directly.. But really I wouldn't recommend that !

 If you really need some specialize list/tree functionality, you can always roll your own.  Here's a couple of examples that create a linked list behaviors using code..  

 The combination of typed pointers and dynamic array creation allow for some pretty out there data structures to be created, form the simplest of constructs.