File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ /* *
2+ * This example pushes between 3 and 9 random numbers to the stack and retrieves them in the reverse order.
3+ */
4+ #include < Arduino.h>
5+ #include " LinkedPointerList.h"
6+
7+ void setup () {
8+ Serial.begin (115200 );
9+ Serial.println (" \n\n LinkedPointerList\n " );
10+
11+ }
12+
13+ void loop () {
14+ // Make the stack
15+ LinkedPointerList<int > stack;
16+
17+ // Decide how many numbers will it generate
18+ int count = random (3 , 10 );
19+
20+ for (int i = 0 ; i < count; i++) {
21+ // Create a new int (it will have to be explicitly removed after use!)
22+ int *value = new int ;
23+ // Store a random value into it
24+ *value = random (1000 );
25+ // Push it to the stack
26+ stack.unshift (value);
27+ Serial.printf (" Pushed value: %d\n " , *value);
28+ }
29+
30+ Serial.println ();
31+
32+ while (stack.size () > 0 ) {
33+ // Retrieve the latest value from the stack
34+ int *value = stack.shift ();
35+ Serial.printf (" Fetched value: %d\n " , *value);
36+ // Delete the int object!
37+ delete value;
38+ }
39+
40+ Serial.println ();
41+ delay (2000 );
42+ }
You can’t perform that action at this time.
0 commit comments