Results 1 to 5 of 5

Thread: A Stack of Quads

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    thinBasic MVPs danbaron's Avatar
    Join Date
    Jan 2010
    Location
    California
    Posts
    1,378
    Rep Power
    152

    A Stack of Quads

    [font=courier new]Here is a script that implements a stack of Quads.


    Dan :P

    [code=thinbasic]'-----------------------------------------------------------------------------------------------------------------------
    'file = qstack.tbasicc
    '-----------------------------------------------------------------------------------------------------------------------
    Uses "console"
    '-----------------------------------------------------------------------------------------------------------------------

    'Implementation of a stack, to hold Quads.

    'As I bet you know, the name, "stack", comes from the analogy to a stack of plates.
    'The first plate, the one that you start a stack with (on the bottom), is the last plate that you remove from it.
    'The last plate, that you put on the top, is the first plate that you remove from it.
    'So, in computer science, a stack is a LIFO (last in first out) structure.
    '"push" means, "put a plate on the stack".
    '"pop" means, "remove a plate from the stack".
    'Here, "plate", means "Quad".

    'I made it to use with something else.
    'I did it without much thinking - instead, typing and changing until it seemed to work.
    'I know that is the lazy, unreliable way to do it; detail by detail, until,
    'it seems to magically do what it is supposed to do. In that case, the plus side is - it works,
    'the minus side is - you may not know how or why.

    'It could be buggy, but it seems to function OK.

    '-----------------------------------------------------------------------------------------------------------------------

    %quadsize = SizeOf(Quad)
    %nodesize = %quadsize + SizeOf(DWord)

    Global stack As DWord = 0

    '-----------------------------------------------------------------------------------------------------------------------

    Function TBMain()
    Local ss As DWord = 4
    Local i, j As Integer
    Local v As Quad

    For i = 1 To 2

    For j = -ss To ss
    push(j)
    Next

    For j = -ss To ss + 1
    If emptystack() Then
    Console_WriteLine("Stack empty.")
    Console_WriteLine()
    Else
    v = pop()
    Console_WriteLine(v)
    EndIf
    Next

    Next

    Console_WriteLine("Done. Press a key.")
    WaitKey
    End Function

    '-----------------------------------------------------------------------------------------------------------------------

    Function push(v As Quad)
    Local plate As DWord
    plate = HEAP_Alloc(%nodesize)
    Poke(Quad, plate, v)
    Poke(DWord, plate + %quadsize, stack)
    stack = plate
    End Function

    '-----------------------------------------------------------------------------------------------------------------------

    Function pop() As Quad
    Local v As Quad
    Local crackedplate As DWord
    crackedplate = stack
    v = Peek(Quad, stack)
    stack = Peek(DWord, stack + %quadsize)
    HEAP_Free(crackedplate)
    Return v
    End Function

    '-----------------------------------------------------------------------------------------------------------------------

    Function emptystack() As Byte
    If stack = 0 Then Return TRUE
    Return FALSE
    End Function

    '-----------------------------------------------------------------------------------------------------------------------
    [/code]
    Attached Files Attached Files
    "You can't cheat an honest man. Never give a sucker an even break, or smarten up a chump." - W.C.Fields

Similar Threads

  1. triangles and quads
    By kryton9 in forum M15 file format
    Replies: 7
    Last Post: 19-12-2007, 14:10
  2. Cylinder from Quads
    By matthew in forum TBGL Scripts and Projects
    Replies: 6
    Last Post: 02-06-2007, 20:04

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •