Qbasicnews.com

Full Version: Stack, Queue
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I know it's kinda primitive, but I was more experimenting around and mainly writing some examples of FB, I could put into my tutorial. anyway, if anyone neads a stack then here it is:[syntax="QBASIC"]'$include: 'crtdll.bi'
'FreeBasic Linked List example
'Stack
'By VonGodric
option explicit
TYPE MyStack
DATA AS STRING
Node AS MyStack ptr
END TYPE

SUB Push (PushData AS STRING, Stack AS ANY ptr)
DIM NewNode AS MyStack ptr
NewNode = Malloc(LEN(MyStack))
NewNode->Data = PushData
NewNode->Node = (*Stack)
*Stack = NewNode
END SUB

FUNCTION Pop (Stack AS ANY ptr) AS STRING
DIM OldNode AS MyStack ptr
OldNode = *Stack
Pop = OldNode->Data
*Stack = OldNode->Node
free(OldNode)
END FUNCTION

FUNCTION Top (Stack AS MyStack ptr) AS STRING
Top = *Stack->Data
END FUNCTION

FUNCTION IsEmpty (Stack AS ANY ptr) AS INTEGER
IsEmpty = (*Stack=0)
END FUNCTION


'-------------------------------------------------------------------------------
'Main code

DIM test AS MyStack pointer
dim a as String
DO
a$=INKEY$
IF a$>"" AND a$<>chr$(13) THEN PRINT a$; : push (a$, @test)
LOOP UNTIL a$=CHR$(13)

PRINT

DO WHILE not IsEmpty(@test)
PRINT pop (@test);
PRINT Top (@test);
LOOP

SLEEP

'-------------------------------------------------------------------------------
[/syntax]

Here's queue
[syntax="QBASIC"]'$include: 'crtdll.bi'
'FreeBasic Linked List example
'Queue
'By VonGodric
option explicit
TYPE MyQueue
DATA AS STRING
Node AS MyQueue ptr
END TYPE

type Queue
Rear as MyQueue ptr
Front as MyQueue ptr
end type

function Initalise(QueuePtr as Queue ptr) as integer
QueuePtr->Front = Malloc(len(MyQueue))
If QueuePtr->Front = 0 then Initalise = 0 : Exit Function
QueuePtr->Rear = QueuePtr->Front
QueuePtr->Front->Node = 0
Initalise=-1
end function

Function Join (Queue as Queue ptr, QData as string) as integer
Dim NewNode As MyQueue Ptr
NewNode=Malloc(Len(MyQueue))
If NewNode = 0 Then Join = 0 : Exit Function
NewNode->Data = QData
NewNode->Node = 0
Queue->Rear->Node = NewNode
Queue->Rear = NewNode
Join = -1
end function

function leave(QueuePtr as Queue ptr) as string
dim OldNode as MyQueue ptr
OldNode = QueuePtr->Front->Node
leave = OldNode->data
If (QueuePtr->Front->Node->Node = 0) Then
QueuePtr->Rear = QueuePtr->Front
Else
QueuePtr->Front->Node = QueuePtr->Front->Node->Node
end if
Free(OldNode)
end function

function IsEmpty (QueuePtr as Queue ptr) as integer
IsEmpty = (QueuePtr->Front = QueuePtr->Rear)
end function

'-------------------------------------------------------------------------------
'Main code
dim test as Queue
if not Initalise(@test) then end

DIM a AS STRING
DO
a$=INKEY$
IF a$>"" AND a$<>chr$(13) THEN PRINT a$; : Join (@test, a$)
LOOP UNTIL a$=CHR$(13)
print
DO WHILE NOT IsEmpty(@test)
PRINT Leave (@test);
LOOP

sleep
'-------------------------------------------------------------------------------[/syntax]

Both of them are easily modifiable to hold any data type you wish, just change in the MyStack and MyQueue types the datatype and also in (pop, top and push) and (join, leave) to apporpriate data types.
Neat Smile Try an associative array next time, those are nifty.
you can simply change it from string type to anything -like any ptr, then you can link with eatch node anything you like :wink:
Wooohooo, FBC 0.9 kicks ass Big Grin

lightly better version. needs 0.9 to run.
[syntax="QBASIC"]'FreeBasic Linked-list example
'Stack
'By VonGodric
OPTION explicit
TYPE MyStack
DATA AS STRING
Node AS MyStack ptr
END TYPE

SUB Push (PushData AS STRING, byref Stack AS MyStack ptr)
DIM NewNode AS MyStack ptr
NewNode = Allocate(LEN(MyStack))
NewNode->Data = PushData
NewNode->Node = (Stack)
Stack = NewNode
END SUB

FUNCTION Pop (byref Stack AS MyStack ptr) AS STRING
DIM OldNode AS MyStack ptr
OldNode = Stack
Pop = OldNode->Data
Stack = OldNode->Node
deallocate(OldNode)
END FUNCTION

FUNCTION Top (byref Stack AS MyStack ptr) AS STRING
Top = Stack->Data
END FUNCTION

FUNCTION IsEmpty (byref Stack AS MyStack ptr) AS INTEGER
IsEmpty = (Stack=0)
END FUNCTION


'-------------------------------------------------------------------------------
'Main code

DIM test AS MyStack pointer
DIM a AS STRING

DO
a$=INKEY$
IF a$>"" AND a$<>chr$(13) THEN PRINT a$; : push (a$, test)
LOOP UNTIL a$=CHR$(13)

PRINT

DO WHILE NOT IsEmpty(test)
PRINT Top (test);
PRINT Pop (test);
LOOP

SLEEP

'------------------------------------------------------------------------------- [/syntax]