1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<TITLE>The Multi-Purpose Stack / Array / Queue Type</TITLE>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<BODY>
<HR>
<div align="center"><img id="headerGraphic" src="images/sludge300.png" alt="SLUDGE"/></div>
<h2>The Multi-Purpose Stack / Array / Queue Type</h2>
<HR>
<P>
The stack variable type is the only type of variable in SLUDGE which can hold more than one element. It can be used as a stack (push elements on using <a href="pushToStack.html">pushToStack</a> and then use <a href="popFromStack.html">popFromStack</a> to pop them off the same end), a queue (<a href="enqueue.html">enqueue</a> elements, <a href="dequeue.html">dequeue</a> them off the opposite end) or a straightforward array (which can be indexed using square brackets). With the implementation used in SLUDGE, <a href="dequeue.html">dequeue</a> and <a href="popFromStack.html">popFromStack</a> are identical.
</P>
<P>
Throughout this documentation, they are referred to as stacks.
</P>
<P>
Take, for example, the following code:
</P>
<P>
<pre>var stuff = newStack ("Hello?", "Hey!", "Bye then.");
# stuff now contains "Hello?", "Hey!", "Bye then."
pushToStack (stuff, "Aarrgghh!");
# stuff now contains "Aarrgghh!", "Hello?", "Hey!", "Bye then."
enqueue (stuff, "Er...?");
# stuff now contains "Aarrgghh!", "Hello?", "Hey!", "Bye then.", "Er...?"
var a = popFromStack (stuff);
# stuff now contains "Hello?", "Hey!", "Bye then.", "Er...?"
# a contains "Aarrgghh!"
var b = dequeue (stuff);
# stuff now contains "Hey!", "Bye then.", "Er...?"
# b contains "Hello?"
var c = stuff[1];
# stuff still contains "Hey!", "Bye then.", "Er...?"
# c contains "Bye then."
deleteFromStack (stuff, c);
# stuff now contains "Hey!", "Er...?"</pre>
</P>
<P>
When treating a stack as a Boolean, it is considered to be TRUE if it is nonempty and FALSE if it is empty. Therefore you can use the following structure to loop until a stack is empty (this example makes the ego character say every element of the stack in order):
</P>
<P>
<pre>while (myStack) say (ego, popFromStack (myStack));</pre>
</P>
<P>
A good example of using these stacks is for implementation of an inventory. Some built-in functions which return more than one element (<a href="getMatchingFiles.html">getMatchingFiles</a> and <a href="wait.html">wait</a> for example) also return information using the stack variable type.
</P>
<H3>See also:</H3>
<P>
<a href="copyStack.html">copyStack</a>
</P>
<P>
<a href="deleteFromStack_and_deleteAllFromStack.html">deleteAllFromStack</a>
</P>
<P>
<a href="deleteFromStack_and_deleteAllFromStack.html">deleteFromStack</a>
</P>
<P class="copyright-notice">SLUDGE and this SLUDGE documentation are <A HREF="Copyright.html">copyright</A> Hungry Software and contributors 2000-2012
</P>
<HR>
</BODY>
</html>
|