Implementing a Stack in QBasic ------------------------------ Before we write a program that involves a stack, we must first decide which QBasic tools we'll use to implement it. Because we can manipulate the elements quite straightforwardly with an array, this is perhaps the best choice for us to make. However, we can only have a bounded stack now since the dimensions of our array will limit the depth of the stack. Let's see how a stack operates. The value of the function EmptyStack is True if the stack is empty, otherwise it's False. The function OutStack pops an element off the stack and takes its value. The procedure InStack (x) pushes the element x onto the stack. An array STACK containing 100 elements is allocated to hold the elements of the stack. Here's our example for the bounded stack: ' PROGRAM 6.1 ' ******************************************* ' * The Demonstration of a Bounded Stack * ' ******************************************* ' The program firstly pushes NS sequential natural numbers ' onto the stack, then pushes OS numbers from the stack and ' outputs them onto the screen (NS and OS are defined by the user). DECLARE FUNCTION EmptyStack! () DECLARE SUB InStack (X!) DECLARE FUNCTION OutStack! () DIM SHARED DeepStack, TopStack CONST True = -1 CONST False = NOT True ' *** Initialization of the stack *** DeepStack = 100 'defining the depth of the stack DIM SHARED Stack(DeepStack) 'defining the array for the stack TopStack = 0 ' ******* Demonstration of a Stack Operation ******* CLS PRINT "Demonstration of a Bounded Stack" PRINT " (stack's depth - 100)" PRINT INPUT "Number of elements to push onto the stack? "; NS ' *** Filling the Stack *** FOR I = 1 TO NS CALL InStack(I) NEXT I ' *** Popping Elements from the Stack *** INPUT "Number of elements to pop from the stack? ", OS FOR I = 1 TO OS IF NOT EmptyStack THEN X = OutStack PRINT X; ELSE PRINT : PRINT "Stack is empty! "; PRINT I; "- th element cannot be retrieved." EXIT FOR END IF NEXT I END FUNCTION EmptyStack IF TopStack = 0 THEN EmptyStack = True ELSE EmptyStack = False END IF END FUNCTION SUB InStack (X) IF TopStack = DeepStack THEN PRINT "Error: Stack is overfilled!" STOP ELSE TopStack = TopStack + 1 Stack(TopStack) = X END IF END SUB FUNCTION OutStack IF TopStack = 0 THEN PRINT : PRINT "Error: Stack is empty!" STOP ELSE OutStack = Stack(TopStack) TopStack = TopStack - 1 END IF END FUNCTION The code first asks you for the number of elements you want the stack to contain , and when it receives an answer ns, it fills the stack with the numbers 1 to ns. If ns is greater than 100, you'll see a display of the overflow message and the program will stop. Otherwise, you'll be asked how many elements you want to pop off the stack and the retrieved elements will be output to the screen. If you want more elements than the stack contains, after the last element is retrieved a message will appear on the screen telling you that it's impossible to pop the next element because there aren't any left in the stack. This sample was taken from the forthcoming book, `The Revolutionary Guide to QBasic', published by Wrox Press, e-mail adrians@wrox.com or check-out our web-page http://www.wrox.com/ for further details. Thanks to Wrox Press and Adrian Sill who is an editor for the book and will kindly donate articles to issues of the fanzine. This book looks and sounds good with not just beginners questions in it. It's THE book for the BASIC programmer. Thank you. -------------------------------------------------------- * EDITOR'S NOTE: * This article was originally printed in Peter Cooper's BASIX Fanzine, * Issue #1 from November 1995. This tutorial was written by Adrian, * and went on to become a section of 'The Revoloutionary Guide to QBasic' * by This Knox Press.