Skip to content

Commit 18ce0a6

Browse files
authored
Create StackExample.ino
1 parent aaea171 commit 18ce0a6

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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\nLinkedPointerList\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+
}

0 commit comments

Comments
 (0)