Articles 3D Audio Custom Controls General RB Graphics Hacks Mac OS X Menus Novelty Printing REALbasic 2005 REALbasic 2006 Registration Resources Reviews Serial Speech Sockets XML Video Resource Links News Current News February 2006 January 2006 December 2005 November 2005 October 2005 September 2005 August 2005 July 2005 June 2005 May 2005 April 2005 March 2005 ![]() REALbasic for Dummies by Erick Tejkowski ![]() Learning REALbasic through Applications REALbasic for Macintosh REALbasic Cross-Platform Application Development
Older files are in Stuffit 5 or greater format. Newer files are ".Zip". Download StuffIt Expander |
|
Now, when you think of a stack you may think of a stack of papers, or a stack of dirty plates. Unless you've used some older programming languages or delt with the Classic Mac OS's System Stack in Pascal or C, you probably don't think of a data structure. Whether we're talking about programming or in life, a stack is an association of items such that the last item stored is the first item retrieved. With our dirty dishes, we remove the last plate from the table and place it on the top of the stack in the sink. Days later when we decide it's time to wash those dishes, the first plate we grab is the one from the top, the last one which was placed on the stack. We recognize this attribute of stacks and give it the caption "First in Last Out", or FILO. FIFOs on the other hand, (first in first out) are called "queues" but we won't be getting into those. In programming, a stack is basically an array where there are two functions; one to add an item or "push" it on to the stack, and one to remove an item, or "pop" it. In REALbasic, there is no such thing as a "stack" or a Stack class, but instead, we can treat an array of any datatype as a stack by using the Append and Pop methods. (Pop was added to REALbasic 5.) When we want to push an item on to the stack, we call the Append method, and to retrieve it, we call Pop. If for instance we did this: dim nums(-1), x as integer nums.Append 1 When we use the the Pop method we get this: x = nums.Pop // x = 5 As you can see, Pop not only retrieves the topmost item in the stack, it also removes it. It's important to remember this. So when are stacks used? Stacks are most offen used in the place of recursion, which is where a function will call itself when some condition is met. A specific example where a stack is used is searching for a specific file in a given folder. Typically, a SearchFolder(filename as string, rootFolder as folderItem) as FolderItem method may be written which returns the folderitem for the file if it was found. SearchFolder would examine all of the items in the rootFolder, and if it comes across a folder within rootFolder, it would call itself again, passing the appropriate rootFolder.Item(n) as the rootFolder. This recursive method of searching is done all the time, not only for files, but also whenever any kind of tree-like heirarchial structure needs to be examined. However it does have some downsides. If you've used recursion before, you may have caused a StackOverflowException* by having too many levels of recursion (SearchFolder called itself too many times for REALbasic to handle). By using a stack, you can rid of this problem and actually pick up a little bit of a performance boost too. * If you've never used recursion or don't know what a StackOverflowException is, don't worry about and pretend you never read that. While the "Stack" does refer to a stack of what we're talking about, you'll never receive a StackOverflowException when using a stack since the conceptual idea of a stack is used differently. Using a Stack The interface for the project is simply a listbox named "FilesList" and a pushbutton with the following code:
As you can follow from the comments, a dialog is displayed in which the user selects a folder. That folder then becomes the first item in the stack. The While-Wend loop then Pops each item in stack (one per iteration of the While loop) and then the For loop goes through each item inside of the folder that was popped from the stack. If and only if the itemInFolder is a folder itself, is it added to the stack. The only items ever in the stack are folder since only the folders need to be inspected. |
||||
|
||||||||||||||||||||||||||||||||
Maintained by the Staff of ResExcellence. This entire site ©1997-2006 ResExcellence
Privacy Statement? Sure we gotta Privacy Statement.
[an error occurred while processing this directive]