File: The_Multi-Purpose_Stack___Array___Queue_Type.html

package info (click to toggle)
sludge 2.2.2-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,912 kB
  • sloc: cpp: 32,441; sh: 1,237; xml: 874; makefile: 682
file content (83 lines) | stat: -rwxr-xr-x 3,317 bytes parent folder | download | duplicates (7)
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 (&quot;Hello?&quot;, &quot;Hey!&quot;, &quot;Bye then.&quot;);
# stuff now contains &quot;Hello?&quot;, &quot;Hey!&quot;, &quot;Bye then.&quot;

pushToStack (stuff, &quot;Aarrgghh!&quot;);
# stuff now contains &quot;Aarrgghh!&quot;, &quot;Hello?&quot;, &quot;Hey!&quot;, &quot;Bye then.&quot;

enqueue (stuff, &quot;Er...?&quot;);
# stuff now contains &quot;Aarrgghh!&quot;, &quot;Hello?&quot;, &quot;Hey!&quot;, &quot;Bye then.&quot;, &quot;Er...?&quot;

var a = popFromStack (stuff);
# stuff now contains &quot;Hello?&quot;, &quot;Hey!&quot;, &quot;Bye then.&quot;, &quot;Er...?&quot;
# a contains &quot;Aarrgghh!&quot;

var b = dequeue (stuff);
# stuff now contains &quot;Hey!&quot;, &quot;Bye then.&quot;, &quot;Er...?&quot;
# b contains &quot;Hello?&quot;

var c = stuff[1];
# stuff still contains &quot;Hey!&quot;, &quot;Bye then.&quot;, &quot;Er...?&quot;
# c contains &quot;Bye then.&quot;

deleteFromStack (stuff, c);
# stuff now contains &quot;Hey!&quot;, &quot;Er...?&quot;</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>