home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
AMOS PD CD
/
amospdcd.iso
/
351-375
/
apd372
/
jason_banks
/
queue.amos
/
queue.amosSourceCode
Wrap
AMOS Source Code
|
1991-06-13
|
7KB
|
207 lines
'
' Queue.amos
'
' (c) 12/91 Jason Banks - Routines used to manipulate a double-linked list
'
' Program to demonstrate use of linked lists - see docs procedure for notes
'
LISTBASE=0
'
_NEXT=0 : _PREV=4 : Rem add in other data-pointers here.
_CARDNO=8 : _CARDVAL=12 : SIZEOF=16
'
Global _NEXT,_PREV,_CARDNO,_CARDVAL,SIZEOF
'
' Structure is:-
' 00 Next long
' 04 Prev long
' 08 cardno word
' 0A cost word
'
INITSCREEN
MAINMENU
'
' Specific Use (Main Program Body) procedures
'
Procedure MAINMENU
Shared LISTBASE
DNE=False
While Not DNE
Cls 0
Locate 2,2 : Centre "QUEUE"
Locate 2,3 : Centre "Christmas Card List"
Locate 0,10 : Centre "1 ... Add New Card to list"
Locate 0,11 : Centre "2 ... List all cards"
Locate 0,12 : Centre "3 ... Delete A Card"
Locate 0,13 : Centre "4 ... Locate a Card"
Locate 0,16 : Centre "0 ... Quit this program"
_GETOPT["12340"] : OPT$=Param$
On Val(OPT$) Proc _ADD,_LIST,_DELETE,_LOCATE
If OPT$="0" Then DNE=True
Wend
_KILLLIST[LISTBASE,SIZEOF] : Cls 0 : Home : Print Param;" items deleted!" : Clear Key : Wait Key
End Proc
Procedure INITSCREEN
While Screen>-1 : Screen Close Screen : Wend
Screen Open 0,640,256,4,Hires : Curs Off : Flash Off : Hide On : Palette Colour(1),0 : Cls 0 : Pen 1 : Paper 0 : Ink 1,0,0
End Proc
Procedure _ADD
Shared LISTBASE
Cls 0
Locate 0,5 : Centre "Add New Record"
Locate 0,10 : Input "Card # : ";CNO
Locate 0,11 : Input "Card Value : ";CVAL#
_GETMEM[16] : ITEM=Param : Loke ITEM+_CARDNO,CNO : Loke ITEM+_CARDVAL,CVAL#*100
_ADDLIST[LISTBASE,ITEM] : LISTBASE=Param
End Proc
Procedure _LIST
Shared LISTBASE
If LISTBASE=0 Then Pop Proc Else Cls 0 : Home
STRT=LISTBASE : COUNT=0
While STRT<>0 and COUNT>-1
Inc COUNT
Print COUNT,"# ";Leek(STRT+_CARDNO),"$ ";(Leek(STRT+_CARDVAL)/100)
If COUNT mod 20=0 Then Print "Continue? [Y/N]" : _GETOPT["YN"] : If OPT$="N" Then COUNT=-5
_GETNEXT[STRT] : STRT=Param
Wend
If COUNT mod 20<>0 and COUNT>0 Then Clear Key : Wait Key
End Proc
Procedure _DELETE
Shared LISTBASE
_REMOVE[LISTBASE,LISTBASE,SIZEOF] : LISTBASE=Param
End Proc
Procedure _LOCATE
Shared LISTBASE
Cls : Locate 0,5 : Centre "LOCATE:-"
Locate 0,10 : Input "Card # = ";CNO : CVAL=0 : ITEM=LISTBASE
While ITEM<>0 and CVAL=0
If Leek(ITEM+_CARDNO)=CNO
CVAL=Leek(ITEM+_CARDVAL)
Else
_GETNEXT[ITEM] : ITEM=Param
End If
Wend
If CVAL<>0
Cls 0 : Home : Print "Card found at ";Hex$(ITEM);" with value of : ",(CVAL/100)
Else
Cls 0 : Home : Centre "Card Not Found!"
End If
Clear Key : Wait Key
End Proc
'
' general use procedures
'
Procedure _ADDLIST[LISTBASE,ITEM]
' TAKES THE LOCATION OF A PRE-DEFINED DATA ELEMENT, AND ADDS IT INTO THE LIST
' usage is :: _ADDLIST[listbase,itemptr] : listbase=param
'
NEWLISTBASE=ITEM
Loke ITEM+_PREV,0
If LISTBASE=0
Loke ITEM+_NEXT,0
Else
Loke ITEM+_NEXT,LISTBASE
TEMPITEM=LISTBASE
Loke TEMPITEM+_PREV,ITEM
End If
End Proc[NEWLISTBASE]
Procedure _INSERT[LISTBASE,ITEMPTR,ITEM]
' Inserts a data element in the list, before the itemptr passed as a pointer
' usage is :: _INSERT[Listbase,insertposition,itemptr] : listbase=param
'
If Leek(ITEMPTR+_PREV)=0
' at start of list!
_ADDLIST[LISTBASE,ITEM] : LISTBASE=Param
Else
PPTR=Leek(ITEMPTR+_PREV)
Loke PPTR+_NEXT,ITEM : Loke ITEMPTR+_PREV,ITEM
Loke ITEM+_NEXT,ITEMPTR : Loke ITEM+_PREV,PPTR
End If
End Proc[LISTBASE]
Procedure _GETNEXT[ITEM]
' Returns the address of the next item in the list
' usage is :: _Getnext[item] : itemnext=param
If ITEM=0
NITEM=0
Else
NITEM=Leek(ITEM+_NEXT)
End If
End Proc[NITEM]
Procedure _GETPREV[ITEM]
' Returns the address of the previous item in the list
' usage is :: _GetPrev[item]: itemprev=param
If ITEM=0
PITEM=0
Else
PITEM=Leek(ITEM+_PREV)
End If
End Proc[PITEM]
Procedure _GETFIRST[ITEM]
' This can be used to find the start of a list, when the LISTBASE has been lost, or is unknown.
' a problem that can occur here, is that in a circular-linked list (one that does not have a start or end)
' this can possibly cause an infinite loop.
' usage _getfirst[item] : strt=param
'
_GETNEXT[ITEM] : STP=Param
While(Leek(ITEM+_PREV)<>0) and(ITEM<>STP)
ITEM=Leek(ITEM+_PREV)
Wend
End Proc
Procedure _GETLAST[ITEM]
' This can be used to find the end item of a lst.
' usage _getlast[item] : finish=param
'
_GETPREV[ITEM] : STP=Param
While(Leek(ITEM+_NEXT)<>0) and(ITEM<>STP)
ITEM=Leek(ITEM+_NEXT)
Wend
End Proc
Procedure _REMOVE[LISTBASE,ITEM,SIZE]
' can be used to delete a record from the list. Note that it is essential that you get all of the parameters
' correct - it always is, but this can crash the computer VERY VERY easily.
' Usage :: _Remove[Listbase,Itemptr,SizeofItem]
'
If ITEM=0 or LISTBASE=0 Then Pop Proc
NITEM=Leek(ITEM+_NEXT) : PITEM=Leek(ITEM+_PREV)
_FREEMEM[ITEM,SIZE]
If NITEM<>0 Then Loke NITEM+_PREV,PITEM
If PITEM<>0 Then Loke PITEM+_NEXT,NITEM
If PITEM=0 Then LISTBASE=NITEM
End Proc[LISTBASE]
Procedure _GETMEM[SIZE]
Dreg(0)=SIZE : Dreg(1)=$10001 : PTR=Execall(-198)
End Proc[PTR]
Procedure _FREEMEM[PTR,SIZE]
Dreg(0)=SIZE : Areg(1)=PTR : DUMMY=Execall(-210)
End Proc
Procedure _KILLLIST[LISTBASE,SIZE]
' This procedure kills an entire list, and thus free's the memory consumed by it.
' USAGE :: _KIlllist[listbase,sizeofitem]
'
ITEM=LISTBASE : ICOUNT=0
While ITEM<>0
NITEM=Leek(ITEM+_NEXT) : _FREEMEM[ITEM,SIZE] : ITEM=NITEM : Inc ICOUNT
' note that we are not using the _REMOVE procedure.
' this is because we do not need to use a "Safe" procedure,
' as we don't care what happens to the list once we've deleted it!
Wend
LISTBASE=0
End Proc[ICOUNT]
Procedure _DOCS
'
' This is a simple program that will keep track of a list of numbers,
' used to represent a christmas card, and the cost of that christmas card
' in pence.
'
' Also note, that I've altered the KILLLIST procedure a little, so that it returns
' as a param, the number of records that it has deleted. This is so that you can check
' that the program is working properly. One of the major drawbacks (and advantages at times!)
' of working with linked lists, is that they are totally dependant on the programmer - one mistake
' and you can loose the entire list!, while the basic arrays are a "Safe" (but slow) form of
' storing data.
'
End Proc
Procedure _GETOPT[TXT$]
TXT$=Upper$(TXT$) : OUT$=""
While Instr(TXT$,OUT$)<1 : OUT$=Upper$(Inkey$) : Wend
End Proc[OUT$]