@@ -28,7 +28,159 @@ A PHP library providing common Stack implementation.
28
28
require_once('path/to/vendor/autoload.php');
29
29
```
30
30
31
- *** TODO: Add example code***
31
+ ### Handling a stack
32
+
33
+ #### Pushing to the stack
34
+
35
+ ``` {php}
36
+ use Markenwerk\StackUtil\Stack;
37
+
38
+ $stack = new Stack();
39
+
40
+ $stack
41
+ ->push('Item')
42
+ ->push('Another item')
43
+ ->push(12)
44
+ ->push(null)
45
+ ->push(8.12);
46
+
47
+ $stackSize = $stack->size();
48
+ echo 'Stack size: ' . $stackSize . PHP_EOL;
49
+ ```
50
+
51
+ ##### Output
52
+
53
+ ``` {http}
54
+ Stack size: 5
55
+ ```
56
+
57
+ ### Reading from the stack
58
+
59
+ #### Getting the last item
60
+
61
+ ``` {php}
62
+ $lastItem = $stack->get();
63
+ echo 'Last item: ' . $lastItem . PHP_EOL;
64
+ ```
65
+
66
+ ##### Output
67
+
68
+ ``` {http}
69
+ Last item: 8.12
70
+ ```
71
+
72
+ #### Getting an item by index
73
+
74
+ If the index is not available ` null ` is returned
75
+
76
+ ``` {php}
77
+ $secondItem = $stack->get(1);
78
+ echo 'Second item: ' . $secondItem . PHP_EOL;
79
+ ```
80
+
81
+ ##### Output
82
+
83
+ ``` {http}
84
+ Second item: Another item
85
+ ```
86
+
87
+ ### Popping from the stack
88
+
89
+ ``` {php}
90
+ $poppedItem = $stack->pop();
91
+ echo 'Popped item: ' . $poppedItem . PHP_EOL;
92
+
93
+ $stackSize = $stack->size();
94
+ echo 'Stack size: ' . $stackSize . PHP_EOL;
95
+ ```
96
+
97
+ ##### Output
98
+
99
+ ``` {http}
100
+ Popped item: 8.12
101
+ Stack size: 4
102
+ ```
103
+
104
+ ### Updating stacked values
105
+
106
+ #### Updating the last item
107
+
108
+ ``` {php}
109
+ $lastItem = $stack
110
+ ->set('9.12')
111
+ ->get();
112
+ echo 'Updated last item: ' . $lastItem . PHP_EOL;
113
+ ```
114
+
115
+ ##### Output
116
+
117
+ ``` {http}
118
+ Updated last item: 9.12
119
+ ```
120
+
121
+ #### Updating an item by index
122
+
123
+ ``` {php}
124
+ $thirdItem = $stack
125
+ ->set('Third item', 2)
126
+ ->get(2);
127
+ echo 'Updated third item: ' . $thirdItem . PHP_EOL;
128
+ ```
129
+
130
+ ##### Output
131
+
132
+ ``` {http}
133
+ Updated third item: Third item
134
+ ```
135
+
136
+ ### Removing from the stack
137
+
138
+ ``` {php}
139
+ echo 'Stack size: ' . $stack->size() . PHP_EOL;
140
+ $stack->delete(1);
141
+ echo 'Stack size: ' . $stack->size() . PHP_EOL;
142
+ ```
143
+
144
+ ##### Output
145
+
146
+ ``` {http}
147
+ Stack size: 4
148
+ Stack size: 3
149
+ ```
150
+
151
+ ### Iterating over the stack
152
+
153
+ #### Using ` foreach `
154
+
155
+ ``` {php}
156
+ foreach ($stack as $stackItemKey => $stackItemValue) {
157
+ echo 'Stack item ' . $stackItemKey . ': ' . $stackItemValue . PHP_EOL;
158
+ }
159
+ ```
160
+
161
+ ##### Output
162
+
163
+ ``` {http}
164
+ Stack item 0: Item
165
+ Stack item 1: Third item
166
+ Stack item 2: 9.12
167
+ ```
168
+
169
+ #### Using ` for `
170
+
171
+ ``` {php}
172
+ for ($i = 0; $i < $stack->size(); $i++) {
173
+ echo 'Stack index ' . $i . ': ' . $stack->get($i) . PHP_EOL;
174
+ }
175
+ ```
176
+
177
+ ##### Output
178
+
179
+ ``` {http}
180
+ Stack index 0: Item
181
+ Stack index 1: Third item
182
+ Stack index 2: 9.12
183
+ ```
32
184
33
185
---
34
186
0 commit comments