Author: Efstathios Chatzikyriakidis Contact: [email protected] Library was updated for Arduino 1.0
1.0 2014-09-22 - Vincent Evrard - added unshift(): add item to the array. (javascript like) 1.0 2010-09-25 - Efstathios Chatzikyriakidis - added resize(): for growing, shrinking the array size. 1.0 2010-09-25 - Efstathios Chatzikyriakidis - added resize(): for growing, shrinking the array size. 1.0 2010-09-23 - Efstathios Chatzikyriakidis - added exit(), blink(): error reporting and handling methods. 1.0 2010-09-20 - Alexander Brevig - added setPrinter(): indirectly reference a Serial object. 1.0 2010-09-15 - Efstathios Chatzikyriakidis - initial release of the library. StackArray is a library implementing a generic, dynamic stack (array version) for the Arduino.It is created to help adding LIFO (Last In - First Out) Abstract Data Structure to a program for any use.
StackArray library is part of the "Data Structures & Algorithms" libraries.
Download here: StackArray.zipPut the "StackArray" directory in "libraries" directory.
In the Arduino IDE, create a new sketch and select from the menubar "Sketch->Import Library->StackArray".
Once the library is imported, an "#include <StackArray.h>" line will appear at the top of your Sketch.
Replace "T" with the data type you want to use.StackArray stack;
Create instance (object) of a StackArray class.
Replace "T" with the data type you want to use.Add an item with data type "T" to the top of stack.
Add an item with data type "T" to the bottom of stack.
Remove and return an item with data type "T" from the bottom stack.
Get an item with data type "T" from the stack.
Check if the stack is empty.
Check if the stack is full.
Get the number of items in the stack.
Sets the printer of the stack.
/*
* Reverse a string by using a generic, dynamic stack data structure.
*
* Copyright (C) 2010 Efstathios Chatzikyriakidis ([email protected])
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// include stack library header.
#include <StackArray.h>
// declare a string message.
const String msg = "Happy Hacking!";
// create a stack of characters.
StackArray <char> stack;
// startup point entry (runs once).
void setup () {
// start serial communication.
Serial.begin (9600);
// set the printer of the stack.
stack.setPrinter (Serial);
// print the message before reversing.
Serial.println ("Normal String: " + msg);
// push all the message's characters to the stack.
for (int i = 0; i < msg.length (); i++)
stack.push (msg.charAt (i));
// print the message after reversing.
Serial.print ("Reversed String: ");
// pop all the message's characters from the stack.
while (!stack.isEmpty ())
Serial.print (stack.pop ());
// print end of line character.
Serial.println ();
}
// loop the main sketch.
void loop () {
// nothing here.
}
/*
* Swapping numbers by using a generic, dynamic stack data structure.
*
* Copyright (C) 2010 Efstathios Chatzikyriakidis ([email protected])
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// include stack library header.
#include <StackArray.h>
// declare two numbers.
double a = 1.1;
double b = 2.2;
// create a stack of numbers.
StackArray <double> stack;
// startup point entry (runs once).
void
setup () {
// start serial communication.
Serial.begin (9600);
// set the printer of the stack.
stack.setPrinter (Serial);
}
// loop the main sketch.
void loop () {
// print the values of the numbers.
Serial.print ("a: "); Serial.println (a);
Serial.print ("b: "); Serial.println (b);
// push the numbers to the stack.
stack.push (a);
stack.push (b);
// pop the numbers from the stack.
a = stack.pop ();
b = stack.pop ();
// delay 1 second.
delay (1000);
}
/*
* Store a string by using a generic, dynamic stack data structure.
*
* Copyright (C) 2014 Vincent Evrard ([email protected])
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// include stack library header.
#include <StackArray.h>
// declare a string message.
const String msg = "Happy Hacking!";
// create a stack of characters.
StackArray <char> stack;
// startup point entry (runs once).
void setup () {
// start serial communication.
Serial.begin (9600);
delay(2000);
// set the printer of the stack.
stack.setPrinter (Serial);
// print the message before reversing.
Serial.println ("Normal String: " + msg);
// push all the message's characters to the stack.
for (int i = 0; i < msg.length (); i++)
stack.unshift(msg.charAt (i));
// print the message after reversing.
Serial.print ("Reversed String: ");
// pop all the message's characters from the stack.
while (!stack.isEmpty ())
Serial.print( stack.pop() );
// print end of line character.
Serial.println ();
}
void loop(){
// nothing here.
}